gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:00:08 29 4
gpt4 key购买 nike

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

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

我希望在 CryptoJS 中解密:

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

但它抛出一个空字符串。

有什么建议吗?

最佳答案

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;

}

在我使用的 mi 测试中: github.com/sytelus/CryptoJS

关于javascript - 在 PHP openssl 中加密并在 javascript CryptoJS 中解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41222162/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com