You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
1.7 KiB
89 lines
1.7 KiB
package util
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/aes"
|
|
"encoding/base64"
|
|
"errors"
|
|
)
|
|
|
|
var Encrypt *Encryption
|
|
|
|
// AES 加密算法
|
|
type Encryption struct {
|
|
key string
|
|
}
|
|
|
|
func init() {
|
|
Encrypt = NewEncryption()
|
|
}
|
|
|
|
func NewEncryption() *Encryption {
|
|
return &Encryption{}
|
|
}
|
|
|
|
// 填充密码长度
|
|
func PadPwd(srcByte []byte, blockSize int) []byte {
|
|
padNum := blockSize - len(srcByte)%blockSize
|
|
ret := bytes.Repeat([]byte{byte(padNum)}, padNum)
|
|
srcByte = append(srcByte, ret...)
|
|
return srcByte
|
|
}
|
|
|
|
// 加密
|
|
func (k *Encryption) AesEncoding(src string) string {
|
|
srcByte := []byte(src)
|
|
block, err := aes.NewCipher([]byte(k.key))
|
|
if err != nil {
|
|
return src
|
|
}
|
|
// 密码填充
|
|
NewSrcByte := PadPwd(srcByte, block.BlockSize()) //由于字节长度不够,所以要进行字节的填充
|
|
dst := make([]byte, len(NewSrcByte))
|
|
block.Encrypt(dst, NewSrcByte)
|
|
// base64 编码
|
|
pwd := base64.StdEncoding.EncodeToString(dst)
|
|
return pwd
|
|
}
|
|
|
|
// 去掉填充的部分
|
|
func UnPadPwd(dst []byte) ([]byte, error) {
|
|
if len(dst) <= 0 {
|
|
return dst, errors.New("长度有误")
|
|
}
|
|
// 去掉的长度
|
|
unpadNum := int(dst[len(dst)-1])
|
|
strErr := "error"
|
|
op := []byte(strErr)
|
|
if len(dst) < unpadNum {
|
|
return op, nil
|
|
}
|
|
str := dst[:(len(dst) - unpadNum)]
|
|
return str, nil
|
|
}
|
|
|
|
// 解密
|
|
func (k *Encryption) AesDecoding(pwd string) string {
|
|
pwdByte := []byte(pwd)
|
|
pwdByte, err := base64.StdEncoding.DecodeString(pwd)
|
|
if err != nil {
|
|
return pwd
|
|
}
|
|
block, errBlock := aes.NewCipher([]byte(k.key))
|
|
if errBlock != nil {
|
|
return pwd
|
|
}
|
|
dst := make([]byte, len(pwdByte))
|
|
block.Decrypt(dst, pwdByte)
|
|
dst, err = UnPadPwd(dst) // 填充的要去掉
|
|
if err != nil {
|
|
return "0"
|
|
}
|
|
return string(dst)
|
|
}
|
|
|
|
// set方法
|
|
func (k *Encryption) SetKey(key string) {
|
|
k.key = key
|
|
}
|