AES 128 CBC加密不起作用,而是在node.js中工作

发布于 2025-01-21 22:24:55 字数 1066 浏览 0 评论 0原文

我正在尝试移植我不久前写的一些节点代码,但由于某种原因,我无法让它开始工作。 奇怪的是,我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文