使用Golang解密来自NodeJS的AES 256 CBC base64加密数据

3

以下是我在Node.js中的代码:

var crypto = require('crypto')

function encryptstring(str) {
    var cipher = crypto.createCipheriv('aes-256-cbc', 'NFd6N3v1nbL47FK0xpZjxZ7NY4fYpNYd', 'TestingIV1234567'),
        encrypted = cipher.update(str, 'utf-8', 'base64');
    encrypted += cipher.final('base64');
    return encrypted;
}

console.log(encryptstring("Testing 111111111111111111111111111111111111111111"))

这段代码返回的是:w2f0vBP2hRfgVqssqOluk68Qxkc9LXFESc0ZGzPBq3p6f/x/LbwBbg1XOoRr7I/DAtESJGdweKG6nL9m8RfewA==

下面是Go代码:

package main

import (
    "crypto/aes"
    "crypto/cipher"
    "encoding/base64"
    "fmt"
)

// decrypt from base64 to decrypted string
func decrypt(key []byte, iv []byte, cryptoText string) string {
    ciphertext, _ := base64.URLEncoding.DecodeString(cryptoText)
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }
    if len(ciphertext) < aes.BlockSize {
        panic("ciphertext too short")
    }

    ciphertext = ciphertext[aes.BlockSize:]
    stream := cipher.NewCFBDecrypter(block, iv)
    stream.XORKeyStream(ciphertext, ciphertext)

    return fmt.Sprintf("%s", ciphertext)
}

func main() {
    encKey := "NFd6N3v1nbL47FK0xpZjxZ7NY4fYpNYd"
    iv := "TestingIV1234567"
    stringtodecrypt := "w2f0vBP2hRfgVqssqOluk68Qxkc9LXFESc0ZGzPBq3p6f/x/LbwBbg1XOoRr7I/DAtESJGdweKG6nL9m8RfewA=="

    stringtodecrypt = decrypt([]byte(encKey), []byte(iv), stringtodecrypt)

    fmt.Println(string(stringtodecrypt))
}

这会返回_▒▒▒6▒▒d,O▒ob"▒

很多Go代码是从https://gist.github.com/manishtpatel/8222606中获取的。

我也尝试了这个问题: How do I decrypt an AES256 bit cipher in golang that was encrypted in nodejs?(由于在此情况下不需要hex.DecodeString ),但它会抛出一个错误,说:panic: crypto/cipher:input not full blocks

当时我使用的代码如下:

func main() {
    encKey := "NFd6N3v1nbL47FK0xpZjxZ7NY4fYpNYd"
    iv := "TestingIV1234567"
    stringtodecrypt := "w2f0vBP2hRfgVqssqOluk68Qxkc9LXFESc0ZGzPBq3p6f/x/LbwBbg1XOoRr7I/DAtESJGdweKG6nL9m8RfewA=="

    block, err := aes.NewCipher([]byte(encKey))
    if err != nil {
        panic(err)
    }

    mode := cipher.NewCBCDecrypter(block, []byte(iv))

    mode.CryptBlocks([]byte(stringtodecrypt), []byte(stringtodecrypt))

    fmt.Println(string(stringtodecrypt))
}

我已经搜索了很多,但似乎无法弄清楚这个问题。

我做错了什么?


我看到的第一件事是您尝试使用URLEncoding来解码base64,但不能成功,因为它包含/字符。您尝试过StdEncoding吗? - JimB
谢谢您的建议,我尝试了一下,但返回结果是:G▒䳾6▒▒d,O▒ob"▒▒G▒{▒▒A▒zj* ơ▒▒▒[▒▒˺ - J Del
1个回答

6
你的第二次尝试更接近正确,但是你没有先解码base64字符串。
如果你按照密码包中CBCDecrypter示例的方式进行操作,代码应该如下:
encKey := "NFd6N3v1nbL47FK0xpZjxZ7NY4fYpNYd"
iv := "TestingIV1234567"
ciphertext, err := base64.StdEncoding.DecodeString("w2f0vBP2hRfgVqssqOluk68Qxkc9LXFESc0ZGzPBq3p6f/x/LbwBbg1XOoRr7I/DAtESJGdweKG6nL9m8RfewA==")
if err != nil {
    panic(err)
}

block, err := aes.NewCipher([]byte(encKey))
if err != nil {
    panic(err)
}

if len(ciphertext)%aes.BlockSize != 0 {
    panic("ciphertext is not a multiple of the block size")
}

mode := cipher.NewCBCDecrypter(block, []byte(iv))
mode.CryptBlocks(ciphertext, ciphertext)

fmt.Printf("%s\n", ciphertext)

https://play.golang.org/p/16wV2UJ5Iw


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接