当前位置:网站首页>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)
}
边栏推荐
- Talk about the pessimistic strategy that triggers full GC?
- Dameng data rushes to the scientific innovation board: it plans to raise 2.4 billion yuan. Feng Yucai was once a professor of Huake
- 今天开户今天能买股票吗?在线开户是很安全么?
- Adjacency matrix undirected graph (I) - basic concepts and C language
- 伸展树(一) - 概念和C实现
- [Maui] add click events for label, image and other controls
- JS date format conversion method
- Are the consequences of securities account cancellation safe
- Deep understanding of grpc part1
- GID:旷视提出全方位的检测模型知识蒸馏 | CVPR 2021
猜你喜欢
S7-1500plc simulation
Wonderful! MarkBERT
深入理解 grpc part1
Exploration and practice of inress in kubernetes
Botu V15 add GSD file
Istio, ebpf and rsocket Broker: in depth study of service grid
Harbor webhook from principle to construction
CPI tutorial - asynchronous interface creation and use
Chen Gong: Micro service, is it still so pure?
ACLY与代谢性疾病
随机推荐
Summary of JFrame knowledge points 1
Introduction to unittest framework and the first demo
Kernel synchronization mechanism
[Maui] add click events for label, image and other controls
力扣首页简介动画
epoll介绍
Share the method of how to preview PSD format and PSD file thumbnail plug-in [easy to understand]
Redis startup and library entry
Neo4j 中文开发者月刊 - 202206期
Abbirb120 industrial robot mechanical zero position
Botu V15 add GSD file
2022/6/29学习总结
241. Design priority for operational expressions: DFS application questions
我在中山,到哪里开户比较好?实际上网上开户安全么?
研发效能度量框架解读
Theoretical basis of graph
今天开户今天能买股票吗?在线开户是很安全么?
博途V15添加GSD文件
Chen Gong: Micro service, is it still so pure?
ES6 promise Usage Summary