2017-06-12 72 views
-3

我像这样在Node.js中加密了一个字符串。如何解密在nodejs中加密的golang中的AES256位密码?

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

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

实施例:

缓冲液:

<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以下的,缓冲器的长度是32和十六进制的长度是64.

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

golang中的aes取决于密钥的大小,当它看到一个长度为64的密钥时,会引发一个错误,说明Invalid key length

如何在golang中解密它?还有就是我目前的方案中去: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)) 

} 
+0

这是如何 “缓冲” 节点和去之间共享? – GPX

+0

@GPX不是。 cookie在nodejs中是这样加密的,我需要在golang中解密它,这样我才能读取存储在它中的数据。 –

+0

Cookie的值本质上是一个字符串。它的长度是多少? – GPX

回答

3

我解决了这个问题,帮助从@osgx

这些都是我需要改变,以正确解密的东西。

  1. 解码我使用的所有十六进制字符串。

  2. 我检查了nodejs文档,密码方法/算法使用与openssl类似的命名方案。所以,我跑这个命令openssl list-cipher-algorithms | grep "AES256",我得到了这样的输出,AES256 => AES-256-CBC这意味着,如果我在nodejs中使用aes256,它将真的在做aes-256-cbc。然后我检查了我的golang代码,并且使用aes-256-cfb这是错误的。所以,我改变了这个,并使用了一个cbc解密器。

更改这两个东西给出了正确的结果。

非常感谢你的帮助@osgx。

我更新的代码是:

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

+0

什么是您的节点js等效代码?我面临着类似的问题。您可以请分享您的节点js代码以及加密文本 – hatellla

+0

@hatellla Cookie使用[client-sessions](https://npm.im/client-sessions)npm模块加密。所有的论点都与他们的“基本用法”例子类似。 –