AES 128 CBC加密不起作用,而是在node.js中工作
我正在尝试移植我不久前写的一些节点代码,但由于某种原因,我无法让它开始工作。 奇怪的是,我的GO代码将与某些输入(例如示例plaintext,比单个块更长,并且一些PAD
>)
时,当我放置有效载荷不起作用时,我会得到A 恐慌:加密/密码:输入不完整块
错误。
我知道我在这里肯定做错了什么,但是由于我很新来,所以我无法独自查明问题。
Node.js代码:
const crypto = require("crypto")
var AESKey = Buffer.from("Ua\x1E4\x0E[\x0FH\x13\r\x1CWdKY*").toJSON().data
var aesKeyBytes = new Uint8Array(AESKey)
let cipherIV = crypto.randomBytes(16);
let cipher = crypto.createCipheriv("aes-128-cbc", aesKeyBytes, cipherIV)
let encrypted = cipher.update(Buffer.from("this is a test for so"), "utf8", "binary")
encrypted += cipher.final("binary")
console.log(Buffer.from(encrypted).toString("base64"))
GO:
AESPrivateKey := []byte("Ua\x1E4\x0E[\x0FH\x13\r\x1CWdKY*")
plaintext := "this is a test for so"
block, _ := aes.NewCipher(AESPrivateKey)
mode := cipher.NewCBCEncrypter(block, []byte("TestingIV1234567"))
mode.CryptBlocks([]byte(plaintext), []byte(plaintext))
fmt.Println(base64.StdEncoding.EncodeToString([]byte(plaintext)))
I'm trying to port some Node.js code that I've written a while ago, and I cannot get it to work in Go for some reason.
The weird thing is that my Go code will work with some inputs (e.g. exampleplaintext that is longer than a single block and some pad
)
When I put a payload that does not work in Go, I'll get a panic: crypto/cipher: input not full blocks
error.
I know I'm definetly doing something wrong here, but since I'm fairly new with Go I'm not able to pinpoint the issue by myself.
Node.js Code:
const crypto = require("crypto")
var AESKey = Buffer.from("Ua\x1E4\x0E[\x0FH\x13\r\x1CWdKY*").toJSON().data
var aesKeyBytes = new Uint8Array(AESKey)
let cipherIV = crypto.randomBytes(16);
let cipher = crypto.createCipheriv("aes-128-cbc", aesKeyBytes, cipherIV)
let encrypted = cipher.update(Buffer.from("this is a test for so"), "utf8", "binary")
encrypted += cipher.final("binary")
console.log(Buffer.from(encrypted).toString("base64"))
Go:
AESPrivateKey := []byte("Ua\x1E4\x0E[\x0FH\x13\r\x1CWdKY*")
plaintext := "this is a test for so"
block, _ := aes.NewCipher(AESPrivateKey)
mode := cipher.NewCBCEncrypter(block, []byte("TestingIV1234567"))
mode.CryptBlocks([]byte(plaintext), []byte(plaintext))
fmt.Println(base64.StdEncoding.EncodeToString([]byte(plaintext)))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论