I have a PHP script that creates a AES-256-CBF8 Encryption for given string. I want to create a equivalent code for the same encryption in javascript. I used CryptoJS for the same. But my encrypted code for javascript and php both differs.
I have tried this:
PHP:
$secret_key = 'qIthpcluB8xA4Y0CGS7ahl3kfluBay7p';
$secret_iv = '99FF8B0332880F69D14110316D640AFFA8F422311C1576AF055A00498A88EEE80D337FBB1E4B8081A0901E9A1750806B2B371E7438AB968E4C1C8D3EF05A81ED';
$output = false;
$encrypt_method = "AES-256-CFB8";
$key = hash( 'sha256', $secret_key );
$iv = substr( hash( 'sha256', $secret_iv ), 0, 16 );
if( $action == 'enc' )
{
$output = base64_encode( openssl_encrypt( $string, $encrypt_method, $key, 0, $iv ) );
}
else if( $action == 'dec' )
{
$output = openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $iv );
}
else
{
return false;
}
return $output;
JavaScript:
var message = '{"Request":"login","Username":"123456","API_AccessKey":"b57a4d91965d456","GMT_Timestamp":"101439"}';
var key = CryptoJS.SHA256("qIthpcluB8xA4Y0CGS7ahl3kfluBay7p"); //length=22
console.log(key);
var iv1 = CryptoJS.SHA256("99FF8B0332880F69D14110316D640AFFA8F422311C1576AF055A00498A88EEE80D337FBB1E4B8081A0901E9A1750806B2B371E7438AB968E4C1C8D3EF05A81ED"); //length=22
var iv = iv1.toString().substring(0,16);
var key1 = key.toString();
console.log(iv);
console.log(key);
//iv.substring(0,16);
//iv is now 987185c4436764b6e27a72f2fffffffd, length=32
var cipherData = CryptoJS.AES.encrypt(message, key1, { iv: iv },{mode: CryptoJS.mode.CFB8});
console.log(cipherData.toString());
//var cipherData="NUUyUDZPVGFLUzVCSUZpRUR3S28vN3dwY2ZLbjVTeDRDc25aTUdmS2pYc3VlTFBFWEpxVFVENmRIV1BCTUdxQXo4UHpOdTlqK2lqcXVNWlBZdTQvTlFlSW5CZnI5UHdiQ1ovNEhCUHU2KytyT3dyOCtrLzBmQT09";
var data = CryptoJS.AES.decrypt(cipherData.toString(), key1, { iv: iv },{mode: CryptoJS.mode.CFB});
console.log(data);
var NewCipher = CryptoJS.enc.Utf8.parse(cipherData);
document.getElementById("demo0").innerHTML = message;
document.getElementById("demo1").innerHTML = CryptoJS.enc.Base64.stringify(NewCipher);
document.getElementById("demo2").innerHTML = data;
//document.getElementById("demo3").innerHTML = CryptoJS.enc.Base64(data.toString(CryptoJS.enc.Utf8));
document.getElementById("demo3").innerHTML = data.toString(CryptoJS.enc.Utf8);
Please help me in this.