在 PHP openssl 中加密并在 javascript CryptoJS 中解密

IT技术 javascript php openssl padding cryptojs
2021-03-05 20:38:36

我正在使用 PHP 加密一些参数

 openssl("parameter", "AES-256-ECB", "client")

我希望在CryptoJS 中解密

CryptoJS.AES.decrypt(parameter, "client", {mode: CryptoJS.mode.ECB}).toString(CryptoJS.enc.Utf8);

但它抛出一个空字符串。

有什么建议?

2个回答

CryptoJS:PHP openssl 加密 -> javascript 解密

PHP:

function CryptoJSAesEncrypt($passphrase, $plain_text){

    $salt = openssl_random_pseudo_bytes(256);
    $iv = openssl_random_pseudo_bytes(16);
    //on PHP7 can use random_bytes() istead openssl_random_pseudo_bytes()
    //or PHP5x see : https://github.com/paragonie/random_compat

    $iterations = 999;  
    $key = hash_pbkdf2("sha512", $passphrase, $salt, $iterations, 64);

    $encrypted_data = openssl_encrypt($plain_text, 'aes-256-cbc', hex2bin($key), OPENSSL_RAW_DATA, $iv);

    $data = array("ciphertext" => base64_encode($encrypted_data), "iv" => bin2hex($iv), "salt" => bin2hex($salt));
    return json_encode($data);
}

$string_json_fromPHP = CryptoJSAesEncrypt("your passphrase", "your plain text");

JS:

function CryptoJSAesDecrypt(passphrase,encrypted_json_string){

    var obj_json = JSON.parse(encrypted_json_string);

    var encrypted = obj_json.ciphertext;
    var salt = CryptoJS.enc.Hex.parse(obj_json.salt);
    var iv = CryptoJS.enc.Hex.parse(obj_json.iv);   

    var key = CryptoJS.PBKDF2(passphrase, salt, { hasher: CryptoJS.algo.SHA512, keySize: 64/8, iterations: 999});


    var decrypted = CryptoJS.AES.decrypt(encrypted, key, { iv: iv});

    return decrypted.toString(CryptoJS.enc.Utf8);
}

console.log(CryptoJSAesDecrypt('your passphrase','<?php echo $string_json_fromPHP?>'));

CryptoJS:javascript 加密 -> PHP openssl 解密

JS:

function CryptoJSAesEncrypt(passphrase, plain_text){

    var salt = CryptoJS.lib.WordArray.random(256);
    var iv = CryptoJS.lib.WordArray.random(16);
    //for more random entropy can use : https://github.com/wwwtyro/cryptico/blob/master/random.js instead CryptoJS random() or another js PRNG

    var key = CryptoJS.PBKDF2(passphrase, salt, { hasher: CryptoJS.algo.SHA512, keySize: 64/8, iterations: 999 });

    var encrypted = CryptoJS.AES.encrypt(plain_text, key, {iv: iv});

    var data = {
        ciphertext : CryptoJS.enc.Base64.stringify(encrypted.ciphertext),
        salt : CryptoJS.enc.Hex.stringify(salt),
        iv : CryptoJS.enc.Hex.stringify(iv)    
    }

    return JSON.stringify(data);
}

PHP:

function CryptoJSAesDecrypt($passphrase, $jsonString){

    $jsondata = json_decode($jsonString, true);
    try {
        $salt = hex2bin($jsondata["salt"]);
        $iv  = hex2bin($jsondata["iv"]);          
    } catch(Exception $e) { return null; }

    $ciphertext = base64_decode($jsondata["ciphertext"]);
    $iterations = 999; //same as js encrypting 

    $key = hash_pbkdf2("sha512", $passphrase, $salt, $iterations, 64);

    $decrypted= openssl_decrypt($ciphertext , 'aes-256-cbc', hex2bin($key), OPENSSL_RAW_DATA, $iv);

    return $decrypted;

}
解决方案: 1. 尝试在PHP 中找到一些与angular 兼容的函数(不幸的是我不使用angular)。2.使用上面的js函数重新加密您的原始数据,然后必须工作。(但有一个条件:如果来自 angular 的人没有更改 CryptoJS 的原始代码,请参阅github.com/sytelus/CryptoJS/tree/master/rollups
2021-04-28 20:38:36
这是您的更新代码。它还支持 AES-CBC 方法的所有 128、192、256。gist.github.com/ve3/0f77228b174cf92a638d81fddb17189d我必须为此感谢你。
2021-05-04 20:38:36
干得好。我仅将示例用于可与 Crypto.js (aes-256-cbc) 一起使用的最强加密
2021-05-10 20:38:36
@GaurangSondagar“你的功能不起作用”对你来说意味着什么?哪些功能对您不起作用,哪些错误或发生了什么?也许你有不同版本的cryptojs ...在我使用的mi测试中: github.com/sytelus/CryptoJS
2021-05-14 20:38:36
如果我使用其他算法,例如 AES-128-CBC、AES-192-CBC,这将不起作用
2021-05-20 20:38:36

请使用这种方法它对我有用..谢谢

typescript代码(Anuglar 4+):

encryptUsingAES256() {
let _key = CryptoJS.enc.Utf8.parse(your_token_here);
let _iv = CryptoJS.enc.Utf8.parse(your_token_here);
let encrypted = CryptoJS.AES.encrypt(
  this.request, _key, {
    keySize: 16,
    iv: _iv,
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  });
this.encrypted = encrypted.toString();
console.log(this.encrypted)
}


decryptUsingAES256() {
let _key = CryptoJS.enc.Utf8.parse(your_token_here);
let _iv = CryptoJS.enc.Utf8.parse(your_token_here);

this.decrypted = CryptoJS.AES.decrypt(
  this.encrypted, _key, {
    keySize: 16,
    iv: _iv,
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  }).toString(CryptoJS.enc.Utf8);
   console.log(this.decrypted) 
}