1 Star 0 Fork 0

xlizy/common-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
crypto.go 1.55 KB
一键复制 编辑 原始数据 按行查看 历史
xlizy 提交于 2024-04-09 15:35 . fix
package crypto
import (
"crypto/aes"
"encoding/base64"
)
// 加密
func AesEncryptECB(origDataStr, keyStr string) string {
origData := []byte(origDataStr)
key := []byte(keyStr)
cipher, _ := aes.NewCipher(generateKey(key))
length := (len(origData) + aes.BlockSize) / aes.BlockSize
plain := make([]byte, length*aes.BlockSize)
copy(plain, origData)
pad := byte(len(plain) - len(origData))
for i := len(origData); i < len(plain); i++ {
plain[i] = pad
}
encrypted := make([]byte, len(plain))
// 分组分块加密
for bs, be := 0, cipher.BlockSize(); bs <= len(origData); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
}
return base64.StdEncoding.EncodeToString(encrypted)
}
// 解密
func AesDecryptECB(encryptedStr, keyStr string) string {
defer func() {
if err := recover(); err != nil {
}
}()
encrypted, _ := base64.StdEncoding.DecodeString(encryptedStr)
key := []byte(keyStr)
cipher, _ := aes.NewCipher(generateKey(key))
decrypted := make([]byte, len(encrypted))
//
for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Decrypt(decrypted[bs:be], encrypted[bs:be])
}
trim := 0
if len(decrypted) > 0 {
trim = len(decrypted) - int(decrypted[len(decrypted)-1])
}
return string(decrypted[:trim])
}
func generateKey(key []byte) (genKey []byte) {
genKey = make([]byte, 32)
copy(genKey, key)
for i := 32; i < len(key); {
for j := 0; j < 32 && i < len(key); j, i = j+1, i+1 {
genKey[j] ^= key[i]
}
}
return genKey
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/xlizy/common-go.git
git@gitee.com:xlizy/common-go.git
xlizy
common-go
common-go
v0.2.0

搜索帮助

0d507c66 1850385 C8b1a773 1850385