mbe*_*Net 2 c# encryption encryption-asymmetric x509certificate2
我目前正在做一个使用证书加密数据的概念验证。它运行良好,但现在,我想尝试证书过期的场景。我创建了一个过期的证书,我惊讶地注意到即使使用过期的证书,一切都可以正常工作。我期待一个错误。
你知道是不是因为它是自签名证书?
这是我用来测试我的案例的代码
[TestMethod]
public void Encrypt_decrypt_with_expired_certificate()
{
//Arrange
var baseString = "This is an encryption test";
X509Certificate2 newX509Certificate2 = new X509Certificate2("d:\\testx509certExpired.pfx", "apassword");
Console.WriteLine(newX509Certificate2.NotAfter); //Show the expiration date which is in the past
var encryptor = new CertificateEncryptor(newX509Certificate2); //This class is a simple wrapper around RSACryptoServiceProvider
//Act
string encryptedResult = encryptor.Encrypt(baseString); //Exception expected because of the expired certificate but not thrown
//Assert
Console.WriteLine("Base string : {0}", baseString);
Console.WriteLine("Encrypted string : {0}", encryptedResult);
Assert.IsNotNull(encryptedResult);
//revert back
string decryptedString = encryptor.Decrypt(encryptedResult);
Console.WriteLine("Decrypted string : {0}", decryptedString);
Assert.AreEqual(baseString, decryptedString);
}
Run Code Online (Sandbox Code Playgroud)
谢谢
正如 GregS 所说,RSACryptoServiceProvider类(不是 X509Certificate2)提供了执行加密操作的能力。RSACryptoServiceProvider对证书一无所知,它只知道密钥及其参数。这就是您看不到任何错误的原因。
这意味着证书验证 - 是您的应用程序的责任。您应该在加密数据时检查证书并跳过所有证书检查以解密数据。
| 归档时间: |
|
| 查看次数: |
1600 次 |
| 最近记录: |