Share via


Facing a error while Decryption: "The data to be decrypted exceeds the maximum for this modulus of 128 bytes."

Question

Monday, November 18, 2013 7:12 AM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Xml;

namespace RSA_Encryption
{
    class Program
    {
        static void Main(string[] args)
        {
            RSAParameters();
            XmlDocument doc = new XmlDocument();
            doc.Load(@"C:\Encrytionanddecrytion.xml");
            Encrypt(doc);
            Decrypt(doc);

        }

        public static void RSAParameters()
        {
            var rsa = new RSACryptoServiceProvider();
            var parameters = rsa.ExportParameters(true);

            byte[] privateParameters = rsa.ExportCspBlob(true);
            byte[] publicParameters = rsa.ExportCspBlob(false);

            Console.WriteLine("PrivateKey");
            string privatekey = Convert.ToBase64String(privateParameters);
            Console.WriteLine(privatekey);

            Console.WriteLine("PublicKey");
            string publickey = Convert.ToBase64String(publicParameters);
            Console.WriteLine(publickey);
            Console.Read();
        }

        static void Encrypt(XmlDocument doc)
        {
            RSACryptoServiceProvider cipher = new RSACryptoServiceProvider();
            string PublicKey = Properties.Resources.PublicKey;
            byte[] Publicbytekey = System.Convert.FromBase64String(PublicKey);
            cipher.ImportCspBlob(Publicbytekey);

            XmlNode Password = doc.SelectSingleNode("/Encrytionanddecrytion/Password");
            byte[] Password = Encoding.ASCII.GetBytes(Password.InnerText);

            byte[] encryptpassword = cipher.Encrypt(Password, false);

            string EncryptedPassword = Convert.ToBase64String(encryptpassword);
            Password.InnerText = EncryptedPassword;

            Console.WriteLine(Password);
            Console.Read();
        }

        static void Decrypt(XmlDocument doc)
        {
            RSACryptoServiceProvider cipher = new RSACryptoServiceProvider();
            string PrivateKey = Properties.Resources.PrivateKey;
            byte[] Privatebytekey = System.Convert.FromBase64String(PrivateKey);
            cipher.ImportCspBlob(Privatebytekey);

            XmlNode Password = doc.SelectSingleNode("/Encrytionanddecrytion/Password");
            byte[] Password = Encoding.ASCII.GetBytes(Password.InnerText);

            byte[] decryptpassword = cipher.Decrypt(Password, false);
            string decryptedPassword = Convert.ToBase64String(decryptpassword);
            Password.InnerText = decryptedPassword;

        }

    }
}

here is the above code that i'm using to Encrypt and decyrpt a String, The String is loading from an xml. With the help of Public and Private Keys i'm encrypting and decrypting the string,as far now the encryption is success ful but while decrypting i'm getting an error like "The data to be decrypted exceeds the maximum for this modulus of 128 bytes." I goggled a lot on this but still i haven't got any clarification.  

                        

All replies (9)

Wednesday, November 20, 2013 4:30 AM âś…Answered

Actually it works good i had changed the 

  Encoding  part while Encrypting and Decrypting instead of "Encoding.ASCII"  used "System.Text.Encoding.Unicode" in both cases it works now. 

byte[] Password = System.Text.Encoding.Unicode.GetString(Password.InnerText);

Monday, November 18, 2013 2:28 PM

Please use Codeblocks for posting Code. reading long code without Syntax Highlighting is almsot impossible.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Xml;

namespace RSA_Encryption
{
    class Program
    {
        static void Main(string[] args)
        {
            RSAParameters();
            XmlDocument doc = new XmlDocument();
            doc.Load(@"C:\Encrytionanddecrytion.xml");
            Encrypt(doc);
            Decrypt(doc);

        }

        public static void RSAParameters()
        {
            var rsa = new RSACryptoServiceProvider();
            var parameters = rsa.ExportParameters(true);

            byte[] privateParameters = rsa.ExportCspBlob(true);
            byte[] publicParameters = rsa.ExportCspBlob(false);

            Console.WriteLine("PrivateKey");
            string privatekey = Convert.ToBase64String(privateParameters);
            Console.WriteLine(privatekey);

            Console.WriteLine("PublicKey");
            string publickey = Convert.ToBase64String(publicParameters);
            Console.WriteLine(publickey);
            Console.Read();
        }

        static void Encrypt(XmlDocument doc)
        {
            RSACryptoServiceProvider cipher = new RSACryptoServiceProvider();
            string PublicKey = Properties.Resources.PublicKey;
            byte[] Publicbytekey = System.Convert.FromBase64String(PublicKey);
            cipher.ImportCspBlob(Publicbytekey);

            XmlNode Password = doc.SelectSingleNode("/Encrytionanddecrytion/Password");
            byte[] Password = Encoding.ASCII.GetBytes(Password.InnerText);

            byte[] encryptpassword = cipher.Encrypt(Password, false);

            string EncryptedPassword = Convert.ToBase64String(encryptpassword);
            Password.InnerText = EncryptedPassword;

            Console.WriteLine(Password);
            Console.Read();
        }

        static void Decrypt(XmlDocument doc)
        {
            RSACryptoServiceProvider cipher = new RSACryptoServiceProvider();
            string PrivateKey = Properties.Resources.PrivateKey;
            byte[] Privatebytekey = System.Convert.FromBase64String(PrivateKey);
            cipher.ImportCspBlob(Privatebytekey);

            XmlNode Password = doc.SelectSingleNode("/Encrytionanddecrytion/Password");
            byte[] Password = Encoding.ASCII.GetBytes(Password.InnerText);

            byte[] decryptpassword = cipher.Decrypt(Password, false);
            string decryptedPassword = Convert.ToBase64String(decryptpassword);
            Password.InnerText = decryptedPassword;

        }

    }
}

We need to know the line on wich the exception happens. It would also help to have the Stacktrace of the exception including all inner exceptions.
The Framework version you are targetting might have an effect too.

Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.


Tuesday, November 19, 2013 4:59 AM

hi Christopher,

                              The Exception was occurred while decryption 

byte[] decryptpassword = cipher.Decrypt(Password, false);  

Tuesday, November 19, 2013 6:16 AM

You've mixed up string encoding/decoding and base64 conversion.

When you encrypt you store the base64 version of the encrypted byte array.

When you decrypt you're supposed to convert that base64 string to a byte array, decrypt the array and then use Encoding.ASCII.GetString to obtain the original string from the decrypted byte array. Your decryption code does exactly the opposite.

And a side note: never use ASCII unless you have specific reasons to do so. Use UTF8 or Unicode encodings instead.


Wednesday, November 20, 2013 7:22 AM

I pretty much doubt that only changing the encoding can help with the exception you're getting. But then, the code you posted doesn't even compile so it's hard to tell what you're actually doing anyway.


Friday, November 22, 2013 5:18 AM

hello mike,

                 What i'm exactly doing here is i'm selecting the particular node which has to be encrypted and decrypted from a xml. Using Public and Private Key i'm doing the Encryption and Decryption.  

 XmlNode Password = doc.SelectSingleNode("/Encrytionanddecrytion/Password");
            byte[]Passwordbyte = Encoding.ASCII.GetBytes(Password.InnerText);

            byte[] encryptpassword = cipher.Encrypt(Password, false);

            string EncryptedPassword = Convert.ToBase64String(encryptpassword);
            Password.InnerText = EncryptedPassword;

After Selecting the particular node which has to be encrypt from xml, loading it into a byte array and doing the encryption. Later i need to save Encrypted text in the form of a string in the xml. This is what i'm doing here.  


Friday, November 22, 2013 6:34 AM

The encryption part is ok, the decryption code is the issue. The way it's written in your post it has no chance of working correctly, Unicode encoding or not.


Friday, November 22, 2013 9:38 AM

Exactly...i did like this in both cases 

XmlNode Password = doc.SelectSingleNode("/Encrytionanddecrytion/Password");
            byte[]Passwordbyte = System.Text.Encoding.Unicode.GetString(Password.InnerText);

            byte[] encryptpassword = cipher.Encrypt(Password, false);

            string EncryptedPassword = Convert.ToBase64String(encryptpassword);
            Password.InnerText = EncryptedPassword; XmlNode Password = doc.SelectSingleNode("/Encrytionanddecrytion/Password");
            byte[] Password = System.Text.Encoding.Unicode.GetString(Password.InnerText);

            byte[] decryptpassword = cipher.Decrypt(Password, false);
            string decryptedPassword = Convert.ToBase64String(decryptpassword);
            Password.InnerText = decryptedPassword;

it works good, i don't know what was the issue before when i use ASCII Encoding while Encryption and Decryption.


Friday, November 22, 2013 9:54 AM

Even if the decryption code doesn't throw any exceptions it doesn't mean it actually works. Your decryption code produces a password in base64 format, the original password was plain text.