aes_test.go 2.0 KB
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
}