1 Star 1 Fork 0

凡卡/libp2parea

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
des.go 1005 Bytes
一键复制 编辑 原始数据 按行查看 历史
凡卡 提交于 2023-11-29 11:01 +08:00 . first commit
// Package crypto provides des 加解密
package crypto
import (
"crypto/cipher"
"crypto/des"
"errors"
)
type DESCrypter struct {
encryptBlock cipher.BlockMode
decryptBlock cipher.BlockMode
}
func newDES(key []byte) (Crypter, error) {
block, err := des.NewCipher(key[0:des.BlockSize])
if err != nil {
return nil, err
}
cpt := &AESCrypter{
encryptBlock: cipher.NewCBCEncrypter(block, []byte(key)[0:des.BlockSize]),
decryptBlock: cipher.NewCBCDecrypter(block, []byte(key)[0:des.BlockSize]),
}
return cpt, nil
}
func (cpt *DESCrypter) Encrypt(src []byte) ([]byte, error) {
src = PKCS5Padding(src, des.BlockSize)
if len(src)%des.BlockSize != 0 {
//填充错误
return nil, errors.New("Fill error")
}
dst := make([]byte, len(src))
cpt.encryptBlock.CryptBlocks(dst, src)
return dst, nil
}
func (cpt *DESCrypter) Decrypt(src []byte) ([]byte, error) {
dst := make([]byte, len(src))
cpt.decryptBlock.CryptBlocks(dst, src)
dst = PKCS5UnPadding(dst)
return dst, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/prestonTao/libp2parea.git
git@gitee.com:prestonTao/libp2parea.git
prestonTao
libp2parea
libp2parea
3aaa451ef873

搜索帮助