代码拉取完成,页面将自动刷新
package utils
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"errors"
)
// AesDecrypt Aes解密
func AesDecrypt(encrypted, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
//panic("err is:" + err.Error())
}
blockMode := newECBDecryptor(block)
origData := make([]byte, len(encrypted))
blockMode.CryptBlocks(origData, encrypted)
return origData, nil
}
// AesEncrypt Aes加密
func AesEncrypt(src, key string) ([]byte, error) {
block, err := aes.NewCipher([]byte(key))
if err != nil {
return nil, err
//panic("key error1" + err.Error())
}
if src == "" {
//panic("plain content empty")
return nil, errors.New("plain text empty")
}
ecb := newECBEncryptor(block)
content := []byte(src)
content = pkCS5Padding(content, block.BlockSize())
encrypted := make([]byte, len(content))
ecb.CryptBlocks(encrypted, content)
return encrypted, nil
}
func pkCS5Padding(ciphertext []byte, blockSize int) []byte {
if len(ciphertext)%blockSize > 0 {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{0}, padding)
return append(ciphertext, padtext...)
}
return ciphertext
}
// /////new Aes///////////
func pkCS5PaddingV2(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func pkCS5UnPaddingV2(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
// AesDecryptV2 Aes解密
func AesDecryptV2(crypted, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
//panic("err is:" + err.Error())
}
blockMode := newECBDecryptor(block)
origData := make([]byte, len(crypted))
blockMode.CryptBlocks(origData, crypted)
return pkCS5UnPaddingV2(origData), nil
}
// AesEncryptV2 Aes加密
func AesEncryptV2(src, key string) ([]byte, error) {
block, err := aes.NewCipher([]byte(key))
if err != nil {
return nil, err
//panic("key error1" + err.Error())
}
if len(src) == 0 {
return nil, errors.New("plain text empty")
//panic("plain content empty")
}
ecb := newECBEncryptor(block)
content := pkCS5PaddingV2([]byte(src), block.BlockSize())
encrypted := make([]byte, len(content))
ecb.CryptBlocks(encrypted, content)
return encrypted, nil
}
///////new Aes///////////
type ecb struct {
b cipher.Block
blockSize int
}
func newECB(b cipher.Block) *ecb {
return &ecb{
b: b,
blockSize: b.BlockSize(),
}
}
type ecbEncryptor ecb
func newECBEncryptor(b cipher.Block) cipher.BlockMode {
return (*ecbEncryptor)(newECB(b))
}
func (x *ecbEncryptor) BlockSize() int { return x.blockSize }
func (x *ecbEncryptor) CryptBlocks(dst, src []byte) {
if len(src)%x.blockSize != 0 {
//panic("crypto/cipher: input not full blocks")
return
}
if len(dst) < len(src) {
//panic("crypto/cipher: output smaller than input")
return
}
for len(src) > 0 {
x.b.Encrypt(dst, src[:x.blockSize])
src = src[x.blockSize:]
dst = dst[x.blockSize:]
}
}
type ecbDecryptor ecb
func newECBDecryptor(b cipher.Block) cipher.BlockMode {
return (*ecbDecryptor)(newECB(b))
}
func (x *ecbDecryptor) BlockSize() int { return x.blockSize }
func (x *ecbDecryptor) CryptBlocks(dst, src []byte) {
if len(src)%x.blockSize != 0 {
return
//panic("crypto/cipher: input not full blocks")
}
if len(dst) < len(src) {
return
//panic("crypto/cipher: output smaller than input")
}
for len(src) > 0 {
x.b.Decrypt(dst, src[:x.blockSize])
src = src[x.blockSize:]
dst = dst[x.blockSize:]
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。