doubishi8303 2013-02-28 20:59
浏览 71
已采纳

在Go中编码AES和在CryptoJS中解码

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?

  • 写回答

2条回答 默认 最新

  • dongxian3574 2013-03-02 02:46
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算