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