dongwaner1367 2017-09-28 17:34
浏览 155
已采纳

来自NodeJS的Golang解密AES 256 CBC base64

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"))

That returns: w2f0vBP2hRfgVqssqOluk68Qxkc9LXFESc0ZGzPBq3p6f/x/LbwBbg1XOoRr7I/DAtESJGdweKG6nL9m8RfewA==

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))
}

That ends up returning _▒▒▒6▒▒d,O▒ob"▒

A lot of the Go code was taken from 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

This was my code when I tried that:

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.

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dqsvnsad79721 2017-09-28 18:10
    关注

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

    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
    ", ciphertext)
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格