gpt4 book ai didi

javascript - 加密 JS : TripleDES not encrypting properly

转载 作者:行者123 更新时间:2023-11-30 15:16:08 25 4
gpt4 key购买 nike

当使用 crypto js tripleDES 进行加密时,我注意到所有要加密的字符串都很短,例如“01”、“111”、“20”正确加密,但对于长字符串,如“5000021234567890000”,输出是' t 正确或不是我期望的值。例如对于这个“5000021234567890000”,我期待这个:“HctDaKc/U9avOwZMSS5mEi62kfojDHA4”但我得到这个:HctDaKc/U9bNnFy6eZHqHj3SemorJxQM.

这是我找到并用来加密的代码

let key = CryptoJS.MD5(enckey);
key.words[4] = key.words[0];
key.words[5] = key.words[1];
let iv = CryptoJS.lib.WordArray.create(64/8);
CryptoJS.TripleDES.encrypt("5000021234567890000", key, {iv: iv});

非常感谢您的帮助。

最佳答案

查看加密数据,第一个 block 匹配,后面两个 block 不匹配。

    HctDaKc/U9avOwZMSS5mEi62kfojDHA4    1DCB4368A73F53D6 AF3B064C492E6612 2EB691FA230C7038    HctDaKc/U9bNnFy6eZHqHj3SemorJxQM    1DCB4368A73F53D6 CD9C5CBA7991EA1E 3DD27A6A2B27140C

The IV is presumably all 0x00 bytes.

THis indicates one is using ECB m ode and the other is using CBC mode.

See Block cipher mode of operation, specifically ECB and CBC modes.

ECB mode encrypted each block independly, CBC mode xors the previous block with the data to be encrypted and for the first block the IV. Since the IV is all 0x00 values no change is made to the first block.

Options can be specified in the createion of the encryptor, you need to see the documentation page, good luck with that.

It would look something like:

encryptor = crypto.createCipheriv( mode, key, iv)

其中模式是以下之一:'des-ede'、'des-ede-cbc'、'des-ede3'、'des-ede3-cbc'、'des3'

关于javascript - 加密 JS : TripleDES not encrypting properly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44436943/

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