本文介绍了CryptoJS AES-128-ECB和PHP openssl_encrypt不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP上有一些代码,这些代码无法编辑,并且数据库中充满了以这种方式加密的消息。

I have some code on PHP, which can't be edited and a database full of encrypted this way messages.

$key = '297796CCB81D2553B07B379D78D87618'
return $encrypted = openssl_encrypt($data, 'AES-128-ECB', $key);

我必须编写一些JS代码来加密和解密这些消息。我为此使用CryptoJS。

I have to write some JS code to encrypt and decrypt these messages. I'm using CryptoJS for this purpose.

const key = '297796CCB81D2553B07B379D78D87618'
let k = CryptoJS.enc.Base64.parse(key)
let cypher = CryptoJS.AES.encrypt(this.text, k, {mode: CryptoJS.mode.ECB})
this.cypher = CryptoJS.enc.Base64.stringify(cypher.ciphertext)

我无法获得这些代码来产生相同的结果。对于'test'字符串,我得到以下结果: JS:H1AG6j / i / iSqifSNCG5JKw == PHP:Nqrur4UMEicEMwJC39qq0A ==

I can not get these codes to produce the same results. For 'test' string i got following results: JS: H1AG6j/i/iSqifSNCG5JKw==, PHP: Nqrur4UMEicEMwJC39qq0A==

我正在尝试3天,但是我找不到问题。
我唯一可以编辑的代码是JS。

I'm trying to work this out for 3 days, but I cannot find the issue.The only code I can edit is JS.

推荐答案

我知道是什么问题。 PHP中的
openssl_encrypt将密钥作为UTF-8字符串。经过足够的长度后,它会忽略下一个字符,因此我们的键 297796CCB81D2553B07B379D78D87618会修剪为 297796CCB81D2553。
此代码有效:

I figured out what problem is.openssl_encrypt in PHP takes the key as a UTF-8 string. After taking enough length, it ignores next characters, so our key: '297796CCB81D2553B07B379D78D87618' trims to '297796CCB81D2553'.This code is working:

// JS
const key = '297796CCB81D2553'
let k = CryptoJS.enc.Utf8.parse(key)
let cypher = CryptoJS.AES.encrypt(this.text, k, {mode: CryptoJS.mode.ECB})
this.cypher = CryptoJS.enc.Base64.stringify(cypher.ciphertext)

这篇关于CryptoJS AES-128-ECB和PHP openssl_encrypt不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 09:21