当前位置:网站首页>Golang des-cbc
Golang des-cbc
2022-07-01 11:53:00 【Master Guo, who sells baked cold noodles】
Golang des-cbc
package descrypt
import (
"bytes"
"crypto/cipher"
"crypto/des"
"crypto/md5"
"encoding/base64"
"encoding/hex"
"fmt"
"net/url"
"strconv"
"strings"
"time"
)
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 HexToByte(hex string) []byte {
length := len(hex) / 2
slice := make([]byte, length)
rs := []rune(hex)
for i := 0; i < length; i++ {
s := string(rs[i*2 : i*2+2])
value, _ := strconv.ParseInt(s, 16, 10)
slice[i] = byte(value & 0xFF)
}
return slice
}
func DesEncryptHash(key string, src string) string {
m := md5.New()
m.Write([]byte(src))
data := string(HexToByte(hex.EncodeToString(m.Sum(nil)))) + src
return DesEncrypt(key, []byte(data))
}
func DesEncrypt(key string, data []byte) string {
keyByte := HexToByte(key[:16])
iv := HexToByte(key[16:])
block, err := des.NewCipher(keyByte)
if err != nil {
panic(err)
}
data = PKCS5Padding(data, block.BlockSize())
mode := cipher.NewCBCEncrypter(block, iv)
out := make([]byte, len(data))
mode.CryptBlocks(out, data)
encoded := base64.StdEncoding.EncodeToString(out)
return encoded
}
func DesDecryptHash(key string, src string) string {
dec := DesDecrypt(key, src)
res := dec[16:]
m := md5.New()
m.Write([]byte(res))
mm := hex.EncodeToString(m.Sum(nil))
h := hex.EncodeToString([]byte(dec[:16]))
if mm != h {
return ""
}
return res
}
func DesDecrypt(key, src string) string {
keyByte := HexToByte(key[:16])
iv := HexToByte(key[16:])
enc, _ := base64.StdEncoding.DecodeString(src)
block, err := des.NewCipher(keyByte)
if err != nil {
panic(err)
}
mode := cipher.NewCBCDecrypter(block, iv)
plaintext := make([]byte, len(enc))
mode.CryptBlocks(plaintext, enc)
plaintext = PKCS5UnPadding(plaintext)
return string(plaintext)
}
边栏推荐
猜你喜欢
Neo4j Chinese developer monthly - issue 202206
Tianrunyun, invested by Tian Suning, was listed: its market value was 2.2 billion Hong Kong, and its first year profit decreased by 75%
ACLY与代谢性疾病
redis配置环境变量
Computer graduation project asp Net hotel room management system VS development SQLSERVER database web structure c programming computer web page source code project
Learning summary on June 30, 2022
MQ prevent message loss and repeated consumption
MQ-防止消息丢失及重复消费
Prepare for the Blue Bridge Cup Day10__ PWM control light brightness
Harbor webhook from principle to construction
随机推荐
力扣首页简介动画
Width and widthstep of iplimage
C summary of knowledge points 1
Istio、eBPF 和 RSocket Broker:深入研究服务网格
redis常识
Computer graduation project asp Net attendance management system vs developing SQLSERVER database web structure c programming computer web page source code project
sshd_config 中 PermitRootLogin 的探讨
Value/sortedset in redis
sshd_ Discussion on permitrotlogin in config
Rural guys earn from more than 2000 a month to hundreds of thousands a year. Most brick movers can walk my way ǃ
Extended tree (I) - concept and C implementation
TMUX usage
构建外部模块(Building External Modules)
GID:旷视提出全方位的检测模型知识蒸馏 | CVPR 2021
Value/string in redis
印象深刻的bug汇总(持续更新)
Understanding of MVVM and MVC
Summary of JFrame knowledge points 2
Building external modules
How to set decimal places in CAD