【问题标题】:No error encrypting / decrypting data with an expired certificate using RSACryptoServiceProvider使用 RSACryptoServiceProvider 使用过期证书加密/解密数据没有错误
【发布时间】:2015-03-21 19:36:45
【问题描述】:

我目前正在做一个概念验证,以使用证书加密数据。它运作良好,但现在,我想尝试证书过期的场景。我创建了一个过期的证书,我惊讶地发现即使证书过期了,everthing 也能正常工作。我期待一个错误。

你知道是不是因为它是自签名证书吗?

这是我用来测试案例的代码

[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);
}

谢谢

【问题讨论】:

  • 在我看来可能最适合代码审查。
  • 如果你想使用过期的证书,X509Certificate2 类不会阻止你。它确实为您提供了检查过期的方法,因此您可以编写代码来检测条件并做出适当的反应。

标签: c# encryption encryption-asymmetric x509certificate2


【解决方案1】:

正如 GregS 所说,RSACryptoServiceProvider 类(不是 X509Certificate2)提供了执行加密操作的能力。 RSACryptoServiceProvider 对证书一无所知,它只知道密钥及其参数。这就是您看不到任何错误的原因。

这意味着证书验证 -- 是您的应用程序的责任。加密数据时应检查证书,并跳过所有证书检查以解密数据。

【讨论】:

  • 此外,证书中的“密钥”实际上并不是“可过期的”。证书上的“expriy”是证书上独立于密钥的 ASN1 对象。
  • 我不相信这种情况了。当我试图通过尝试访问 X509Certificate2.PublicKey.Key 属性来将证书公钥转换为 RSACryptoServiceProvider 对象时,会引发 CryptographicException,仅指示“坏密钥”。这是.NET 4.5
【解决方案2】:

当尝试访问证书的 X509Certificate2.PublicKey.Key 属性时,如果证书不在其有效期内,则应抛出 CryptographicException。

这是我从证书加载公钥和私钥以执行加密操作的方式:

using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

class Example

{
    private RSACryptoServiceProvider publicKey,
                                     privateKey;

    private bool getRSAKeys(X509Certificate2 cert, StoreLocation location)
    {
        try
        {
            //This will throw a CryptographicException if the certificate is expired
            publicKey = (RSACryptoServiceProvider)cert.PublicKey.Key;

            privateKey = (RSACryptoServiceProvider)cert.PrivateKey;
            return true;
        }
        catch (CryptographicException e)
        {
            Console.WriteLine("The certificate is expired or otherwise unusable\r\n" + e.ToString());
            return false;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多