简体   繁体   中英

openssl_decrypt PHP to CryptoJS

In PHP I use openssl_decrypt($encryptData,'AES128',$key, OPENSSL_ZERO_PADDING|OPENSSL_RAW_DATA) and I need the exact same thing with same option on JS so I use CryptoJS with this function cryptoJS.AES.decrypt(encryptData, key, {padding: cryptoJS.pad.ZeroPadding}).toString() . With PHP my decoded message start with C7 with others hexa character. But in JS only decimal values.

Here encryptData and key on PHP and JS have the exact same value. But cryptoJS return something different from PHP. I don't know what can be the problem. I've tried to change the base of my values with UTF8 or Base64. But nothing works.

How can I convert this function to crypto and have it working? Thank you !

PHP example:

$encryptData = hex2bin("D5F630E93F36C21293012D78E5A384F1");
$key = hex2bin("A254FE00A791AA74386E8DEF3712B256");

var_dump(bin2hex(openssl_decrypt($encryptData,'AES128', $key, OPENSSL_ZERO_PADDING|OPENSSL_RAW_DATA)));
//result : c704469332aa61804601008a92dc10e5

JS example: (and of course hex2bin return the same output than in PHP)

let encData = hex2bin("D5F630E93F36C21293012D78E5A384F1");
let key = hex2bin("A254FE00A791AA74386E8DEF3712B256");
let data = bin2hex(cryptoJS.AES.decrypt(encData, key, {padding: cryptoJS.pad.ZeroPadding}).toString());
console.log(data);
//result : 326638346632336661323135353832343236376139343564 

Result change every time I reload on JS.

CryptoJS uses the WordArray type and provides encoders for conversion, eg the Hex encoder.

In the PHP code, aes128 is applied, which is an alias for aes-128-cbc and requires an IV. If none is specified, PHP implicitly uses a zero vector, which must be explicitly specified in the CryptoJS code. The CBC mode itself does not need to be explicitly specified, since it is the default .

Moreover the PHP code disables padding, which must be explicitly specified in the CryptoJS code, since PKCS7 padding is the default . Note that OPENSSL_ZERO_PADDING does not enable Zero padding, but disables padding, ie the CryptoJS counterpart is NoPadding .

Your CryptoJS code must be changed as follows to be functionally equivalent to the PHP code:

 let encData = CryptoJS.enc.Hex.parse("D5F630E93F36C21293012D78E5A384F1"); let key = CryptoJS.enc.Hex.parse("A254FE00A791AA74386E8DEF3712B256"); let iv = CryptoJS.enc.Hex.parse("00000000000000000000000000000000"); let data = CryptoJS.AES.decrypt( {ciphertext: encData}, key, {iv: iv, padding: CryptoJS.pad.NoPadding} ).toString(CryptoJS.enc.Hex); console.log(data); // c704469332aa61804601008a92dc10e5
 <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>

Please also note that a static IV is insecure. Typically, a random IV is generated for each encryption, which is not secret and passed along with the ciphertext (usually concatenated) to the recipient, who separates both before decryption.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM