完善不同加密方法的使用
This commit is contained in:
+150
-63
@@ -1,94 +1,181 @@
|
||||
package tdesx
|
||||
package tdesx_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"code.yun.ink/pkg/encryptx/cipherx"
|
||||
"code.yun.ink/pkg/encryptx/tdesx"
|
||||
)
|
||||
|
||||
func TestEncryptECBHex(t *testing.T) {
|
||||
// 3DES密钥需要24字节
|
||||
key := "123456789012345678901234"
|
||||
input := "hello world"
|
||||
|
||||
encrypted, err := EncryptECBHex(input, key)
|
||||
func TestGenerateKey(t *testing.T) {
|
||||
key, err := tdesx.GenerateKey()
|
||||
if err != nil {
|
||||
t.Errorf("加密失败: %v", err)
|
||||
return
|
||||
t.Fatalf("GenerateKey 失败: %v", err)
|
||||
}
|
||||
if len(key) != tdesx.KeySize {
|
||||
t.Fatalf("密钥长度 = %d, 期望 %d", len(key), tdesx.KeySize)
|
||||
}
|
||||
|
||||
decrypted, err := DecryptECBHex(encrypted, key)
|
||||
if err != nil {
|
||||
t.Errorf("解密失败: %v", err)
|
||||
return
|
||||
other, _ := tdesx.GenerateKey()
|
||||
if bytes.Equal(key, other) {
|
||||
t.Error("两次 GenerateKey 产生了相同密钥")
|
||||
}
|
||||
|
||||
if decrypted != input {
|
||||
t.Errorf("解密结果不匹配,期望: %s, 实际: %s", input, decrypted)
|
||||
hexKey, err := tdesx.GenerateKeyHex()
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateKeyHex 失败: %v", err)
|
||||
}
|
||||
if len(hexKey) != tdesx.KeySize*2 {
|
||||
t.Errorf("GenerateKeyHex 长度 = %d, 期望 %d", len(hexKey), tdesx.KeySize*2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptCBCHex(t *testing.T) {
|
||||
// 3DES密钥需要24字节
|
||||
key := "123456789012345678901234"
|
||||
input := "hello world"
|
||||
|
||||
encrypted, err := EncryptCBCHex(input, key)
|
||||
if err != nil {
|
||||
t.Errorf("CBC加密失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
decrypted, err := DecryptCBCHex(encrypted, key)
|
||||
if err != nil {
|
||||
t.Errorf("CBC解密失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if decrypted != input {
|
||||
t.Errorf("CBC解密结果不匹配,期望: %s, 实际: %s", input, decrypted)
|
||||
func TestKeySizeValidation(t *testing.T) {
|
||||
plaintext := []byte("hello")
|
||||
for _, wrongLen := range []int{0, 8, 16, 23, 25, 32} {
|
||||
key := bytes.Repeat([]byte{0x01}, wrongLen)
|
||||
if _, err := tdesx.EncryptCBC(plaintext, key); !errors.Is(err, cipherx.ErrInvalidKeySize) {
|
||||
t.Errorf("密钥长度 %d: 期望 ErrInvalidKeySize, 得到 %v", wrongLen, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptCFBHex(t *testing.T) {
|
||||
// 3DES密钥需要24字节
|
||||
key := "123456789012345678901234"
|
||||
input := "hello world"
|
||||
|
||||
encrypted, err := EncryptCFBHex(input, key)
|
||||
func TestAllModesRoundTrip(t *testing.T) {
|
||||
key, err := tdesx.GenerateKey()
|
||||
if err != nil {
|
||||
t.Errorf("CFB加密失败: %v", err)
|
||||
return
|
||||
t.Fatalf("GenerateKey 失败: %v", err)
|
||||
}
|
||||
|
||||
decrypted, err := DecryptCFBHex(encrypted, key)
|
||||
if err != nil {
|
||||
t.Errorf("CFB解密失败: %v", err)
|
||||
return
|
||||
encryptors := map[string]func([]byte, []byte) ([]byte, error){
|
||||
"ECB": tdesx.EncryptECB, "CBC": tdesx.EncryptCBC, "CFB": tdesx.EncryptCFB,
|
||||
"OFB": tdesx.EncryptOFB, "CTR": tdesx.EncryptCTR,
|
||||
}
|
||||
decryptors := map[string]func([]byte, []byte) ([]byte, error){
|
||||
"ECB": tdesx.DecryptECB, "CBC": tdesx.DecryptCBC, "CFB": tdesx.DecryptCFB,
|
||||
"OFB": tdesx.DecryptOFB, "CTR": tdesx.DecryptCTR,
|
||||
}
|
||||
plaintexts := [][]byte{
|
||||
{}, []byte("a"), []byte("1234567"), []byte("12345678"), []byte("123456789"),
|
||||
bytes.Repeat([]byte("3DES"), 200),
|
||||
}
|
||||
|
||||
if decrypted != input {
|
||||
t.Errorf("CFB解密结果不匹配,期望: %s, 实际: %s", input, decrypted)
|
||||
for name, encrypt := range encryptors {
|
||||
for i, plaintext := range plaintexts {
|
||||
ciphertext, err := encrypt(plaintext, key)
|
||||
if err != nil {
|
||||
t.Fatalf("%s 明文#%d 加密失败: %v", name, i, err)
|
||||
}
|
||||
decrypted, err := decryptors[name](ciphertext, key)
|
||||
if err != nil {
|
||||
t.Fatalf("%s 明文#%d 解密失败: %v", name, i, err)
|
||||
}
|
||||
if !bytes.Equal(decrypted, plaintext) {
|
||||
t.Errorf("%s 明文#%d 往返不一致", name, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPKCS7Padding(t *testing.T) {
|
||||
// 测试PKCS7填充和去除
|
||||
data := []byte("hello")
|
||||
blockSize := 8
|
||||
func TestHexAPIRoundTrip(t *testing.T) {
|
||||
key, _ := tdesx.GenerateKey()
|
||||
plaintext := []byte("十六进制往返")
|
||||
|
||||
// 填充
|
||||
padded := PKCS7Padding(data, blockSize)
|
||||
if len(padded) != blockSize {
|
||||
t.Errorf("填充长度错误,期望: %d, 实际: %d", blockSize, len(padded))
|
||||
cases := []struct {
|
||||
name string
|
||||
encrypt func([]byte, []byte) (string, error)
|
||||
decrypt func(string, []byte) ([]byte, error)
|
||||
}{
|
||||
{"ECB", tdesx.EncryptECBHex, tdesx.DecryptECBHex},
|
||||
{"CBC", tdesx.EncryptCBCHex, tdesx.DecryptCBCHex},
|
||||
{"CFB", tdesx.EncryptCFBHex, tdesx.DecryptCFBHex},
|
||||
{"OFB", tdesx.EncryptOFBHex, tdesx.DecryptOFBHex},
|
||||
{"CTR", tdesx.EncryptCTRHex, tdesx.DecryptCTRHex},
|
||||
}
|
||||
for _, c := range cases {
|
||||
hexStr, err := c.encrypt(plaintext, key)
|
||||
if err != nil {
|
||||
t.Fatalf("%s Hex 加密失败: %v", c.name, err)
|
||||
}
|
||||
if bytes.ContainsAny([]byte(hexStr), "\n\r \t") {
|
||||
t.Errorf("%s Hex 输出含空白字符", c.name)
|
||||
}
|
||||
decrypted, err := c.decrypt(hexStr, key)
|
||||
if err != nil {
|
||||
t.Fatalf("%s Hex 解密失败: %v", c.name, err)
|
||||
}
|
||||
if !bytes.Equal(decrypted, plaintext) {
|
||||
t.Errorf("%s Hex 往返不一致", c.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithIVRoundTrip(t *testing.T) {
|
||||
key, _ := tdesx.GenerateKey()
|
||||
iv := bytes.Repeat([]byte{0x5A}, tdesx.BlockSize)
|
||||
plaintext := []byte("fixed iv")
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
encrypt func([]byte, []byte, []byte) ([]byte, error)
|
||||
decrypt func([]byte, []byte, []byte) ([]byte, error)
|
||||
}{
|
||||
{"CBC", tdesx.EncryptCBCWithIV, tdesx.DecryptCBCWithIV},
|
||||
{"CFB", tdesx.EncryptCFBWithIV, tdesx.DecryptCFBWithIV},
|
||||
{"OFB", tdesx.EncryptOFBWithIV, tdesx.DecryptOFBWithIV},
|
||||
{"CTR", tdesx.EncryptCTRWithIV, tdesx.DecryptCTRWithIV},
|
||||
}
|
||||
for _, c := range cases {
|
||||
ciphertext, err := c.encrypt(plaintext, key, iv)
|
||||
if err != nil {
|
||||
t.Fatalf("%s WithIV 加密失败: %v", c.name, err)
|
||||
}
|
||||
decrypted, err := c.decrypt(ciphertext, key, iv)
|
||||
if err != nil {
|
||||
t.Fatalf("%s WithIV 解密失败: %v", c.name, err)
|
||||
}
|
||||
if !bytes.Equal(decrypted, plaintext) {
|
||||
t.Errorf("%s WithIV 往返不一致", c.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestMalformedCiphertextNeverPanics 是 P0-3 在 3DES 上的回归测试。
|
||||
func TestMalformedCiphertextNeverPanics(t *testing.T) {
|
||||
key, _ := tdesx.GenerateKey()
|
||||
decryptors := map[string]func([]byte, []byte) ([]byte, error){
|
||||
"ECB": tdesx.DecryptECB, "CBC": tdesx.DecryptCBC, "CFB": tdesx.DecryptCFB,
|
||||
"OFB": tdesx.DecryptOFB, "CTR": tdesx.DecryptCTR,
|
||||
}
|
||||
for _, decrypt := range decryptors {
|
||||
for n := 0; n <= 24; n++ {
|
||||
_, _ = decrypt(bytes.Repeat([]byte{0x7F}, n), key)
|
||||
}
|
||||
}
|
||||
|
||||
nonAligned := bytes.Repeat([]byte{0xAB}, 12)
|
||||
if _, err := tdesx.DecryptECB(nonAligned, key); !errors.Is(err, cipherx.ErrNotBlockAligned) {
|
||||
t.Errorf("ECB 非对齐: 期望 ErrNotBlockAligned, 得到 %v", err)
|
||||
}
|
||||
if _, err := tdesx.DecryptCBC(nonAligned, key); !errors.Is(err, cipherx.ErrNotBlockAligned) {
|
||||
t.Errorf("CBC 非对齐: 期望 ErrNotBlockAligned, 得到 %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestECBIsDeterministic(t *testing.T) {
|
||||
key, _ := tdesx.GenerateKey()
|
||||
plaintext := bytes.Repeat([]byte("12345678"), 3)
|
||||
|
||||
// 去除填充
|
||||
unpadded, err := PKCS7UnPadding(padded)
|
||||
if err != nil {
|
||||
t.Errorf("去除填充失败: %v", err)
|
||||
return
|
||||
a, _ := tdesx.EncryptECB(plaintext, key)
|
||||
b, _ := tdesx.EncryptECB(plaintext, key)
|
||||
if !bytes.Equal(a, b) {
|
||||
t.Error("ECB 应当是确定性的")
|
||||
}
|
||||
|
||||
if string(unpadded) != string(data) {
|
||||
t.Errorf("去除填充后数据不匹配,期望: %s, 实际: %s", string(data), string(unpadded))
|
||||
c, _ := tdesx.EncryptCBC(plaintext, key)
|
||||
d, _ := tdesx.EncryptCBC(plaintext, key)
|
||||
if bytes.Equal(c, d) {
|
||||
t.Error("CBC 使用了随机 IV,两次结果不应相同")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user