[英]How can I use AES encryption on top of JSON marshalling using Go?
我嘗試使用以下教程:
https://golangdocs.com/aes-encryption-decryption-in-golang
為了使用 AES256 和 Go 加密/解密文本,
它似乎適用於純字符串,但不適用於 JSON 編碼結構。
我不明白為什么,因為我認為 JSON 編碼數據也是字符串。
處理純字符串的代碼部分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)
注釋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)
我究竟做錯了什么?
我正在使用 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
}
這是 encryptFile 和 decryptFile 函數的有效實現:
(基於: 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.