J D*_*Del 3 encryption go node.js
这是我在 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"))
Run Code Online (Sandbox Code Playgroud)
那返回: 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))
}
Run Code Online (Sandbox Code Playgroud)
最终返回 _???6??d,O?ob"?
很多 Go 代码摘自https://gist.github.com/manishtpatel/8222606
我也试过这个:如何在 golang 中解密在 nodejs 中加密的 AES256 位密码?(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))
}
Run Code Online (Sandbox Code Playgroud)
我已经搜索了很多,我似乎无法弄清楚这一点。
我究竟做错了什么?
您的第二次尝试更接近,但您没有先解码 base64 字符串。
如果您按照 cipher 包中的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)
Run Code Online (Sandbox Code Playgroud)
https://play.golang.org/p/16wV2UJ5Iw
| 归档时间: |
|
| 查看次数: |
2635 次 |
| 最近记录: |