CryptoJS加密Go解密

huangapple go评论82阅读模式
英文:

CryptoJS encrypt Go decrypt

问题

Go和CryptoJS之间产生不同输出的原因可能是使用了不同的加密算法或参数配置。在你提供的代码中,Go使用了AES加密算法和CBC模式,而CryptoJS也使用了AES加密算法,但具体的模式和参数配置可能有所不同。

要确保两者产生相同的输出,需要确保以下几点:

  1. 加密算法:确保Go和CryptoJS都使用相同的AES加密算法。
  2. 模式:确保Go和CryptoJS都使用相同的加密模式,如CBC模式。
  3. 密钥和IV:确保Go和CryptoJS都使用相同的密钥和初始化向量(IV)。

请检查Go代码和CryptoJS代码中的这些方面,确保它们在加密和解密过程中使用相同的算法、模式和参数配置。这样应该能够产生相同的输出。

英文:

I have the following Go code

ciphertext := "Zff9c+F3gZu/lsARvPhpMau50KUkMAie4j8MYfb12HMWhkLqZreTk8RPbtRB7RDG3QFw7Y0FXJsCq/EBEAz//XoeSZmqZXoyq2Cx8ZV+/Rw="
decodedText, _ := base64.StdEncoding.DecodeString(ciphertext)
decodedIv, _ := base64.StdEncoding.DecodeString("u9CV7oR2w+IIk8R0hppxaw==")
newCipher, _ := aes.NewCipher([]byte("~NB8CcOL#J!H?|Yr"))
cfbdec := cipher.NewCBCDecrypter(newCipher, decodedIv)
cfbdec.CryptBlocks(decodedText, decodedText)
data, _ := base64.StdEncoding.DecodeString(string(decodedText))
println(string(data))

The output is {"data":{"value":300}, "SEQN":700 , "msg":"IT WORKS!!"

It's encrypted with the following CryptoJS

function encrypt(message, key) {
  let keyHex = CryptoJS.enc.Hex.parse(parseToHex(key))
  let iv = CryptoJS.lib.WordArray.random(128 / 8);
  let wordArray = CryptoJS.enc.Utf8.parse(message);
  let base64 = CryptoJS.enc.Base64.stringify(wordArray);
  let encrypted = CryptoJS.AES.encrypt(base64, keyHex, { iv: iv });
  return {
    cipher: encrypted.ciphertext.toString(CryptoJS.enc.Base64),
    iv: CryptoJS.enc.Base64.stringify(iv),
    length: base64.length,
    size: encrypted.ciphertext.sigBytes,
  }
}

And can be decrypted with

function decrypt(message, key, iv) {
  let ivEX = CryptoJS.enc.Hex.parse(decodeToHex(iv));
  let keyEX = CryptoJS.enc.Hex.parse(parseToHex(key));
  let bytes = CryptoJS.AES.decrypt(message, keyEX , { iv: ivEX});
  let plaintext = bytes.toString(CryptoJS.enc.Base64);
  return decodeToString(decodeToString(plaintext));
}

The output is {"data":{"value":300}, "SEQN":700 , "msg":"IT WORKS!!" } - this is the correct output

Why Go has different output?

答案1

得分: 3

请检查您的错误。始终

illegal base64 data at input byte 75

https://play.golang.org/p/dRLIT51u4I

更具体地说,第75个字节的值是5,超出了base64可用字符的范围。在ASCII中,它是ENQ(询问)字符。至于为什么它最终出现在您的base64字符串中,我不清楚。

编辑:好的,找到了问题所在。由于某种原因,base64填充字符=在末尾被解密为包含值5的5个连续字节。这是一个修复后的playground链接。https://play.golang.org/p/tf3OZ9XG1M

编辑:根据matt的评论,我更新了修复函数,简单地删除了所有的PKCS7块填充,并在最后的base64解码中使用了RawStdEncoding。现在应该是一个合理的修复。

英文:

Check your errors please. ALWAYS

illegal base64 data at input byte 75

https://play.golang.org/p/dRLIT51u4I

More specifically, the value at byte 75 is 5, which is out of the range of characters available to base64. In ascii, it is the ENQ (enquiry) character. As to why this ends up in your final base64 string is beyond me.

EDIT: OK found the issue. For whatever reason, the base64 padding character = at the end is being decrypted as 5 consecutive bytes containing the value 5. Here is a playground link that shows it fixed. https://play.golang.org/p/tf3OZ9XG1M

EDIT: As per matt's comments. I updated the fix function to simply remove all the PKCS7 block padding and use RawStdEncoding for the last base64 decode. This should now be a reasonable fix.

huangapple
  • 本文由 发表于 2017年3月30日 04:17:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/43103343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定