golang中使用aes加密的过程
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li><a href="#_label0">AES 加密基础</a></li><li><a href="#_label1">生成密钥</a></li><li><a href="#_label2">加密数据</a></li><li><a href="#_label3">解密数据</a></li><li><a href="#_label4">使用 GCM 模式(推荐)</a></li><li><a href="#_label5">注意事项</a></li></ul></div><p class="maodian"><a name="_label0"></a></p><h2>AES 加密基础</h2><p>AES(Advanced Encryption Standard)是一种对称加密算法,适用于加密敏感数据。Golang 的 <code>crypto/aes</code> 包提供了 AES 加密的实现,通常结合 <code>crypto/cipher</code> 包使用。</p>
<p class="maodian"><a name="_label1"></a></p><h2>生成密钥</h2>
<p>AES 密钥长度需为 16(AES-128)、24(AES-192)或 32(AES-256)字节。</p>
<div class="jb51code"><pre class="brush:go;">key := []byte("32-byte-long-key-here-1234567890") // AES-256 密钥
</pre></div>
<p class="maodian"><a name="_label2"></a></p><h2>加密数据</h2>
<p>使用 CBC 模式(需填充)和随机 IV(初始化向量):</p>
<div class="jb51code"><pre class="brush:go;">import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"io"
)
func encrypt(plaintext []byte, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
// 填充数据到块大小
plaintext = pkcs7Pad(plaintext, aes.BlockSize)
// 生成随机 IV
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
// 加密
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(ciphertext, plaintext)
return ciphertext, nil
}
// PKCS7 填充
func pkcs7Pad(data []byte, blockSize int) []byte {
padding := blockSize - len(data)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(data, padText...)
}</pre></div>
<p class="maodian"><a name="_label3"></a></p><h2>解密数据</h2>
<p>解密时需提取 IV 并移除填充:</p>
<div class="jb51code"><pre class="brush:go;">func decrypt(ciphertext []byte, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
if len(ciphertext) < aes.BlockSize {
return nil, fmt.Errorf("ciphertext too short")
}
iv := ciphertext[:aes.BlockSize]
ciphertext = ciphertext
// 解密
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(ciphertext, ciphertext)
// 移除填充
ciphertext = pkcs7Unpad(ciphertext)
return ciphertext, nil
}
// PKCS7 去填充
func pkcs7Unpad(data []byte) []byte {
length := len(data)
unpadding := int(data)
return data[:(length - unpadding)]
}</pre></div>
<p class="maodian"><a name="_label4"></a></p><h2>使用 GCM 模式(推荐)</h2>
<p>GCM(Galois/Counter Mode)提供认证加密,无需手动填充:</p>
<div class="jb51code"><pre class="brush:go;">func encryptGCM(plaintext []byte, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
nonce := make([]byte, gcm.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return nil, err
}
return gcm.Seal(nonce, nonce, plaintext, nil), nil
}
func decryptGCM(ciphertext []byte, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
nonceSize := gcm.NonceSize()
if len(ciphertext) < nonceSize {
return nil, fmt.Errorf("ciphertext too short")
}
nonce, ciphertext := ciphertext[:nonceSize], ciphertext
return gcm.Open(nil, nonce, ciphertext, nil)
}</pre></div>
<p class="maodian"><a name="_label5"></a></p><h2>注意事项</h2>
<ul><li><strong>密钥管理</strong>:密钥需安全存储,避免硬编码。</li><li><strong>IV/Nonce</strong>:每次加密需生成随机 IV 或 Nonce,禁止重复使用。</li><li><strong>性能</strong>:GCM 模式适合高性能场景,CBC 需手动处理填充。</li></ul>
<p>通过上述方法,可在 Golang 中实现 AES 加密/解密功能,根据需求选择 CBC 或 GCM 模式。</p>
頁:
[1]