当前位置:网站首页>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)
}
边栏推荐
- Vscode shortcut key (the most complete) [easy to understand]
- 证券账户销户后果 开户安全吗
- Redis启动与库进入
- redis配置环境变量
- redis中value/String
- Epoll introduction
- Width and widthstep of iplimage
- Oneconnect plans to be listed in Hong Kong on July 4: a loss of nearly 3 billion in two years, with a market capitalization evaporation of more than 90%
- epoll介绍
- Botu V15 add GSD file
猜你喜欢
随机推荐
Learning summary on June 30, 2022
基于IMDB评论数据集的情感分析
Understanding of MVVM and MVC
[classic example] classic list questions @ list
CAD如何設置標注小數比特
241. Design priority for operational expressions: DFS application questions
Why must we move from Devops to bizdevops?
Is it safe for Huatai Securities to open an account online?
Redis启动与库进入
Vscode shortcut key (the most complete) [easy to understand]
JS date format conversion method
CAD如何设置标注小数位
Redis configuration environment variables
Talk about the pessimistic strategy that triggers full GC?
Use set_ Handler filters out specific SystemC wrapping & error messages
Are the consequences of securities account cancellation safe
使用set_handler过滤掉特定的SystemC Wraning &Error Message
solo 可以通过 IPV6 访问吗?
Kafuka learning path (I) Kafuka installation and simple use
对于mvvm和mvc的理解








