1 Star 1 Fork 0

zhuyuns / basic

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ecb.go 2.25 KB
一键复制 编辑 原始数据 按行查看 历史
wing 提交于 2021-12-18 11:42 . 添加基础包
package des
import (
"bytes"
"crypto/des"
"errors"
"fmt"
"crypto/cipher"
"encoding/base64"
)
// des ecb PKCS5
func Encrypt(data, key []byte) ([]byte, error) {
defer func() {
if x := recover(); x != nil {
fmt.Println("caught panic in ECBEncrypt()", x)
}
}()
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
bs := block.BlockSize()
data = PKCS5Padding(data, bs)
if len(data)%bs != 0 {
return nil, errors.New("Need a multiple of the blocksize")
}
out := make([]byte, len(data))
dst := out
for len(data) > 0 {
block.Encrypt(dst, data[:bs])
data = data[bs:]
dst = dst[bs:]
}
return out, nil
}
func Decrypt(data []byte, key []byte) ([]byte, error) {
defer func() {
if x := recover(); x != nil {
fmt.Println("caught panic in ECBDecrypt()", x)
}
}()
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
bs := block.BlockSize()
if len(data)%bs != 0 {
return nil, errors.New("crypto/cipher: input not full blocks")
}
out := make([]byte, len(data))
dst := out
for len(data) > 0 {
block.Decrypt(dst, data[:bs])
data = data[bs:]
dst = dst[bs:]
}
out = PKCS5UnPadding(out)
return out, nil
}
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func PKCS5UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
func ZeroPadding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{0}, padding)
return append(ciphertext, padtext...)
}
func ZeroUnPadding(origData []byte) []byte {
return bytes.TrimRightFunc(origData, func(r rune) bool {
return r == rune(0)
})
}
//DES解密 cbc
func DesDecrypt(encodeString string, key []byte) (string, error) {
//base64解密
crypted, err := base64.StdEncoding.DecodeString(encodeString)
block, err := des.NewCipher(key)
if err != nil {
return "", err
}
blockMode := cipher.NewCBCDecrypter(block, []byte{0,0,0,0,0,0,0,0})
origData := crypted
blockMode.CryptBlocks(origData, crypted)
origData = PKCS5UnPadding(origData)
return string(origData), nil
}
Go
1
https://gitee.com/zhuyuns/basic.git
git@gitee.com:zhuyuns/basic.git
zhuyuns
basic
basic
v1.0.3

搜索帮助

53164aa7 5694891 3bd8fe86 5694891