Go语言中可以使用标准库中的crypto包进行加解密操作。具体实现方法如下:
- 导入crypto包和相关子包:
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"io"
)
- 定义用于加解密的key和iv(初始化向量):
var key = []byte("0123456789ABCDEF") // 16位或32位的key
var iv = []byte("ABCDEF0123456789") // 16位iv
- 定义加密函数encrypt:
func encrypt(plaintext string) (string, error) {
plaintextBytes := []byte(plaintext)
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
ciphertext := make([]byte, aes.BlockSize+len(plaintextBytes))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return "", err
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintextBytes)
return hex.EncodeToString(ciphertext), nil
}
- 定义解密函数decrypt:
func decrypt(ciphertext string) (string, error) {
ciphertextBytes, err := hex.DecodeString(ciphertext)
if err != nil {
return "", err
}
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
if len(ciphertextBytes) < aes.BlockSize {
return "", errors.New("ciphertext too short")
}
iv := ciphertextBytes[:aes.BlockSize]
ciphertextBytes = ciphertextBytes[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(ciphertextBytes, ciphertextBytes)
return string(ciphertextBytes), nil
}
- 测试加解密函数:
func main() {
plaintext := "Hello World!"
ciphertext, err := encrypt(plaintext)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("ciphertext:", ciphertext)
decryptedText, err := decrypt(ciphertext)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("decrypted text:", decryptedText)
}
完整的示例代码如下:
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"io"
)
var key = []byte("0123456789ABCDEF") // 16位或32位的key
var iv = []byte("ABCDEF0123456789") // 16位iv
func encrypt(plaintext string) (string, error) {
plaintextBytes := []byte(plaintext)
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
ciphertext := make([]byte, aes.BlockSize+len(plaintextBytes))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return "", err
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintextBytes)
return hex.EncodeToString(ciphertext), nil
}
func decrypt(ciphertext string) (string, error) {
ciphertextBytes, err := hex.DecodeString(ciphertext)
if err != nil {
return "", err
}
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
if len(ciphertextBytes) < aes.BlockSize {
return "", errors.New("ciphertext too short")
}
iv := ciphertextBytes[:aes.BlockSize]
ciphertextBytes = ciphertextBytes[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(ciphertextBytes, ciphertextBytes)
return string(ciphertextBytes), nil
}
func main() {
plaintext := "Hello World!"
ciphertext, err := encrypt(plaintext)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("ciphertext:", ciphertext)
decryptedText, err := decrypt(ciphertext)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("decrypted text:", decryptedText)
}




