f
This commit is contained in:
@@ -61,6 +61,90 @@ func TestAesEcbHexEncryption(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// 需要解密的身份证密文(可替换为其他密文进行测试)
|
||||
const encryptedIDCardForTest = "z4pIrjlrpfsBtF74kvtRWnjM2UTx8Uq8HVA8dk9hSeI="
|
||||
|
||||
// TestDecryptIDCard 身份证解密单元测试(已知密文)
|
||||
func TestDecryptIDCard(t *testing.T) {
|
||||
fmt.Printf("encryptedIDC开始解密")
|
||||
key, _ := hex.DecodeString("ff83609b2b24fc73196aac3d3dfb874f")
|
||||
fmt.Printf("key: %s\n", key)
|
||||
decrypted, err := DecryptIDCard(encryptedIDCardForTest, key)
|
||||
if err != nil {
|
||||
t.Fatalf("身份证解密失败: %v", err)
|
||||
}
|
||||
fmt.Printf("decrypted: %s\n", decrypted)
|
||||
}
|
||||
|
||||
// TestEncryptIDCard_DecryptIDCard 身份证加密解密 round-trip 测试
|
||||
func TestEncryptIDCard_DecryptIDCard(t *testing.T) {
|
||||
idCard := "360733199005090032"
|
||||
key, _ := hex.DecodeString("ff83609b2b24fc73196aac3d3dfb874f")
|
||||
|
||||
// 加密
|
||||
fmt.Printf("idCard: %s\n", idCard)
|
||||
encrypted, err := EncryptIDCard(idCard, key)
|
||||
if err != nil {
|
||||
t.Fatalf("身份证加密失败: %v", err)
|
||||
}
|
||||
fmt.Printf("encrypted: %s\n", encrypted)
|
||||
// 解密
|
||||
decrypted, err := DecryptIDCard(encrypted, key)
|
||||
if err != nil {
|
||||
t.Fatalf("身份证解密失败: %v", err)
|
||||
}
|
||||
|
||||
if decrypted != idCard {
|
||||
t.Errorf("解密结果不匹配,期望: %s, 实际: %s", idCard, decrypted)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDecryptIDCard_EdgeCases 身份证解密边界情况
|
||||
func TestDecryptIDCard_EdgeCases(t *testing.T) {
|
||||
key, _ := hex.DecodeString("ff83609b2b24fc73196aac3d3dfb874f")
|
||||
|
||||
t.Run("空密文", func(t *testing.T) {
|
||||
_, err := DecryptIDCard("", key)
|
||||
if err == nil {
|
||||
t.Error("空密文应返回错误")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("无效Base64", func(t *testing.T) {
|
||||
_, err := DecryptIDCard("invalid-base64!!!@#$", key)
|
||||
if err == nil {
|
||||
t.Error("无效Base64应返回错误")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("错误密钥", func(t *testing.T) {
|
||||
encrypted, err := EncryptIDCard("440101199001011234", key)
|
||||
if err != nil {
|
||||
t.Fatalf("加密失败: %v", err)
|
||||
}
|
||||
wrongKey, _ := hex.DecodeString("00000000000000000000000000000000")
|
||||
_, err = DecryptIDCard(encrypted, wrongKey)
|
||||
if err == nil {
|
||||
t.Error("错误密钥应返回错误")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("密文长度不是块大小整数倍", func(t *testing.T) {
|
||||
shortCipher := base64.StdEncoding.EncodeToString([]byte("short"))
|
||||
_, err := DecryptIDCard(shortCipher, key)
|
||||
if err == nil {
|
||||
t.Error("密文长度异常应返回错误")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("加密空身份证应返回错误", func(t *testing.T) {
|
||||
_, err := EncryptIDCard("", key)
|
||||
if err == nil {
|
||||
t.Error("空身份证号应返回错误")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestAesEcbKeyValidation(t *testing.T) {
|
||||
// 测试不同长度的密钥
|
||||
validKeys := [][]byte{
|
||||
|
||||
Reference in New Issue
Block a user