PHP 中的 AES 加密和 Javascript 中的解密

Asi*_*ony 0 javascript php encryption aes

我有一个应用程序,其中使用 AES CBC 128 算法加密 json 编码数组,然后在 javascript(React/Next Js 项目)中对其进行解密。我在php中的加密如下面的代码所示

加密 PHP


$plaintext = "message to be encrypted";
$ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC");
$iv = openssl_random_pseudo_bytes($ivlen);
$ciphertext_raw = openssl_encrypt($plaintext, $cipher, $key, $options=OPENSSL_RAW_DATA, $iv);
$hmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary=true);
$ciphertext = base64_encode( $iv.$hmac.$ciphertext_raw );
Run Code Online (Sandbox Code Playgroud)

我在用 Javascript 解密时遇到问题

到目前为止我的代码如下所示

 const baseenc = CryptoJS.enc.Base64.parse(cipher).toString();
  var encrypted = CryptoJS.AES.decrypt(cipher, key, { iv: iv }).toString();
  var plaintext = CryptoJS.enc.Latin1.stringify(encrypted);
Run Code Online (Sandbox Code Playgroud)

任何人都可以显示错误是什么或帮助我获得正确的输出

Top*_*aco 5

CryptoJS 代码中必须执行以下步骤:

  • 单独的IV、HMAC和密文(Base64解码后)
  • 计算密文的 HMAC
  • 检查密文的真实性。如果接收到的和计算出的 HMAC 相同,则密文是真实的。
  • 仅当密文真实时才执行解密

下面的代码是一个可能的实现。0123456789012345当应用密钥并使用 PHP 代码时,生成了使用的密文:

var ciphertext = 'WqfMfCxKg7U7h5S1mbx7mSHOkkkIrUUpg++mX4ZdWt0I26VfKn7bsi60Oo/SIsWQGyC4dF5z081NvjTXwZGjIpguA0k/QqIM/GDEpCojaro=';
var key = '0123456789012345';

// Convert key and ciphertext into WordArrays
var ciphertextWA = CryptoJS.enc.Base64.parse(ciphertext);
var keyWA = CryptoJS.enc.Utf8.parse(key);

// Separate IV, HMAC and ciphertext
var ivWA = CryptoJS.lib.WordArray.create(ciphertextWA.words.slice(0, 4)); 
var hmacWA = CryptoJS.lib.WordArray.create(ciphertextWA.words.slice(4, 4 + 8)); 
var actualCiphertextWA = CryptoJS.lib.WordArray.create(ciphertextWA.words.slice(4 + 8)); 

// Authenticate
var hmacCalculatedWA = CryptoJS.HmacSHA256(actualCiphertextWA, keyWA); 
if(CryptoJS.enc.Base64.stringify(hmacCalculatedWA) === CryptoJS.enc.Base64.stringify(hmacWA)) {
  
  // Decrypt if authentication is successfull
  var decryptedMessageWA = CryptoJS.AES.decrypt({ciphertext: actualCiphertextWA}, keyWA, {iv: ivWA});
  var decryptedMessage = CryptoJS.enc.Utf8.stringify(decryptedMessageWA);
  console.log(decryptedMessage);
} else {
  console.log('Authentication failed!');
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

请注意,最好使用不同的密钥进行加密和身份验证,请参阅此处