dsrbb20862 2018-12-22 09:23
浏览 760

CryptoJS AES-128-ECB和PHP openssl_encrypt不匹配

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);

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)

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

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

  • 写回答

1条回答 默认 最新

  • douchun1859 2018-12-24 16:30
    关注

    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) 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 CSS实现渐隐虚线框
  • ¥15 有没有帮写代码做实验仿真的
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真