encryption - Node.js crypto key and iv to match java SecretKeySpec / IvParameterSpec -


i'm trying to port java (simple) encryption algorythm node js. need able decrypt/encrypt stuff encrypted/decrypted java side.

i'm stuck @ beginning, initialization of cipher.

in java, key secretkeyspec, , initialization vector ivparameterspec:

public cryptstuff(string password) throws zillion_exceptions {     if (password==null) throw new invalidkeyexception("no encryption password set!");     key = new secretkeyspec(password.getbytes("utf-8"), "aes");     cipher = cipher.getinstance("aes/cbc/pkcs5padding");     ivspec=new ivparameterspec(new byte[cipher.getblocksize()]);     cipher.init(cipher.encrypt_mode, key, ivspec); } 

nodejs requires key buffer , iv buffer, however, don't know how calculate them scratch:

var mcrypto = require('crypto'),     key=[0,0,0,0,0,0,.......],     iv=[0,0,0,0,0,.........];  function init (password) {      // generate key password     // generate iv blocksize?      var aescipher = mcrypto.createcipheriv("aes-????????", (new buffer(key)), (new buffer(iv)));     .     .     . } 

also, what's matching algorithm string aes/cbc/pkcs5padding?

assuming have same password string in java code, can create key buffer in node:

var key = new buffer(password, "utf8"); 

since you're using 0 filled iv (bad!) in java, equivalent code in node:

var iv = new buffer(16); // 16 byte buffer random data iv.fill(0); // fill zeros 

since you're using cbc mode in java, have same in node. note have select correct key size when selecting cipher string depending on "password" length:

var aescipher = mcrypto.createcipheriv("aes-128-cbc", key, iv); // or var aescipher = mcrypto.createcipheriv("aes-192-cbc", key, iv); // or var aescipher = mcrypto.createcipheriv("aes-256-cbc", key, iv); 

node automatically apply pkcs#7 padding same pkcs#5 padding aes.

a password not key!

a password has not appropriate length used key (valid lengths 16 byte, 24 byte , 32 byte aes) , comprised of printable characters might make easier attacker brute force key.

what need create key password key derivation function. popular ones pbkdf2, bcrypt , scrypt (with increasing cost).

random iv!

you should generating new random iv every ciphertext produce. if use static iv, attacker observes ciphertexts can determine sent same or similar messages. if use random iv, ciphertexts differ attacker cannot determine whether 2 different ciphertexts created same plaintext or not. called semantic security.

the random iv doesn't have secret, can prepend ciphertext , slice off before decryption.

you can combine key derivation function (kdf). generate random salt kdf. kdf able derive variable amount of output bytes, let derive key || iv (concatenation) , split them. now, need prepend salt ciphertext.

authentication!

depending on system, might vulnerable attacks such padding oracle attack. best defense against authenticate ciphertext. can either use encrypt-then-mac scheme strong mac such hmac-sha256 or authenticated mode of operation such gcm or eax. java , node both support gcm, there little more work involved.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -