简体   繁体   English

解码 OAEP 填充时出错

[英]Error occurred while decoding OAEP padding

While decrypting text using RSACryptoServiceProvider.Decrypt , I am getting the error:使用RSACryptoServiceProvider.Decrypt解密文本时,出现错误:

Error occurred while decoding OAEP padding.解码 OAEP 填充时出错。

Here's my code:这是我的代码:

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

The above is works if I am not using the keys of my digital certificate.如果我不使用我的数字证书的密钥,上述方法是有效的。 but if the keys are from the digital certificate, I get the OAEP padding error.但是如果密钥来自数字证书,我会收到 OAEP 填充错误。

Note: This question is in continuation of the Error occurred while decoding OAEP padding question注意:这个问题是在解码 OAEP 填充问题时发生错误的延续

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

I ran into this exact problem.我遇到了这个确切的问题。 UnicodeEncoding.GetBytes is not always the inverse of UnicodeEncoding.GetString . UnicodeEncoding.GetBytes并不总是与UnicodeEncoding.GetString相反。

byte[] a = new byte[32];

RandomNumberGenerator gen = new RNGCryptoServiceProvider();
gen.GetBytes(a);

UnicodeEncoding byteConverter = new UnicodeEncoding();

byte[] b = byteConverter.GetBytes(byteConverter.GetString(a));

//byte array 'a' and byte array 'b' will not always contain the same elements.

This is why RSACryptoServiceProvider.Decrypt fails.这就是RSACryptoServiceProvider.Decrypt失败的原因。 A lot of encrypt/decrypt examples on the web use Unicode encoding.网络上的许多加密/解密示例都使用 Unicode 编码。 Do not use Unicode encoding.不要使用 Unicode 编码。 Use Convert.FromBase64String and Convert.ToBase64String instead.请改用Convert.FromBase64StringConvert.ToBase64String

This error normally indicates you are using a public key to decrypt, while you should be using a private key for decryption.此错误通常表示您正在使用公钥进行解密,而您应该使用私钥进行解密。 Give it a try.试一试。

In my case the error has been caused by wrong padding settings.在我的情况下,错误是由错误的填充设置引起的。

Error: RSA decrypt: error:0407A079:rsa routines:RSA_padding_check_PKCS1_OAEP:oaep decoding error

I had openssl_public_encrypt() with OPENSSL_PKCS1_PADDING as a default value in PHP and keypair.decrypt() with the default value RSA_PKCS1_OAEP_PADDING innode-rsa .我将openssl_public_encrypt()OPENSSL_PKCS1_PADDING作为PHP 中的默认值和keypair.decrypt()node-rsa 中的默认值RSA_PKCS1_OAEP_PADDING

So don't forget to check these options too.所以不要忘记检查这些选项。

FYI, you can still be (en/de)crypting in the right key sequence (encr:pub key, decr:priv key) - ie can still get this error decrypting with a private key - it just may be the wrong private key (ie from another cert/key pair), not the one paired w/ the pub key with which u encrypted initially.仅供参考,您仍然可以以正确的密钥序列(encr:pub key, decr:priv key)进行(en/de)加密 - 即仍然可以使用私钥解密此错误 - 它可能是错误的私钥(即来自另一个证书/密钥对),而不是与您最初加密的公钥配对的那个。 If u turn off OAEP padding and get a "bad data" exception, that's another indication.如果您关闭 OAEP 填充并获得“错误数据”异常,则这是另一个迹象。

RSA encryption may result non readable character, make sure not to cut the string due to special character indicating end of something during write/read the encryption result; RSA 加密可能会导致不可读的字符,请确保在写入/读取加密结果时不要因为特殊字符指示某事结束而将字符串剪掉; eg you must not use strlen for it will stop when encounter a '\\0' in the string.例如,您不能使用 strlen,因为它会在遇到字符串中的 '\\0' 时停止。

另一件事要检查:由于忘记将公钥传递到RSACryptoServiceProvider进行加密操作,它在解密操作中给了我这个错误。

当我们使用错误的密钥进行解密时,我们遇到了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 解码 OAEP 填充时出现 RSACryptoServiceProvider 错误 - RSACryptoServiceProvider error occurred while decoding OAEP padding 如何在使用GO语言加密时在c#中解密RSA加密字符串。 解码OAEP填充时发生错误 - How to decrypt RSA encrypted string in c# while it was encrypted in GO language. Error occurred while decoding OAEP padding C#:解码OAEP填充奇怪的问题时出错 - C#: Error while decoding OAEP padding weird issue C#RSA加密然后解密失败(错误:0407A079:RSA例程:RSA_padding_check_PKCS1_OAEP:OAEP解码错误) - C# RSA encrypt then decrypt fails(error:0407A079:rsa routines:RSA_padding_check_PKCS1_OAEP:oaep decoding error) 使用RSA-OAEP解密用JavaScript加密的C#中的数据时,OAEP填充错误 - OAEP Padding Error When Decrypting Data in C# That Was Encrypted in JavaScript with RSA-OAEP 解析带有“&”的实体名称时发生错误 - an error occurred while parsing entityname with '&' 创建路由时出错 - An error occurred while creating a route 发送请求时出错 - An error occurred while sending the request 解析 EntityName 时出错 - An error occurred while parsing EntityName 收到HTTP响应错误时发生错误 - An error occurred while receiving the HTTP response error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM