CryptoJS加密Go解密 [英] CryptoJS encrypt Go decrypt

查看:2376
本文介绍了CryptoJS加密Go解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

输出为 {data:{value:300},SEQN:700,msg:IT WORKS !!

使用以下CryptoJS进行加密

 函数加密(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。长度,
大小:encrypted.ciphertext.sigBytes,
}
}

可以用

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

输出为 {data:{值:300},SEQN:700,msg:IT WORKS !! } - 这是正确的输出



为什么Go有不同的输出?

解决方案

请检查你的错误。总是



输入字节75处的非法base64数据



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



更具体地说,字节75中的值是 5 ,它超出了base64可用字符的范围。在ascii中,它是ENQ(查询)字符。至于为什么这最终在您的最终base64字符串超出了我。



编辑:确定发现问题。无论什么原因,最后的base64填充字符 = 被解密为包含值 5 的5个连续字节。这是一个操场链接,显示它是固定的。 https://play.golang.org/p/tf3OZ9XG1M



编辑:按照matt的评论。我更新了修复功能,以便删除所有PKCS7块填充,并使用 RawStdEncoding 来进行最后的base64解码。这应该是一个合理的解决方案。


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?

解决方案

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.

这篇关于CryptoJS加密Go解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆