簡體   English   中英

在CryptoJS中的Go和解碼中編碼AES

[英]Encoding AES in Go and Decoding in CryptoJS

我在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

輸出:

e0df84c3b83681a8133e1787

我導入以下網址:

<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>

我在JS中的代碼如下:

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));

我做錯了什么?

你不想在Go代碼中使用CBCEncrypter嗎?

看起來您在Go代碼中使用CBCEncrypter (塊計數器模式)但在JS代碼中使用CryptoJS.mode.CFB (密碼反饋模式)。 據我所知,這些是不兼容的塊模式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM