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 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示