1 Star 0 Fork 3

Fengzhi / gkit

forked from menuiis / gkit 
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
aes.go 2.78 KB
Copy Edit Raw Blame History
Bin authored 2023-02-02 14:05 +08:00 . feat: page_token (#13)
package aes
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"unsafe"
)
const defaultKey = "gkit"
func PadKey(s string) string {
if s == "" {
s = defaultKey
}
ps := []byte(s)
ls := len(ps)
if ls > 32 {
return string(ps[:32])
}
idx := 0
for i := ls; !(i == 16 || i == 24 || i == 32); i++ {
ps = append(ps, s[idx])
idx = (idx + 1) % ls
}
return string(ps)
}
func Encrypt(orig string, key string) string {
defer func() {
if err := recover(); err != nil {
//fmt.Println("Encrypt Err", orig, err)
return
}
}()
// 转成字节数组
origData := []byte(orig)
k := []byte(key)
// 分组秘钥
// NewCipher该函数限制了输入k的长度必须为16, 24或者32
block, _ := aes.NewCipher(k)
// 获取秘钥块的长度
blockSize := block.BlockSize()
// 补全码
origData = PKCS7Padding(origData, blockSize)
// 加密模式
blockMode := cipher.NewCBCEncrypter(block, k[:blockSize])
// 创建数组
cryted := make([]byte, len(origData))
// 加密
blockMode.CryptBlocks(cryted, origData)
return base64.StdEncoding.EncodeToString(cryted)
}
func Decrypt(cryted string, key string) string {
defer func() {
if err := recover(); err != nil {
//fmt.Println("Decrypt Err", cryted, err)
return
}
}()
// 转成字节数组
crytedByte, _ := base64.StdEncoding.DecodeString(cryted)
k := []byte(key)
// 分组秘钥
block, _ := aes.NewCipher(k)
// 获取秘钥块的长度
blockSize := block.BlockSize()
// 加密模式
blockMode := cipher.NewCBCDecrypter(block, k[:blockSize])
// 创建数组
orig := make([]byte, len(crytedByte))
// 解密
if len(crytedByte)%block.BlockSize() != 0 {
return ""
}
if len(orig) < len(crytedByte) {
return ""
}
if InexactOverlap(orig[:len(crytedByte)], crytedByte) {
return ""
}
blockMode.CryptBlocks(orig, crytedByte)
// 去补全码
orig = PKCS7UnPadding(orig)
return string(orig)
}
// PKCS7Padding 补码
// AES加密数据块分组长度必须为128bit(byte[16]),密钥长度可以是128bit(byte[16])、192bit(byte[24])、256bit(byte[32])中的任意一个。
func PKCS7Padding(ciphertext []byte, blocksize int) []byte {
padding := blocksize - len(ciphertext)%blocksize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padText...)
}
// PKCS7UnPadding 去码
func PKCS7UnPadding(origData []byte) []byte {
length := len(origData)
unPadding := int(origData[length-1])
return origData[:(length - unPadding)]
}
func InexactOverlap(x, y []byte) bool {
if len(x) == 0 || len(y) == 0 || &x[0] == &y[0] {
return false
}
return AnyOverlap(x, y)
}
func AnyOverlap(x, y []byte) bool {
return len(x) > 0 && len(y) > 0 &&
uintptr(unsafe.Pointer(&x[0])) <= uintptr(unsafe.Pointer(&y[len(y)-1])) &&
uintptr(unsafe.Pointer(&y[0])) <= uintptr(unsafe.Pointer(&x[len(x)-1]))
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/fengzhi_1/gkit.git
git@gitee.com:fengzhi_1/gkit.git
fengzhi_1
gkit
gkit
b6285053065d

Search