Match PHPs openssl_encrypt with blank IV in JavaScript -


how can match output of openssl_encrypt in javascript, when no iv declared in php , non-compliant key length used?

php

php -r '$value = openssl_encrypt("test", "aes-128-cbc", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); echo $value;'  /u+5lb/vwbmx9u1yy4cncq== 

javascript

iv  = cryptojs.enc.utf8.parse(""); var encrypted = cryptojs.aes.encrypt("test", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa", {iv: iv, mode: cryptojs.mode.cbc, padding: cryptojs.pad.pkcs7}); console.log(encrypted.tostring());  u2fsdgvkx19cn1f6x8c/rjdfwzszk5m5wwcurr4z3u4=  console.log(cryptojs.enc.base64.stringify(encrypted.ciphertext));  l1/doxmtmblzyjsthjpdtg== 

i can modify javascript code , need way encrypt string can decrypted php's openssl_decrypt. works fine if string it's encrypted php.

i understand not declaring iv renders code less secure, in particular case it's not big issue.

i've read in other topics php defaults padding pkcs7, why added js method.

my current theory either default iv of php issue or output of js function needs further processed. can see tried base64.stringify method, results still different.

the sample key i'm using here of same length of actual key.

i'm using https://github.com/sytelus/cryptojs/blob/master/rollups/aes.js (whatever use in js, needs distributed standalone file, relatively small footprint)

there issues code:

  • if want use existing key, need provide wordarray object cryptojs.<cipher>.encrypt. means has parsed in way. if provide string "key", cryptojs assume password, generate random salt , use evp_bytestokey derive key , iv password , salt.
  • your "key" 29 characters long. aes supports 3 key sizes: 16, 24 , 32 bytes. since you've used aes-128 in php, need provide 16 byte key in cryptojs aes-128 automatically selected. remember: key supposed randomly chosen , indistinguishable random noise has kind of security. if must print key in way, use hex or base64 encoding.

full example:

var iv  = cryptojs.enc.utf8.parse("");  var key = cryptojs.enc.utf8.parse("aaaaaaaaaaaaaaaa");  var encrypted = cryptojs.aes.encrypt("test", key, {      iv: iv,       mode: cryptojs.mode.cbc,       padding: cryptojs.pad.pkcs7  });    console.log(cryptojs.enc.base64.stringify(encrypted.ciphertext));
<script src="https://cdn.rawgit.com/cryptostore/crypto-js/3.1.2/build/rollups/aes.js"></script>


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -