如何解密在nodejs中加密的golang中的AES256位密码

如何解密在nodejs中加密的golang中的AES256位密码

本文介绍了如何解密在nodejs中加密的golang中的AES256位密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Node.js中这样加密了一个字符串。

  var cipher = crypto.createCipheriv(
aes256,
<长度缓冲区32>,
79b67e539e7fcaefa7abf167de5c06ed
);

我注意到nodejs中的缓冲区就像十六进制,但每连续2个字符都是成对的。所以,如果我将它转换为十六进制,它的长度是一半。



示例:

缓冲区:

<$ p $ < code>< Buffer c3 80 36 f6 51 57 cb 6d b0 e8 fd 85 5a a2 8a da 07 4b e7 19 17 d1 c8 ee dc 2a e4 d8 5e 3c 9d a6>

十六进制:

  c38036f65157cb6db0e8fd855aa28ada074be71917d1c8eedc2ae4d85e3c9da6 

现在,我在aes256中使用的密钥长度不能超过64个。这里,Buffer的长度是32,十六进制的长度是64.



我想在golang中解密这个密码,我将不得不使用这个密钥和iv来解密它。

golang中的aes取决于键的大小,当它看到一个长度为64的键时,它会抛出一个错误,说无效密钥长度



我如何在golang中解密它?目前我的程序正在运行:


I encrypted a string in Node.js like this.

var cipher = crypto.createCipheriv(
"aes256",
"<A Buffer of length 32>",
"79b67e539e7fcaefa7abf167de5c06ed"
);

I noticed that a buffer in nodejs is like hex but every 2 consecutive characters are paired. So, It's length is half of whatever will come out if I convert it to a hex.

Example:

Buffer:

<Buffer c3 80 36 f6 51 57 cb 6d b0 e8 fd 85 5a a2 8a da 07 4b e7 19 17 d1 c8 ee dc 2a e4 d8 5e 3c 9d a6>

Hex:

c38036f65157cb6db0e8fd855aa28ada074be71917d1c8eedc2ae4d85e3c9da6

Now, The key I use in aes256 can not be of length 64. Here, Buffer's length is 32 and hex's length is 64.

I want to decrypt this cipher in golang and I'll have to use this key and iv to decrypt it.

aes in golang takes a length depending upon the size of key and when it sees a key of length 64 it throws an error that says, Invalid key length.

How do I decrypt it in golang? There is my current program in go: https://play.golang.org/p/SoXOz3XIPK

package main

import (
    "crypto/aes"
    "crypto/cipher"
    "fmt"
    "log"
)

func main() {

    encKey := "c38036f65157cb6db0e8fd855aa28ada074be71917d1c8eedc2ae4d85e3c9da6"
    iv := "79b67e539e7fcaefa7abf167de5c06ed"
    cipherText := "c02eccfc514a0b7fae830586dd56e0fcebb81fc49f41fa6dedf099c3645793bef7ec7075eca30063f9c0ef395d5ee2d44e4f3490114280abb7cf86d6eb525e2ec9bd2b781388986480f8b3df95f7b10e"

    block, err := aes.NewCipher([]byte(encKey))
    if err != nil {
        log.Fatalf("%s", err)
    }

    decrypter := cipher.NewCFBDecrypter(block, []byte(iv))

    decrypted := make([]byte, 1000)
    decrypter.XORKeyStream(decrypted, []byte(cipherText))

    fmt.Printf("%s\n", string(decrypted))

}
解决方案

I solved this problem with help from @osgx

These are the things that I needed to change to decrypt correctly.

  1. Decode all hex strings that I was using.

  2. I checked nodejs documentation and the cipher methods/algorithms use similar naming scheme as openssl. So, I ran this command openssl list-cipher-algorithms | grep "AES256"and I got an output like this, AES256 => AES-256-CBC which means that, if I am using aes256 in nodejs, It'll really be doing aes-256-cbc. Then I checked my golang code and I was using aes-256-cfb which is wrong. So, I changed that and used a cbc decrypter.

Changing these two things gives proper results.

Thank you so much for the help @osgx.

My updated code is:

package main

import (
    "crypto/aes"
    "crypto/cipher"
    "encoding/hex"
    "fmt"

)

func main() {

    encKey := "c38036f65157cb6db0e8fd855aa28ada074be71917d1c8eedc2ae4d85e3c9da6"
    iv := "79b67e539e7fcaefa7abf167de5c06ed"
    cipherText := "c02eccfc514a0b7fae830586dd56e0fcebb81fc49f41fa6dedf099c3645793bef7ec7075eca30063f9c0ef395d5ee2d44e4f3490114280abb7cf86d6eb525e2ec9bd2b781388986480f8b3df95f7b10e"

    encKeyDecoded, err := hex.DecodeString(encKey)
    if err != nil {
        panic(err)
    }
    cipherTextDecoded, err := hex.DecodeString(cipherText)
    if err != nil {
        panic(err)
    }
    ivDecoded, err := hex.DecodeString(iv)
    if err != nil {
        panic(err)
    }
    block, err := aes.NewCipher([]byte(encKeyDecoded))
    if err != nil {
        panic(err)
    }

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

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

    fmt.Println(string(cipherTextDecoded))
}

https://play.golang.org/p/Zv24WoKtBY

这篇关于如何解密在nodejs中加密的golang中的AES256位密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 20:50