package licence
|
|
import (
|
"bytes"
|
"crypto/aes"
|
"crypto/cipher"
|
"crypto/md5"
|
"encoding/hex"
|
"fmt"
|
"strings"
|
)
|
|
var ivspec = []byte("0000000000000000")
|
|
func GetMd5String(s string, upper bool, half bool) string {
|
h := md5.New()
|
h.Write([]byte(s))
|
result := hex.EncodeToString(h.Sum(nil))
|
if upper == true {
|
result = strings.ToUpper(result)
|
}
|
if half == true {
|
result = result[8:24]
|
}
|
return result
|
}
|
|
func AESEncodeStr(src []byte, key string) string {
|
block, err := aes.NewCipher([]byte(key))
|
if err != nil {
|
fmt.Println("key error1", err)
|
}
|
if src == nil {
|
fmt.Println("plain content empty")
|
}
|
ecb := cipher.NewCBCEncrypter(block, ivspec)
|
content := PKCS5Padding(src, block.BlockSize())
|
crypted := make([]byte, len(content))
|
ecb.CryptBlocks(crypted, content)
|
|
return hex.EncodeToString(crypted)
|
}
|
|
func AESDecodeStr(crypt, key string) []byte {
|
crypted, err := hex.DecodeString(strings.ToLower(crypt))
|
if err != nil || len(crypted) == 0 {
|
return nil
|
}
|
|
block, err := aes.NewCipher([]byte(key))
|
if err != nil {
|
return nil
|
}
|
|
decrypted := make([]byte, len(crypted))
|
bs := block.BlockSize()
|
if len(crypted)%bs != 0 {
|
return nil
|
}
|
|
ecb := cipher.NewCBCDecrypter(block, ivspec)
|
ecb.CryptBlocks(decrypted, crypted)
|
|
return PKCS5Trimming(decrypted)
|
}
|
|
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
|
padding := blockSize - len(ciphertext)%blockSize
|
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
return append(ciphertext, padtext...)
|
}
|
|
func PKCS5Trimming(encrypt []byte) []byte {
|
length := len(encrypt)
|
number := int(encrypt[length-1])
|
if number >= length {
|
return nil
|
}
|
return encrypt[:length-number]
|
}
|