当前位置:网站首页>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)
}
边栏推荐
- Comment Nike a - t - il dominé la première place toute l'année? Voici les derniers résultats financiers.
- Personnaliser le plug - in GRPC
- Dameng data rushes to the scientific innovation board: it plans to raise 2.4 billion yuan. Feng Yucai was once a professor of Huake
- Redis的攻击手法
- C#依赖注入(直白明了)讲解 一看就会系列
- Wonderful! MarkBERT
- Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)
- Is it safe for Huatai Securities to open an account online?
- On recursion and Fibonacci sequence
- 小米手机解BL锁教程
猜你喜欢

Redis的攻击手法

博途V15添加GSD文件

Summary of JFrame knowledge points 1

Emotion analysis based on IMDB comment data set

C summary of knowledge points 1

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

Deep understanding of grpc part1

强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐

ACLY与代谢性疾病

Introduction to unittest framework and the first demo
随机推荐
sshd_config 中 PermitRootLogin 的探讨
ACLY与代谢性疾病
uniapp 使用 uni-upgrade-center
研发效能度量框架解读
邻接矩阵无向图(一) - 基本概念与C语言
Learning summary on June 28, 2022
华为HMS Core携手超图为三维GIS注入新动能
Matrix of numpy
CPI tutorial - asynchronous interface creation and use
强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
妙啊!MarkBERT
redis中value/SortedSet
印象深刻的bug汇总(持续更新)
Vscode shortcut key (the most complete) [easy to understand]
MQ prevent message loss and repeated consumption
树莓派4B安装tensorflow2.0[通俗易懂]
Summary of JFrame knowledge points 2
深入理解 grpc part1
activity工作流引擎
C summary of knowledge points 3