1 Star 0 Fork 0

余济舟/util

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
aes.go 3.29 KB
一键复制 编辑 原始数据 按行查看 历史
余济舟 提交于 2024-08-01 09:41 . [optimize] 实现aes加盐与去盐
package symmetric
import (
"crypto/rand"
"encoding/base64"
"io"
)
type (
// Aes Aes密钥对象
Aes struct {
Err error
Encrypt *AesEncrypt
Decrypt *AesDecrypt
sailStr string
}
// AesEncrypt Aes加密密钥对象
AesEncrypt struct {
Err error
sailStr string
sailByte []byte
randKey []byte
aesKey []byte
openKey string
}
// AesDecrypt Aes解密密钥对象
AesDecrypt struct {
Err error
sailStr string
sailByte []byte
randKey []byte
aesKey []byte
openKey string
}
)
var (
AesHelper Aes
AesEncryptHelper AesEncrypt
AesDecryptHelper AesDecrypt
)
// New 实例化:Aes密钥
func (Aes) New(sail string) *Aes { return &Aes{sailStr: sail} }
// NewEncrypt 实例化:Aes加密密钥对象
func (r *Aes) NewEncrypt() *Aes {
r.Encrypt = AesEncryptHelper.New(r.sailStr)
return r
}
// NewDecrypt 实例化:Aes解密密钥对象
func (r *Aes) NewDecrypt(openKey string) *Aes {
r.Decrypt = AesDecryptHelper.New(r.sailStr, openKey)
return r
}
// GetEncrypt 获取加密密钥
func (r *Aes) GetEncrypt() *AesEncrypt { return r.Encrypt }
// GetDecrypt 获取解密密钥
func (r *Aes) GetDecrypt() *AesDecrypt { return r.Decrypt }
// New 实例化:Aes加密密钥对象
func (AesEncrypt) New(sailStr string) *AesEncrypt {
aesHelper := &AesEncrypt{
sailStr: sailStr,
sailByte: make([]byte, 16),
randKey: make([]byte, 16),
aesKey: make([]byte, 16),
openKey: "",
}
aesHelper.randKey = make([]byte, 16)
_, aesHelper.Err = io.ReadFull(rand.Reader, aesHelper.randKey)
aesHelper.sailByte, aesHelper.Err = base64.StdEncoding.DecodeString(sailStr)
return aesHelper.sailByByte()
}
// sailByByte 密码加盐:使用byte盐
func (r *AesEncrypt) sailByByte() *AesEncrypt {
copy(r.aesKey, r.randKey)
for i := 0; i < 4; i++ {
index := int(r.randKey[i]) % 16
r.aesKey[index] = r.sailByte[index]
}
r.openKey = base64.StdEncoding.EncodeToString(r.randKey)
return r
}
// GetAesKey 获取加盐后的密钥
func (r *AesEncrypt) GetAesKey() []byte {
return r.aesKey
}
// SetAesKey 设置加盐后的密钥
func (r *AesEncrypt) SetAesKey(aesKey []byte) *AesEncrypt {
r.aesKey = aesKey
return r
}
// GetOpenKey 获取公开密码
func (r *AesEncrypt) GetOpenKey() string {
return r.openKey
}
// New 实例化:Aes解密密钥对象
func (AesDecrypt) New(sailStr, openKey string) *AesDecrypt {
aesDecrypt := &AesDecrypt{
sailStr: sailStr,
sailByte: make([]byte, 16),
randKey: make([]byte, 16),
aesKey: make([]byte, 16),
openKey: openKey,
}
aesDecrypt.randKey, aesDecrypt.Err = base64.StdEncoding.DecodeString(openKey)
copy(aesDecrypt.aesKey, aesDecrypt.randKey)
aesDecrypt.sailByte, aesDecrypt.Err = base64.StdEncoding.DecodeString(sailStr)
return aesDecrypt.deSailByByte()
}
// deSailByByte 密码解盐:使用byte盐
func (r *AesDecrypt) deSailByByte() *AesDecrypt {
index := r.randKey[:4]
// 替换key中的字节
for _, x := range index {
i := int(x) % 16
r.aesKey[i] = r.sailByte[i]
}
return r
}
// GetAesKey 获取加盐后的密钥
func (r *AesDecrypt) GetAesKey() []byte {
return r.aesKey
}
// SetAesKey 设置加盐后的密钥
func (r *AesDecrypt) SetAesKey(aesKey []byte) *AesDecrypt {
r.aesKey = aesKey
return r
}
// GetOpenKey 获取公开密码
func (r *AesDecrypt) GetOpenKey() string {
return r.openKey
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jericho-yu/util.git
git@gitee.com:jericho-yu/util.git
jericho-yu
util
util
v2.2.0

搜索帮助