[英]How can I use AES encryption on top of JSON marshalling using Go?
I tried to use the following tutorial:我尝试使用以下教程:
https://golangdocs.com/aes-encryption-decryption-in-golang https://golangdocs.com/aes-encryption-decryption-in-golang
In order to encrypt/decrypt text using AES256 with Go,为了使用 AES256 和 Go 加密/解密文本,
It seems to work with plain strings, but not with JSON encoded structure.它似乎适用于纯字符串,但不适用于 JSON 编码结构。
I don't understand why, because I thought JSON encoded data were strings as well.我不明白为什么,因为我认为 JSON 编码数据也是字符串。
The part of the code dealing with plain strings is commented with Using trings
.处理纯字符串的代码部分
Using trings
进行了注释。
// Using strings
pt := "This is a secret"
c := EncryptAES([]byte(key), []byte(pt))
fmt.Printf("Initial string: %#v\n", pt)
fmt.Printf("Coded: %v\n", c)
decoded := DecryptAES([]byte(key), c)
fmt.Printf("Decoded: %s\n", decoded)
The part of the code after the comment Using JSON strings
is the part which doesn't seem to word as expected.注释
Using JSON strings
之后的代码部分似乎没有按预期措辞。
// Using JSON strings
p2 := []record{{Name: "John", Age: 20}, {Name: "Jane", Age: 25}}
m2, _ := json.Marshal(p2)
fmt.Printf("m2 = %s\n", string(m2))
fmt.Printf("m2 = %#v\n", string(m2))
coded := EncryptAES([]byte(key), m2)
decoded = DecryptAES([]byte(key), coded)
fmt.Printf("Decoded: %s\n", decoded)
What am I doing wrong?我究竟做错了什么?
I'm using Go: go version go1.18 darwin/arm64我正在使用 Go: go 版本 go1.18 darwin/arm64
package main
import (
"crypto/aes"
"encoding/json"
"fmt"
)
func CheckError(err error) {
if err != nil {
panic(err)
}
}
type record struct {
Name string `json:"first_name"`
Age int `json:"age"`
}
func main() {
// cipher key
key := "thisis32bitlongpassphraseimusing"
fmt.Printf("len of key %d\n", len(key))
// Using strings
pt := "This is a secret"
c := EncryptAES([]byte(key), []byte(pt))
fmt.Printf("Initial string: %#v\n", pt)
fmt.Printf("Coded: %v\n", c)
decoded := DecryptAES([]byte(key), c)
fmt.Printf("Decoded: %s\n", decoded)
// Using JSON strings
p2 := []record{{Name: "John", Age: 20}, {Name: "Jane", Age: 25}}
m2, _ := json.Marshal(p2)
fmt.Printf("m2 = %s\n", string(m2))
fmt.Printf("m2 = %#v\n", string(m2))
coded := EncryptAES([]byte(key), m2)
decoded = DecryptAES([]byte(key), coded)
fmt.Printf("Decoded: %s\n", decoded)
}
func EncryptAES(key []byte, plaintext []byte) []byte {
c, err := aes.NewCipher(key)
CheckError(err)
out := make([]byte, len(plaintext))
c.Encrypt(out, []byte(plaintext))
return out
}
func DecryptAES(key []byte, ct []byte) []byte {
c, err := aes.NewCipher(key)
CheckError(err)
pt := make([]byte, len(ct))
c.Decrypt(pt, ct)
return pt
}
Here is a working implementation of the encryptFile and decryptFile functions:这是 encryptFile 和 decryptFile 函数的有效实现:
(Based on: https://medium.com/@mertkimyonsen/encrypt-a-file-using-go-f1fe3bc7c635 ) (基于: https://medium.com/@mertkimyonsen/encrypt-a-file-using-go-f1fe3bc7c635 )
func encryptFile(key []byte, plainText []byte) []byte {
// Creating block of algorithm
block, err := aes.NewCipher(key)
if err != nil {
log.Fatalf("cipher err: %v", err.Error())
}
// Creating GCM mode
gcm, err := cipher.NewGCM(block)
if err != nil {
log.Fatalf("cipher GCM err: %v", err.Error())
}
// Generating random nonce
nonce := make([]byte, gcm.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
log.Fatalf("nonce err: %v", err.Error())
}
// Decrypt file
cipherText := gcm.Seal(nonce, nonce, plainText, nil)
return cipherText
}
func decryptFile(key []byte, cipherText []byte) []byte {
// Creating block of algorithm
block, err := aes.NewCipher(key)
if err != nil {
log.Fatalf("cipher err: %v", err.Error())
}
// Creating GCM mode
gcm, err := cipher.NewGCM(block)
if err != nil {
log.Fatalf("cipher GCM err: %v", err.Error())
}
// Deattached nonce and decrypt
nonce := cipherText[:gcm.NonceSize()]
cipherText = cipherText[gcm.NonceSize():]
plainText, err := gcm.Open(nil, nonce, cipherText, nil)
if err != nil {
log.Fatalf("decrypt file err: %v", err.Error())
}
return plainText
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.