c# - 解码 OAEP 填充时出错

标签 c# encryption rsa digital-signature rsacryptoserviceprovider

使用 RSACryptoServiceProvider.Decrypt 解密文本时,出现错误:

Error occurred while decoding OAEP padding.

这是我的代码:

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>");

如果我不使用我的数字证书的 key ,上面的方法是有效的。但如果 key 来自数字证书,我会收到 OAEP 填充错误。

注意:此问题是 Error occurred while decoding OAEP padding 的延续问题

最佳答案

一个常见的错误是尝试使用公钥解密。

关于c# - 解码 OAEP 填充时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/954416/

相关文章:

c# System.Diagnostics.Process 不执行任何操作或抛出错误

c# - 防止任何子类覆盖/隐藏方法 c#;为父类(super class)保留方法签名

ios - Objective C 中的 RSA 实现

java - RSA 签名异常 : Signature length not correct

c# - Android 的 Mono 无法构建 - 找不到 Novell.MonoDroid.CSharp.targets

c# - 在 Jet 引擎 (Access) 数据库中存储一个 64 位整数?

c# - .net 加密数据库表

java - 使 Android 键盘能够抵御 KeyLogger 攻击

java - PHP和JAVA加解密例程

ios - RSA 加密长度结果不一致