审查视图

pkg/util/aes_test.go 2.0 KB
yangfu authored
1 2 3 4 5
package util

import (
	"encoding/base64"
	"fmt"
yangfu authored
6
	"github.com/forgoer/openssl"
yangfu authored
7 8 9 10 11
	"testing"
)

func Test_Aes(t *testing.T) {
	//key的长度必须是16、24或者32字节,分别用于选择AES-128, AES-192, or AES-256
yangfu authored
12
	var aeskey = []byte("mmm.qrcode.(%^&)")
yangfu authored
13
	pass := []byte("https://www.baidu.com/")
yangfu authored
14 15 16 17 18
	xpass, err := AesEncrypt(pass, aeskey)
	if err != nil {
		fmt.Println(err)
		return
	}
yangfu authored
19
	fmt.Printf("base64 加密前:%v\n", string(xpass))
yangfu authored
20
	pass64 := base64.StdEncoding.EncodeToString(xpass)
yangfu authored
21 22 23
	fmt.Printf("base64 加密:%v\n", pass64)

	//pass64 ="qYy8JqxzYGkhmDCZ6581/Fg9LfcGGqlAVjCWHpSayNs="
yangfu authored
24 25

	bytesPass, err := base64.StdEncoding.DecodeString(pass64)
yangfu authored
26
	fmt.Printf("base64 解密:%v\n", string(bytesPass))
yangfu authored
27 28 29 30 31
	if err != nil {
		fmt.Println(err)
		return
	}
yangfu authored
32 33
	//bytesPass:=xpass
	//fmt.Println(string(bytesPass))
yangfu authored
34 35 36 37 38 39 40
	tpass, err := AesDecrypt(bytesPass, aeskey)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("解密后:%s\n", tpass)
}
yangfu authored
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

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
}