117 lines
2.8 KiB
Go
117 lines
2.8 KiB
Go
package aesx_test
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"code.yun.ink/pkg/encryptx/aesx"
|
|
)
|
|
|
|
func TestGCM(t *testing.T) {
|
|
key := "example key 1234"
|
|
plaintext := "exampleplaintext"
|
|
|
|
// aesx.AesEncrypt(plaintext,key)
|
|
// aesx.AesDecrypt()
|
|
|
|
ciphertext, err := aesx.AESCBCEncrypt(key, plaintext)
|
|
fmt.Println(ciphertext, err)
|
|
|
|
plaintext, err = aesx.AESCBCDecrypter(key, ciphertext)
|
|
fmt.Println(plaintext, err)
|
|
///GCM
|
|
noncetext := ""
|
|
ciphertext, noncetext, err = aesx.EncryptGCM(key, plaintext)
|
|
fmt.Println(ciphertext, err)
|
|
|
|
plaintext, err = aesx.DecryptGCM(key, ciphertext, noncetext)
|
|
fmt.Println(plaintext, err)
|
|
}
|
|
|
|
func TestECB(t *testing.T) {
|
|
key := "example key 1234"
|
|
plaintext := "exampleplaintext"
|
|
|
|
// 测试ECB加密和解密
|
|
encrypted, err := aesx.EncryptECBHex(plaintext, key)
|
|
if err != nil {
|
|
t.Fatalf("ECB加密失败: %v", err)
|
|
}
|
|
fmt.Printf("ECB加密结果: %s\n", encrypted)
|
|
|
|
decrypted, err := aesx.DecryptECBHex(encrypted, key)
|
|
if err != nil {
|
|
t.Fatalf("ECB解密失败: %v", err)
|
|
}
|
|
fmt.Printf("ECB解密结果: %s\n", decrypted)
|
|
|
|
if decrypted != plaintext {
|
|
t.Fatalf("ECB解密结果不匹配,原文: %s, 解密结果: %s", plaintext, decrypted)
|
|
}
|
|
}
|
|
|
|
func TestCBC(t *testing.T) {
|
|
key := "example key 1234"
|
|
plaintext := "exampleplaintext"
|
|
|
|
// 测试CBC加密和解密
|
|
encrypted, err := aesx.EncryptCBCHex(plaintext, key)
|
|
if err != nil {
|
|
t.Fatalf("CBC加密失败: %v", err)
|
|
}
|
|
fmt.Printf("CBC加密结果: %s\n", encrypted)
|
|
|
|
decrypted, err := aesx.DecryptCBCHex(encrypted, key)
|
|
if err != nil {
|
|
t.Fatalf("CBC解密失败: %v", err)
|
|
}
|
|
fmt.Printf("CBC解密结果: %s\n", decrypted)
|
|
|
|
if decrypted != plaintext {
|
|
t.Fatalf("CBC解密结果不匹配,原文: %s, 解密结果: %s", plaintext, decrypted)
|
|
}
|
|
}
|
|
|
|
func TestCFB(t *testing.T) {
|
|
key := "example key 1234"
|
|
plaintext := "exampleplaintext"
|
|
|
|
// 测试CFB加密和解密
|
|
encrypted, err := aesx.EncryptCFBHex(plaintext, key)
|
|
if err != nil {
|
|
t.Fatalf("CFB加密失败: %v", err)
|
|
}
|
|
fmt.Printf("CFB加密结果: %s\n", encrypted)
|
|
|
|
decrypted, err := aesx.DecryptCFBHex(encrypted, key)
|
|
if err != nil {
|
|
t.Fatalf("CFB解密失败: %v", err)
|
|
}
|
|
fmt.Printf("CFB解密结果: %s\n", decrypted)
|
|
|
|
if decrypted != plaintext {
|
|
t.Fatalf("CFB解密结果不匹配,原文: %s, 解密结果: %s", plaintext, decrypted)
|
|
}
|
|
}
|
|
|
|
func TestOFB(t *testing.T) {
|
|
key := "example key 1234"
|
|
plaintext := "exampleplaintext"
|
|
|
|
// 测试OFB加密和解密
|
|
encrypted, err := aesx.EncryptOFBHex(plaintext, key)
|
|
if err != nil {
|
|
t.Fatalf("OFB加密失败: %v", err)
|
|
}
|
|
fmt.Printf("OFB加密结果: %s\n", encrypted)
|
|
|
|
decrypted, err := aesx.DecryptOFBHex(encrypted, key)
|
|
if err != nil {
|
|
t.Fatalf("OFB解密失败: %v", err)
|
|
}
|
|
fmt.Printf("OFB解密结果: %s\n", decrypted)
|
|
|
|
if decrypted != plaintext {
|
|
t.Fatalf("OFB解密结果不匹配,原文: %s, 解密结果: %s", plaintext, decrypted)
|
|
}
|
|
} |