I am trying to send AES encrypted messages between javascript and php using a shared secret. In Javascript I am using the CryptoJS library. In php, I am using mycrypt. I am trying to construct an encrypted message in javascript and then decrypt it in php using the shared secret. I can encrypt and decrypt a message in Javascript. I can encrypt and decrypt the same message in php - but the encryption is not the same between the two.
The Javascript
var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase");
alert(encrypted);
Gives
U2FsdGVkX18+k3pba4l4MbGZfmDjMc1yQ6uj1fg+BGo=
In php
<?php
$Pass = "Secret Passphrase";
$Clear = "Message";
$crypted = fnEncrypt($Clear, $Pass);
echo "Encrypted: ".$crypted."</br>";
$newClear = fnDecrypt($crypted, $Pass);
echo "Decrypted: ".$newClear."</br>";
function fnEncrypt($sValue, $sSecretKey) {
return rtrim(
base64_encode(
mcrypt_encrypt(
MCRYPT_RIJNDAEL_256,
$sSecretKey, $sValue,
MCRYPT_MODE_ECB,
mcrypt_create_iv(
mcrypt_get_iv_size(
MCRYPT_RIJNDAEL_256,
MCRYPT_MODE_ECB
),
MCRYPT_RAND
)
)
),"\0"
);
}
function fnDecrypt($sValue, $sSecretKey) {
return rtrim(
mcrypt_decrypt(
MCRYPT_RIJNDAEL_256,
$sSecretKey,
base64_decode($sValue),
MCRYPT_MODE_ECB,
mcrypt_create_iv(
mcrypt_get_iv_size(
MCRYPT_RIJNDAEL_256,
MCRYPT_MODE_ECB
),
MCRYPT_RAND
)
),"\0"
);
}
The output is
Encrypted: iqJ0R5ahRP7GpWKtW7+OBSCGnudDr99VbJC36OQlmgE=
Decrypted: Message
My question is, why are these not the same?