Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to encrypt/decrypt strings with block ciphers #2

Closed
tjarvstrand opened this issue May 17, 2015 · 3 comments
Closed

How to encrypt/decrypt strings with block ciphers #2

tjarvstrand opened this issue May 17, 2015 · 3 comments

Comments

@tjarvstrand
Copy link

Hi,

I can't really seem to figure out how to encrypt and decrypt strings using buddy-core.

(defn test []
  (let [eng   (crypto/block-cipher :twofish :cbc)
        iv16  (nonce/random-nonce 16)
        key32 (nonce/random-nonce 32)
        data  (codecs/str->bytes "00000000000000000000000000000000")]
    (crypto/initialize! eng {:key key32 :iv iv16 :op :encrypt})
    (let [r (crypto/process-bytes! eng data)]
      (crypto/initialize! eng {:key key32 :iv iv16 :op :decrypt})
      (codecs/bytes->str (crypto/process-bytes! eng data)))))

Doesn't actually return the string of zeroes, but some value that looks binary.

What am I doing wrong?

@niwinz
Copy link
Member

niwinz commented May 17, 2015

In the last line, you should replace data with r. In your snippet you are trying decrypt plain data and not the previously encrypted data.

@niwinz
Copy link
Member

niwinz commented May 17, 2015

If you are working with strings, maybe you find that https://github.com/funcool/buddy-sign/blob/master/src/buddy/sign/jwe.clj#L84 useful. Is a collection of helpers for work with arbitrary length strings and encrypt/decrypt them using block ciphers.

In future versions I have plan expose them (or similar) as public api in buddy-core.

@tjarvstrand
Copy link
Author

Of course, that was clumsy of me. I had some troubles getting it to work and I was sloppy when trying to write a minimal example. Thanks for the pointers, that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants