来自NodeJS的Golang解密AES 256 CBC base64 [英] Golang Decrypt AES 256 CBC base64 from NodeJS

查看:81
本文介绍了来自NodeJS的Golang解密AES 256 CBC base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在Node.js中拥有的:

This is what I have in 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中所拥有的:

This is what I have in 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

我也尝试过此操作:

I tried this as well: How do I decrypt an AES256 bit cipher in golang that was encrypted in nodejs? (with some modifications as hex.DecodeString was not necessary in this case) but it would throw an error saying 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))
}

我搜索了很多东西,但似乎无法弄清楚.

I've searched around a lot and I can't seem to figure this out.

我在做什么错了?

推荐答案

您的第二次尝试更近了,但是您没有首先解码base64字符串.

Your second attempt was closer, but you didn't decode the base64 string first.

如果您通过 CBCDecrypter 示例密码包,您将获得以下内容:

If you go by the CBCDecrypter example in the cipher package, you would have something like this:

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

这篇关于来自NodeJS的Golang解密AES 256 CBC base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆