【发布时间】:2010-10-31 13:32:09
【问题描述】:
在使用 RSACryptoServiceProvider.Decrypt 解密文本时,我收到了错误:
解码 OAEP 填充时出错。
这是我的代码:
CspParameters cspParam = new CspParameters();
cspParam = new CspParameters();
cspParam.Flags = CspProviderFlags.UseMachineKeyStore;
clsCertificates cc = new clsCertificates();
string a = "";
cc.OpenStoreIE(ref a);
cc.SetProperties();
X509Certificate2 cert = new X509Certificate2();
cert = cc.x509_2Cert;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspParam);
//to gentrate private and public keys from the certificate
rsa.FromXmlString(cert.PublicKey.Key.ToXmlString(false));
String publicKey = rsa.ToXmlString(false); // gets the public key
String privateKey = rsa.ToXmlString(true); // gets the private key working if paramter is false if true give error key is not valid for use in specified state
Response.Write("<Textarea rows=10 cols=100>PUBLIC: " + publicKey + "</TextArea>");
Response.Write("<Textarea rows=10 cols=100>PRIVATE: " + privateKey + "</Textarea>");
Response.Write("<BR>Encrypting the string \"HelloThere\" with the public Key:<BR>");
String str = "HelloThere";
RSACryptoServiceProvider RSA2 = new RSACryptoServiceProvider(cspParam);
//---Load the Public key---
RSA2.FromXmlString(publicKey);
//working with the folowing line instead of above but i need the keys of he certificte
//RSA2.ToXmlString(true);
Byte[] EncryptedStrAsByt = RSA2.Encrypt(System.Text.Encoding.Unicode.GetBytes(str), true);
String EncryptedStr = System.Text.Encoding.Unicode.GetString(EncryptedStrAsByt);
Response.Write("<Textarea rows=10 cols=100>Encrypted String: " + EncryptedStr + "</Textarea>");
Response.Write("<BR>Decrypting the Encrypted String with the Private key:<BR>");
RSACryptoServiceProvider RSA3 = new RSACryptoServiceProvider(cspParam);
//---Load the Private key---
RSA3.FromXmlString(privateKey);
//working with the folowing line instead of above but i need the keys of he certificte
//RSA3.ToXmlString(true);
Byte[] DecryptedStrAsByt = RSA3.Decrypt(EncryptedStrAsByt, true );//Error if true then error is error occured while decoding the OAE$P padding and if false then error is bad key i am using windows xp so it should be true.
String DecryptedStr = System.Text.Encoding.Unicode.GetString(DecryptedStrAsByt);
Response.Write("<Textarea rows=10 cols=100>Decrypted String: " + DecryptedStr + "</Textarea>");
如果我不使用我的数字证书的密钥,以上是有效的。但如果密钥来自数字证书,则会出现 OAEP 填充错误。
注意:这个问题是Error occurred while decoding OAEP padding问题的延续
【问题讨论】:
-
如果您告诉我们错误消息,而不仅仅是发生错误,您可能更有可能获得答案。有人可能遇到过同样的错误,并记住解决问题的方法。
-
请不要发布重复的问题。这属于您的原始帖子 (stackoverflow.com/questions/949907/…)。
-
我也试过 Array.Reverse(EncryptedStrAsByt);之前 Byte[] DecryptedStrAsByt = RSA3.Decrypt(EncryptedStrAsByt, true);但仍然没有结果错误是一样的。
-
我尝试在该问题中发表评论,但字数限制,所以我发布了新问题抱歉
-
解码 OAEP 填充时发生错误。描述:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。异常详细信息:System.Security.Cryptography.CryptographicException:解码 OAEP 填充时发生错误。源错误:第 83 行:Byte[] DecryptedStrAsByt = RSA3.Decrypt(EncryptedStrAsByt, true);
标签: c# encryption rsa digital-signature rsacryptoserviceprovider