在Go中进行AES编码,然后在CryptoJS中进行解码。

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

Encoding AES in Go and Decoding in CryptoJS

问题

var commonIV = []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
plaintext := []byte("hello, world")
key_text := "32o4908go293hohg98fh40gh"
c, err := aes.NewCipher([]byte(key_text))
if err != nil {
fmt.Printf("Error: NewCipher(%d bytes) = %s", len(key_text), err)
return
}
cfbdec := cipher.CBCEncrypter(c, commonIV)
ciphertext := make([]byte, len(plaintext))
cfbdec.CryptBlock(ciphertext, plaintext)
fmt.Printf("%x", ciphertext) //HEX

英文:

I have these in Go:

var commonIV = []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
plaintext := []byte("hello, world")
key_text := "32o4908go293hohg98fh40gh"
c, err := aes.NewCipher([]byte(key_text))
if err != nil {
	fmt.Printf("Error: NewCipher(%d bytes) = %s", len(key_text), err)
	return
}
cfbdec := cipher.CBCEncrypter(c, commonIV)
ciphertext := make([]byte, len(plaintext))
cfbdec.CryptBlock(ciphertext, plaintext)
fmt.Printf("%x", ciphertext) //HEX

Output:

> e0df84c3b83681a8133e1787

and I import the following urls:

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/mode-cfb-min.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/components/pad-nopadding.js"></script>

and my code in JS is the following:

var data = CryptoJS.enc.Hex.parse("e0df84c3b83681a8133e1787");
console.log(data);
var key = "32o4908go293hohg98fh40gh";
var iv = CryptoJS.enc.Base64.parse("AAAAAAAAAAAAAAAAAAAAAA==");
console.log(iv);

var encrypted = {};
encrypted.key=key;
encrypted.iv=iv;
encrypted.ciphertext = data;

var dec = CryptoJS.AES.decrypt(encrypted, key, { mode: CryptoJS.mode.CFB, iv: iv,  padding: CryptoJS.pad.NoPadding  });

console.log(dec);
console.log(dec.toString());
console.log(dec.toString(CryptoJS.enc.Utf8));

What i'm doing wrong?

答案1

得分: 1

你不想在Go代码中使用CBCEncrypter吗?

英文:

Don't you want a CBCEncrypter in the Go code?

答案2

得分: 1

看起来你在Go中使用的是CBCEncrypter(块计数器模式),而在JS代码中使用的是CryptoJS.mode.CFB(密码反馈模式)。据我所知,这些不兼容的块模式。

英文:

It looks like you are using CBCEncrypter (block counter mode) in the Go but CryptoJS.mode.CFB (cypher feedback mode) in the JS code. As far as I know, these are not compatible block modes.

huangapple
  • 本文由 发表于 2013年3月1日 04:59:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/15145521.html
匿名

发表评论

匿名网友

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

确定