当前位置:网站首页>Golang des-cbc
Golang des-cbc
2022-07-01 11:53:00 【卖烤冷面的郭师傅】
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)
}
边栏推荐
- 华为HMS Core携手超图为三维GIS注入新动能
- 2022/6/28学习总结
- 耐克如何常年霸榜第一名?最新财报答案来了
- Wonderful! MarkBERT
- Brief explanation of the working principle, usage scenarios and importance of fingerprint browser
- How to set decimal places in CAD
- Redis startup and library entry
- Botu V15 add GSD file
- Istio、eBPF 和 RSocket Broker:深入研究服务网格
- Prepare for the Blue Bridge Cup Day10__ PWM control light brightness
猜你喜欢

C summary of knowledge points 1

Botu V15 add GSD file

强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
![[classic example] classic list questions @ list](/img/d8/a259e5f9d08eacbef31254d1bc3304.jpg)
[classic example] classic list questions @ list

Harbor webhook from principle to construction

Compile and debug net6 source code
![[MCU] [nixie tube] nixie tube display](/img/5e/9e14302b4e4f5e03601392ac90479d.png)
[MCU] [nixie tube] nixie tube display

邻接矩阵无向图(一) - 基本概念与C语言

CPU 上下文切换的机制和类型 (CPU Context Switch)

Neo4j 中文开发者月刊 - 202206期
随机推荐
MQ-防止消息丢失及重复消费
Personnaliser le plug - in GRPC
Neo4j 中文开发者月刊 - 202206期
Talk about the pessimistic strategy that triggers full GC?
Istio、eBPF 和 RSocket Broker:深入研究服务网格
Abbirb120 industrial robot mechanical zero position
TMUX usage
About keil compiler, "file has been changed outside the editor, reload?" Solutions for
耐克如何常年霸榜第一名?最新财报答案来了
[buuctf.reverse] 144_ [xman2018 qualifying]easyvm
Nordic nrf52832 flash download M4 error
Wonderful! MarkBERT
伸展树(一) - 概念和C实现
redis中value/String
Emotion analysis based on IMDB comment data set
Value/string in redis
Building external modules
Why must we move from Devops to bizdevops?
图的理论基础
流动性质押挖矿系统开发如何制作,dapp丨defi丨nft丨lp流动性质押挖矿系统开发案例分析及源码