Error occurred while decoding OAEP padding
Asked Answered
G

8

23

While decrypting text using RSACryptoServiceProvider.Decrypt, I am getting the error:

Error occurred while decoding OAEP padding.

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.

Note: This question is in continuation of the Error occurred while decoding OAEP padding question

Goosefoot answered 5/6, 2009 at 5:48 Comment(10)
You might have a better chance of getting an answer, it you told us the error message and not just that an error occured. Someone might have run into the same error, and remember what solved the problem.Bashful
Please don't post duplicate questions. This belongs at your original post (#950407).Funeral
I also tried Array.Reverse(EncryptedStrAsByt); before Byte[] DecryptedStrAsByt = RSA3.Decrypt(EncryptedStrAsByt, true); but still no results error is same.Goosefoot
I tried posting comment in that question but word limit so i posted new quetion sorryGoosefoot
Error occurred while decoding OAEP padding.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.Security.Cryptography.CryptographicException: Error occurred while decoding OAEP padding. Source Error: Line 83:Byte[] DecryptedStrAsByt = RSA3.Decrypt(EncryptedStrAsByt, true);Goosefoot
Stack Trace: [CryptographicException: Error occurred while decoding OAEP padding.] System.Security.Cryptography.Utils._DecryptPKWin2KEnhGoosefoot
(SafeKeyHandle hPubKey, Byte[] key, Boolean fOAEP, Int32& hr) +0 System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb, Boolean fOAEP) +214 Secure_Login.WebForm3.Page_Load(Object sender, EventArgs e) in D:\Secure Login\Secure Login\WebForm3.aspx.cs:83 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +99Goosefoot
System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627Goosefoot
Any one here ? Please Help.....................Goosefoot
should i repost the question or wait for the responseGoosefoot
N
23

A common mistake is to try to decrypt using the public key.

Norty answered 24/10, 2009 at 17:23 Comment(5)
I want to encrypt data with my private key and then decrypt it with my public key on the client machine. Why is this a mistake ? I thought that was a normal way of using RSA. :(Spice
No, the "normal" way is to encrypt with the public key and decrypt with the private key. If you encrypt data with the private key, anyone with your public key can decrypt that data. While this is not useful for keeping the data private, it is useful for verifying that the data did come from the expected origin.Fawcett
(This is in the general case. I'm pretty sure the OpenSSL library RSA_encrypt/decrypt functions cannot perform this action).Fawcett
@NoOne: "I want to encrypt data with my private key and then decrypt it with my public key" - yes, its not correct. Perhaps you want a Signature Scheme with Recovery.Effeminate
Another reason for this is that you're simply using the wrong keyset for decryption. I've encountered this when, for example, loading the wrong key file into an application to decrypt data.Rudelson
T
16

I ran into this exact problem. UnicodeEncoding.GetBytes is not always the inverse of 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. A lot of encrypt/decrypt examples on the web use Unicode encoding. Do not use Unicode encoding. Use Convert.FromBase64String and Convert.ToBase64String instead.

Topazolite answered 29/1, 2010 at 18:25 Comment(3)
Thanks this solved my problem, which was starting to drive me nuts!Mysterious
Thanks....Faced same issue..This helpedPurulent
It is if you use codepage 1252 to store binary in a string instead of using Unicode. i.e. System.Encodings.GetEncoding(1252).GetBytes() and .GetString() will be the exact inverse.Intermediate
C
5

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.

Cream answered 23/9, 2010 at 22:55 Comment(1)
Thank for you advice. I used the correct private key, but wrong public key. When replace it by the correct public key file, it solved.Dublin
G
3

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 in node-rsa.

So don't forget to check these options too.

Grappa answered 13/8, 2012 at 14:9 Comment(0)
M
2

FYI, you can still be (en/de)crypting in the right key sequence (encr:pub key, decr:priv key) - i.e. can still get this error decrypting with a private key - it just may be the wrong private key (i.e. from another cert/key pair), not the one paired w/ the pub key with which u encrypted initially. If u turn off OAEP padding and get a "bad data" exception, that's another indication.

Managua answered 10/10, 2018 at 21:35 Comment(1)
Currently facing this hurdle at this present time.Amalita
C
1

We were getting this issue when we were using the wrong key for decryption.

Conventional answered 27/1, 2017 at 17:20 Comment(0)
H
0

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; e.g you must not use strlen for it will stop when encounter a '\0' in the string.

Hachmann answered 26/5, 2014 at 2:52 Comment(0)
P
0

Another thing to check: it was giving me this error, on the decrypt operation, as a result of forgetting to pass the public key into the RSACryptoServiceProvider for the encrypt operation.

Park answered 24/4, 2015 at 20:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.