aes_test.go
2.0 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package util
import (
"encoding/base64"
"fmt"
"github.com/forgoer/openssl"
"testing"
)
func Test_Aes(t *testing.T) {
//key的长度必须是16、24或者32字节,分别用于选择AES-128, AES-192, or AES-256
var aeskey = []byte("mmm.qrcode.(%^&)")
pass := []byte("https://www.baidu.com/")
xpass, err := AesEncrypt(pass, aeskey)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("base64 加密前:%v\n", string(xpass))
pass64 := base64.StdEncoding.EncodeToString(xpass)
fmt.Printf("base64 加密:%v\n", pass64)
//pass64 ="qYy8JqxzYGkhmDCZ6581/Fg9LfcGGqlAVjCWHpSayNs="
bytesPass, err := base64.StdEncoding.DecodeString(pass64)
fmt.Printf("base64 解密:%v\n", string(bytesPass))
if err != nil {
fmt.Println(err)
return
}
//bytesPass:=xpass
//fmt.Println(string(bytesPass))
tpass, err := AesDecrypt(bytesPass, aeskey)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("解密后:%s\n", tpass)
}
func Test_Base64(t *testing.T) {
xpass := []byte("123") //123
fmt.Printf("base64 加密前:%v\n", string(xpass))
pass64 := base64.StdEncoding.EncodeToString(xpass)
fmt.Printf("base64 加密:%v\n", pass64)
bytesPass, err := base64.StdEncoding.DecodeString(pass64)
fmt.Printf("base64 解密:%v\n", string(bytesPass))
if err != nil {
fmt.Println(err)
return
}
}
func Test_AES_ECB(t *testing.T) {
src := []byte("123456")
key := []byte("mmm.qrcode.ecb.1")
dst, _ := openssl.AesECBEncrypt(src, key, openssl.PKCS7_PADDING)
fmt.Println(base64.StdEncoding.EncodeToString(dst)) // 1jdzWuniG6UMtoa3T6uNLA==
dst, _ = openssl.AesECBDecrypt(dst, key, openssl.PKCS7_PADDING)
fmt.Println(string(dst)) // 123456
}
func Test_AES_CBC(t *testing.T) {
src := []byte("123456")
key := []byte("mmm.qrcode.ecb.1")
iv := []byte("mmm.qrcode.ecb.1")
dst, _ := openssl.AesCBCEncrypt(src, key, iv, openssl.PKCS7_PADDING)
fmt.Println(base64.StdEncoding.EncodeToString(dst)) // 1jdzWuniG6UMtoa3T6uNLA==
dst, _ = openssl.AesCBCDecrypt(dst, key, iv, openssl.PKCS7_PADDING)
fmt.Println(string(dst)) // 123456
}