Encrypt with Node.js Crypto module and decrypt with Java (in Android app)

前端 未结 4 1383
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 22:26

Looking for a way to encrypt data (mainly strings) in node and decrypt in an android app (java).

Have successfully done so in each one (encrypt/decrypt in node, and

4条回答
  •  一生所求
    2020-11-29 23:27

    Apparently if you pass a passphrase to crypto.createCipher() it uses OpenSSL's EVP_BytesToKey() to derive the key. You can either pass a raw byte buffer and use the same to initialize Java's SecretKey, or emulate EVP_BytesToKey() in your Java code. Use $ man EVP_BytesToKey for more details, but essentially it hashes the passphrase multiple times with MD5 and concatenates a salt.

    As for using a raw key, something like this should let you use a raw key:

    var c = crypto.createCipheriv("aes-128-ecb", new Buffer("00010203050607080a0b0c0d0f101112", "hex").toString("binary"), "");

    Note that since you are using CBC, you need to use the same IV for encryption and decryption (you might want to append it to your message, etc.)

    Mandatory warning: implementing a crypto protocol yourself is rarely a good idea. Even if you get this to work, are you going to use the same key for all messages? For how long? If you decide to rotate the key, how to you manage this. Etc, .etc.

提交回复
热议问题