解码OAEP Padding C#时出错

时间:2016-06-11 00:12:51

标签: c# encryption

以下是我的代码,错误在标题中。我使用私钥加密但我使用相同的私钥解密我遇到标题中提到的错误,不确定错误的根本原因是什么。在这方面的任何帮助将受到高度赞赏。

Class Program
{
    static void Main(string[] args)
    {
       string thumbPrint = "SomeValue";
       EncryptUserName("Steve", thumbPrint, true, true);
    }
    public static void EncryptUserName(string textToEncript, string certificateThumbprint, bool searchLocalMachine, bool searchUser)
    {
        X509Certificate2 cert = FindCertificate(certificateThumbprint, searchLocalMachine, searchUser);
        RSACryptoServiceProvider rsaEncryptor = (RSACryptoServiceProvider)cert.PublicKey.Key;
        byte[] cipherData = rsaEncryptor.Encrypt(Encoding.UTF8.GetBytes(textToEncript), true);
        var encryptedString  = Convert.ToBase64String(cipherData);
        Console.WriteLine(encryptedString);
    }
    public static byte[] Decrypt(byte[] encryptedData, bool fOAEP, X509Certificate2 certificate)
    {
        if (encryptedData == null)
        {
            throw new ArgumentNullException("encryptedData");
        }
        if (certificate == null)
        {
            throw new ArgumentNullException("certificate");
        }
        if (certificate.PrivateKey == null)
        {
            throw new ApplicationException("Certificate provided has no private key");
        }
        Console.WriteLine(certificate.PrivateKey);
        using (RSACryptoServiceProvider provider = (RSACryptoServiceProvider)certificate.PrivateKey)
        {
            return provider.Decrypt(encryptedData, fOAEP);
        }
    }

    public static string CertificateDecrypt(string textToDecript, string certificateThumbprint, bool searchLocalMachine, bool searchUser)
    {
        X509Certificate2 certificate = FindCertificate(certificateThumbprint, searchLocalMachine, searchUser);
        byte[] bytesArray = Convert.FromBase64String(textToDecript);
        //Decrypt(bytesArray, true, certificate);
        return Encoding.UTF8.GetString(Decrypt(bytesArray, true, certificate));
    }
    public static X509Certificate2 LoadCertificate(StoreName storeName, StoreLocation storeLocation, string thumbprint)
    {
        X509Store store = null;
        X509Certificate2 certificate2;
        try
        {
            store = new X509Store(storeName, storeLocation);
            store.Open(OpenFlags.ReadOnly);
            X509Certificate2Enumerator enumerator = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false).GetEnumerator();
            X509Certificate2 current = null;
            while (enumerator.MoveNext())
            {
                current = enumerator.Current;
            }
            certificate2 = current;
        }
        finally
        {
            if (store != null)
            {
                store.Close();
            }
        }
        return certificate2;
    }
    private static X509Certificate2 FindCertificate(string certificateThumbprint, bool searchLocalMachine, bool searchUser)
    {
        certificateThumbprint = certificateThumbprint.Replace(" ", "");
        X509Certificate2 certificate = null;
        if (searchUser)
        {
            certificate = LoadCertificate(StoreName.My, StoreLocation.CurrentUser, certificateThumbprint);
        }
        if (searchLocalMachine && (certificate == null))
        {
            certificate = LoadCertificate(StoreName.My, StoreLocation.LocalMachine, certificateThumbprint);
        }
        if (certificate == null)
        {
            throw new ApplicationException($"Certificate with thumbprint {certificateThumbprint} cannot be loaded (not found)");
        }
        return certificate;
    }
}

1 个答案:

答案 0 :(得分:1)

解码OAEP时发生错误当解密的输入字符串与加密的输出字符串不完全相同时,或者因为用于加密的公钥与用于解密的私钥不匹配时,就会发生填充。 / p>

在您的情况下,您实际上并没有将加密的字符串传递给Decrypt方法,因此它绝对会一直失败,从而导致加密和解密密钥不匹配。