0
votes

We have some JSON files that live on a filesystem in our internal network that are written by Ruby and read by Clojure (and also Ruby). We'd like to encrypt them to increase their security. We've used AES 256 CBC inside our Ruby project for other things that need symmetric encryption so we'd like to use that. However, this time, the encryption will need to be decrypted in a Clojure application. The output of encryption (using this as a guide: OpenSSL::Cipher), as represented in a Ruby string, looks like: "a\x96\xECLI\xBC%\xC4@{\xBD\x99%\xA1\x84\x84" and putting that into a Clojure REPL results in a bunch of "Syntax error... Unsupported escape character: \x" I tried making every "\" a "\\" but then using Clojure's library "Buddy-Core" and :aes256-cbc-hmac-sha512 as the encryption algorithm results in:

Execution error (AssertionError) at buddy.core.crypto/eval1558$fn (crypto.clj:478).
Assert failed: (keylength? key 64)

Even though the key/iv were a fine length when used to encrypt the string in Ruby.

To sum up:

  • To convert from a Ruby String to a Clojure String is it correct to replace all backslashes with double backslashes?
  • Is :aes256-cbc-hmac-sha512 the correct algorithm for use with Clojure's Buddy-Core to decrypt AES 256 CBC?
  • Would I be better off doing this in Java inside of Clojure? (sub question: Please do advise on converting Ruby Strings to Java/Clojure Strings)
2

2 Answers

0
votes

Whoops misread the question!

Try to avoid strings at all cost when working with raw binary data

0
votes

The Ruby string is not a valid Java string (i.e. \x is not a valid escape character) The allowed Java escape characters are here: https://docs.oracle.com/javase/specs/jls/se13/html/jls-3.html#jls-EscapeSequence

I'm not sure if Ruby strings are unicode by default, so this is a bit tricky. The safest bet is to turn your encryption result into BASE64 and use that.