
aes 加密方法
在 go 中实现 aes 加密时,可以采取以下步骤:
- 导入依赖库:import "crypto/aes"
- 创建 aes 密码块:cipher, _ := aes.newcipher(key)
- 为明文进行填充:明文的长度可能不是 aes 密码块大小的倍数,因此需要进行填充以满足要求。
- 执行加密:cipher.encrypt([]byte(ciphertext), []byte(plaintext))
- 对密文进行 base64 编码:ciphertext = base64.stdencoding.encodetostring([]byte(ciphertext))
下面提供了一个完整的 go 代码示例:
package main
import (
"crypto/aes"
"encoding/base64"
)
func main() {
str := []byte("406BF0AD11310101220213481000320000")
key := []byte("ER2Fb6ts3ECX")
cipher, _ := aes.NewCipher(key)
length := (len(str) + aes.BlockSize) / aes.BlockSize
plain := make([]byte, length*aes.BlockSize)
copy(plain, str)
cipherText := make([]byte, len(plain))
cipher.Encrypt(cipherText, plain)
result := base64.StdEncoding.EncodeToString(cipherText)
println(result)
}使用上述方法,可以成功对给定的明文进行 aes 加密,并将密文编码为 base64 字符串。









