代码拉取完成,页面将自动刷新
package aes
import (
"bytes"
"crypto/aes"
"crypto/cipher"
)
// aes加密算法,CBC , pkcs7
func Encrypt(plantText, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key) //选择加密算法
if err != nil {
return nil, err
}
plantText = PKCS7Padding(plantText, block.BlockSize())
blockModel := cipher.NewCBCEncrypter(block, key)
ciphertext := make([]byte, len(plantText))
blockModel.CryptBlocks(ciphertext, plantText)
return ciphertext, nil
}
func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func Decrypt(ciphertext, key []byte) ([]byte, error) {
keyBytes := []byte(key)
block, err := aes.NewCipher(keyBytes) //选择加密算法
if err != nil {
return nil, err
}
blockModel := cipher.NewCBCDecrypter(block, keyBytes)
plantText := make([]byte, len(ciphertext))
blockModel.CryptBlocks(plantText, ciphertext)
plantText = PKCS7UnPadding(plantText, block.BlockSize())
return plantText, nil
}
func PKCS7UnPadding(plantText []byte, blockSize int) []byte {
length := len(plantText)
unpadding := int(plantText[length-1])
return plantText[:(length - unpadding)]
}
//解密字符串
func CbcDecrypt(src, key, iv []byte) (strDesc string, err error) {
aesBlockDecrypter, err := aes.NewCipher(key)
if err != nil {
return "", err
}
blockModel := cipher.NewCBCDecrypter(aesBlockDecrypter, iv)
plantText := make([]byte, len(src))
blockModel.CryptBlocks(plantText, src)
plantText = PKCS7UnPadding(plantText, aesBlockDecrypter.BlockSize())
return string(plantText), nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。