JS CryptoJS encrypted -> PHP OpenSSL decrypted: cipher changes length

1 answer

I have a problem which i cant understand. I programmed some code to encrypt data in JS with CryptoJS and to decrypt this data in PHP with OpenSSL. Sometimes it work but most of the time it breaks


    PHP          JS $passphrase == passkey 


let data = CryptoJS.AES.encrypt(data, passkey).toString(); send(data); 


list($cipher, $salt) = decode($_POST["data"]); list($passkey, $iv) = evpkdf($passphrase, $salt);  $data = openssl_decrypt($cipher, "AES-256-CBC", $passkey, OPENSSL_RAW_DATA, $iv); echo $data  function evpkdf($passphrase, $salt) {     $salted = "";     $dx = "";     while(strlen($salted) < 48) {         $dx = md5($dx . $passphrase . $salt, true);         $salted .= $dx;     }     $passkey = substr($salted, 0, 32);     $iv = substr($salted, 32, 16);      return [$passkey, $iv]; }  function decode($data) {     $data = base64_decode($data);      if (substr($data, 0, 8) !== "Salted__") {         // throw new \InvalidArgumentException();         echo "no salt\n";     }      $salt = substr($data, 8, 8);     $cipher = substr($data, 16);      return [$cipher, $salt]; } 


   PHP    JS     iv == iv   salt == salt cipher ?? cipher ?? = sometimes 

The only thing which i noticed was that the Cipher is not allways the same length and only if the cipher has the right length it worked. What could be my error which i cant see. I allready tried a lot and i really have no idea what to do. I tried to change the passkey in JS or to look if something else is different, but as above only the cipher doesnt make sense all the time.

All answers to this question, which has the identifier 61218188

The best answer:

What kind of pass key are you using to encrypt the data ?

CryptoJS supports AES-128, AES-192, and AES-256. It will pick the variant by the size of the key you pass in. If you use a passphrase, then it will generate a 256-bit key.


Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data