C# Rsa Error Occurred While Decoding Oaep Padding
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn system.security.cryptography.cryptographicexception: error occurred while decoding oaep padding. more about Stack Overflow the company Business Learn more about hiring developers or posting error occurred while decoding oaep padding powershell ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack rsa oaep padding c# Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up RSACryptoServiceProvider error occurred while decoding OAEP padding up vote -1 down error occurred while decoding oaep padding saml vote favorite I've read many articles regarding this problem and I cannot seem to find an answer that solves my bug. I have a WCF service; my app sends the public RSA key generated and the service returns a set of AES key+IV encrypted with the public key, but when my app then tries to decrypt the keys, I either get "Error occurred while decoding OAEP padding" (if
Oaep Padding Example
I pass true to useOAEP) or "The parameter is incorrect" (if I pass false to useOAEP). For simplicity, this is the entire code used put together, leaving Exception handling aside: public static string EncryptAES(byte[] aesKey, byte[] aesIV, string publicRSAKey) { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { rsa.FromXmlString(publicRSAKey); byte[] encryptedKey = rsa.Encrypt(aesKey, true); byte[] encryptedIV = rsa.Encrypt(aesIV, true); return string.Format("{0}{1}{2}", Convert.ToBase64String(encryptedKey), "\n", Convert.ToBase64String(encryptedIV)); } } public static byte[] Decrypt(string aesKeyorIV) { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { byte[] buffer = Convert.FromBase64String(aesKeyorIV); rsa.FromXmlString(RSA_Private_Key); return rsa.Decrypt(buffer, true); //Exception thrown here } } (RSA_Private_Key is generated in the static constructor using rsa.ToXmlString(true)) (publicRSAKey is passed to the service by sending rsa.ToXmlString(false) in the static constuctor) I tried using Encrypt/Decrypt(byte[], false) but it didn't make any difference. I also tried Array.Reverse(encryptedKey) and then Array.Reverse(buffer). The private key is never sent/modified after it's set so that cannot be it. I'm out of ideas and debugging the WCF service is pretty horrible. c# wcf encryption rsa share|improve this question edited Sep 1 '15 at 2:05 Andrew Savinykh 11.2k64372 asked Sep 1 '15 at 1:10 Camilo Terevinto 2,56231033 @zespri, I said I'm encrypting the AES key with RSA. Besides, I am not getting any error regarding bad l
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more
Sviconfig Finished With An Error. Exit Code: 4
about Stack Overflow the company Business Learn more about hiring developers or posting ads c# decrypt with public key with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow rsacryptoserviceprovider decrypt with public key is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up C# RSA encrypt/decrypt throws exception up vote 1 down vote favorite 1 I'm http://stackoverflow.com/questions/32322113/rsacryptoserviceprovider-error-occurred-while-decoding-oaep-padding trying to set up a simple server side RSA encryption of a small chunk of info which is to be decrypted on the client side. Just as a proof of concept I wrote a few lines to ensure that the public and private key could be loaded from xml. However, I'm struggling to make even the most simple stuff work on my machine: byte[] bytes = Encoding.UTF8.GetBytes("Some text"); bool http://stackoverflow.com/questions/1629992/c-sharp-rsa-encrypt-decrypt-throws-exception fOAEP = true; // seeding a public and private key RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); var publicKey = rsa.ToXmlString(false); var privateKey = rsa.ToXmlString(true); //server side RSACryptoServiceProvider rsaServer = new RSACryptoServiceProvider(); rsaServer.FromXmlString(privateKey); var encrypted = rsaServer.Encrypt(bytes, fOAEP); //client side RSACryptoServiceProvider rsaClient = new RSACryptoServiceProvider(); rsaClient.FromXmlString(publicKey); var decrypted = rsaClient.Decrypt(encrypted, fOAEP); The last call to Decrypt throw a CryptographicException with the message "Error occurred while decoding OAEP padding.". I must be missing something totally obvious here. Do I need more setup of the rsa instances or maybe the initial rsa seeding instance? c# rsacryptoserviceprovider share|improve this question asked Oct 27 '09 at 10:42 soren.enemaerke 2,56033662 add a comment| 1 Answer 1 active oldest votes up vote 6 down vote accepted You should use public key for encryption and private key for decryption. Take a look here: RSACryptoServiceProvider decrypt with public key Now, let's get back to the RSACryptoServiceProvider class. The Encrypt method ONLY encrypts using the public key and the Decrypt method only decrypts using the private key. share|improve this answer answered Oct 27 '09 at 10:49 Nikolay R 81521119 Thanks Nikolay...I was expection something as obvious as this :) –soren.enemaerke Oct 27 '09 at 11:04 :) You're welcome. –Nikolay R Oct 27 '09
ASP.NET Community Standup Forums Help Home/ASP.NET Forums/General ASP.NET/Security/Error occurred while decoding OAEP padding http://forums.asp.net/t/1430677.aspx?Error+occurred+while+decoding+OAEP+padding Error occurred while decoding OAEP padding RSS 11 replies Last http://beachbc.blogspot.com/2008/12/rsa-in-c-net.html post Jan 29, 2010 01:13 PM by anvilis ‹ Previous Thread|Next Thread › Print Share Twitter Facebook Email Shortcuts Active Threads Unanswered Threads Unresolved Threads Support Options Advanced Search Related Links GuidanceSamples Reply Meetu Choudh... Member 278 Points oaep padding 269 Posts Error occurred while decoding OAEP padding Jun 03, 2009 02:32 AM|Meetu Choudhary|LINK Hi, Iam half th way of my problem,,. Please Help I have succesfuly encrypted the text using public key of digital signatures but while decrypting it. i am getting error Error occurred while decoding OAEP error occurred while padding my piece of code #region Test Encryption public void a() { using (var rsa = new RSACryptoServiceProvider()) { // This String consists only Public Key Information String publicKeyOnly = rsa.ToXmlString(false); // This String consists both Private/Public Key information String publicPrivate = rsa.ToXmlString(true); } } //encrypt public byte[] b(String publicKeyOnly) { String s = "
encrypted string.Decrypt: takes an encrypted string and returns a plain text string.1)Server:Generate a RSA public and private key.Pass the public key in plain text to the client2)Client:Pass a plain string to RSAObjectPass encrypted string from RSAObject to Server.3)Server:Pass encrypted string to RSAObjectUse decrypted string from RSAObject.PROBLEMS:My origional code for encrypting and decrypting data:public string Encrypt(string plainText) { try { byte[] plainData = Encoding.UTF32.GetBytes(plainText); byte[] encryptedData = crypto.Encrypt(plainData, true); return Encoding.UTF32.GetString(encryptedData); } catch (Exception ex) { Console.WriteLine(ex.ToString()); return ""; } } public string Decrypt(string encryptedText) { try { byte[] encryptedData = Encoding.UTF32.GetString(encryptedText); byte[] plainData = crypto.Decrypt(encryptedData, true); return Encoding.UTF32.GetString(plainData); } catch (Exception ex) { Console.WriteLine(ex.ToString()); return ""; } }All I would get during decryption from crypto, a RSACryptoServiceProvider object, was one of two exceptions:Bad LengthError occurred while decoding OAEP padding.After reading up more on RSA I discovered that a UTF32(32bit) can't properly handle the data returned by crypto.Encrypt, therefore some of my bytes were being changed in the conversion process to UTF32, and therefor could not be decrypted. use Convert.ToBase64String(string)SOLUTION:Encrypt method needs to convert to a 64bit string.Decrypt method needs to convert from a 64bit string.fixed and working source code:public string Encrypt(string plainText) { try { byte[] plainData = Encoding.UTF32.GetBytes(plainText); byte[] encryptedData = crypto.Encrypt(plainData, true); return Convert.ToBase64String(encryptedData); } catch (Exception ex) { Console.WriteLine(ex.ToString()); return ""; } } public string Decrypt(string encryptedText) { try { byte[] encryptedData = Convert.FromBase64String(encryptedText); byte[] plainData = crypto.Decrypt(encryptedData, true); return Encoding.UTF32.GetString(plainData); } catch (Exception ex) { Console.WriteLine(ex.ToString()); return ""; } }cheers,-bb Posted by Bradley Beach at 3:38 PM Labels: .NET, c#, cryptogrophy, Network, programming, RSA No comments: Post a Comment Newer Post Home Subscribe to: Post Comments (Atom) Blog Archive ► 2010 (1) ► June (1) ► 2009 (5) ► October (1) ► May (2) ► April (1) ► January (1) ▼ 2008 (2) ▼ Dec