-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleCrypto.go
39 lines (35 loc) · 997 Bytes
/
simpleCrypto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* @FileName:
* @Author: xjj
* @CreateTime: 下午6:23
* @Description:
*/
package simpleCrypto
import (
"bytes"
"crypto/aes"
"crypto/md5"
"encoding/base64"
"encoding/hex"
)
//Md5Enscrypto md5加密
func Md5Enscrypto(data string) string {
m := md5.New()
m.Write([]byte(data))
return hex.EncodeToString(m.Sum(nil))
}
//SimpleEncryptAesECB ECB加密,并对加密结果进行了base64编码
func SimpleEncryptAesECB(aesText []byte, aesKey []byte) string {
c, _ := aes.NewCipher(aesKey)
encrypter := NewECBEncrypter(c)
aesText = PKCS5Padding(aesText, c.BlockSize())
d := make([]byte, len(aesText))
encrypter.CryptBlocks(d, aesText)
return base64.StdEncoding.EncodeToString(d)
}
//PKCS5Padding PKCS5明文填充方案,此方案使明文保持为块长度的倍数
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}