1 Star 0 Fork 0

yzsunjianguo/sponge

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
des.go 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
yzsunjianguo 提交于 2024-02-08 14:53 +08:00 . init
package gocrypto
// 对称加密DES,是目前最为流行的加密算法之一,逐渐被AES替换。
import (
"encoding/hex"
"gitee.com/yzsunjianguo/sponge/pkg/gocrypto/comCipher"
)
// DesEncrypt des加密byte,返回的密文未经过转码
func DesEncrypt(rawData []byte, opts ...DesOption) ([]byte, error) {
o := defaultDesOptions()
o.apply(opts...)
return desEncrypt(o.mode, rawData, o.desKey)
}
// DesDecrypt des解密byte,参数输入未经过转码的密文
func DesDecrypt(cipherData []byte, opts ...DesOption) ([]byte, error) {
o := defaultDesOptions()
o.apply(opts...)
return desDecrypt(o.mode, cipherData, o.desKey)
}
// DesEncryptHex des加密string,返回的密文已经转码
func DesEncryptHex(rawData string, opts ...DesOption) (string, error) {
o := defaultDesOptions()
o.apply(opts...)
cipherData, err := desEncrypt(o.mode, []byte(rawData), o.desKey)
if err != nil {
return "", err
}
return hex.EncodeToString(cipherData), nil
}
// DesDecryptHex des解密string,参数输入已经转码的密文字符串
func DesDecryptHex(cipherStr string, opts ...DesOption) (string, error) {
o := defaultDesOptions()
o.apply(opts...)
cipherData, err := hex.DecodeString(cipherStr)
if err != nil {
return "", err
}
rawData, err := desDecrypt(o.mode, cipherData, o.desKey)
if err != nil {
return "", err
}
return string(rawData), nil
}
func desEncrypt(mode string, rawData []byte, key []byte) ([]byte, error) {
cipherMode, err := getCipherMode(mode)
if err != nil {
return nil, err
}
cip, err := comCipher.NewDESWith(key, cipherMode)
if err != nil {
return nil, err
}
return cip.Encrypt(rawData), nil
}
func desDecrypt(mode string, cipherData []byte, key []byte) ([]byte, error) {
cipherMode, err := getCipherMode(mode)
if err != nil {
return nil, err
}
cip, err := comCipher.NewDESWith(key, cipherMode)
if err != nil {
return nil, err
}
return cip.Decrypt(cipherData), nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/yzsunjianguo/sponge.git
git@gitee.com:yzsunjianguo/sponge.git
yzsunjianguo
sponge
sponge
v1.0.3

搜索帮助