公钥RSA解密(C#)

时间:2016-05-06 21:46:44

标签: c# encryption cryptography rsa public-key

我的问题如下:

我收到带有RSA私钥的加密字符串。我有公钥来解密它和下一个方法(基于http://www.codeproject.com/Articles/10877/Public-Key-RSA-Encryption-in-C-NET之类的建议 和msdn):

relativePath(rootProject.projectDir)

当你跑步时我使用以下值:

inputString = public static string DecryptString(string inputString, int dwKeySize, string xmlString){ try { RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider(dwKeySize); rsaCryptoServiceProvider.FromXmlString(xmlString); int base64BlockSize = ((dwKeySize / 8) % 3 != 0) ? (((dwKeySize / 8) / 3) * 4) + 4 : ((dwKeySize / 8) / 3) * 4; int iterations = inputString.Length / base64BlockSize; ArrayList arrayList = new ArrayList(); for (int i = 0; i < iterations; i++) { byte[] encryptedBytes = Convert.FromBase64String(inputString.Substring(base64BlockSize * i, base64BlockSize)); Array.Reverse(encryptedBytes); arrayList.AddRange(rsaCryptoServiceProvider.Decrypt(encryptedBytes, false)); } return Encoding.UTF32.GetString(arrayList.ToArray(Type.GetType("System.Byte")) as byte[]); } catch (Exception exp) { Console.WriteLine(exp.ToString()); throw exp; } } 加密字符串344个字符。

dwKeySize = g/h/UizCowOX/uQubCZ1 .... I1k2FaCMSOpkYcTOQ==

xmlString = 2048,公共密钥为xml格式。

收到以下错误:

<RSAKeyValue><Modulus>mxBSd9ss/OmV ... D8miCOWclUJdbWw==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>

如果我使用最后一个参数调用rsaCryptoServiceProvider.Decrypt为TRUE,则消息为:

System.Security.Cryptography.CryptographicException: Key does not exist.
   at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
   at System.Security.Cryptography.RSACryptoServiceProvider.DecryptKey(SafeKeyHandle pKeyContext, Byte[] pbEncryptedKey, Int32 cbEncryptedKey,Boolean fOAEP, ObjectHandleOnStack ohRetDecryptedKey)
   at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb,Boolean fOAEP)
   at ConsoleApplication1.Program.DecryptString(String inputString, Int32 dwKeySize, String xmlString)

我尝试通过以下方式加载公钥替换第一行(实例化提供程序):

System.Security.Cryptography.CryptographicException: Error occurred while decoding OAEP padding.
   at System.Security.Cryptography.RSACryptoServiceProvider.DecryptKey(SafeKeyHandle pKeyContext, Byte[] pbEncryptedKey, Int32 cbEncryptedKey, Boolean fOAEP, ObjectHandleOnStack ohRetDecryptedKey)
   at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb,Boolean fOAEP)
   at ConsoleApplication1.Program.DecryptString(String inputString, Int32 dwKeySize, String xmlString)

但同样的错误也会发生。

有关如何解决问题的任何建议?

0 个答案:

没有答案