代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。