Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 252085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:38:55+00:00 2026-05-11T21:38:55+00:00

while encrypting and descripting the string with rsa provider I am getting this error.

  • 0

while encrypting and descripting the string with rsa provider I am getting this error.

RSA Data decryption error.The data to be decrypted exceeds the maximum for this modulus of 64 bytes.

Can any one have idea how to slove this error?


    internal sealed class RSAProvider
    {
        #region key store class

        [Serializable]
            private struct rsaKey
        {
            public rsaKey(RSAParameters rsaKeyInfo)
            {
                D = rsaKeyInfo.D;
                DP = rsaKeyInfo.DP;
                DQ = rsaKeyInfo.DQ;
                Exponent = rsaKeyInfo.Exponent;
                InverseQ = rsaKeyInfo.InverseQ;
                Modulus = rsaKeyInfo.Modulus;
                P = rsaKeyInfo.P;
                Q = rsaKeyInfo.Q;
            }

            public RSAParameters CreateRSAKey()
            {
                RSAParameters rsaKeyInfo = new RSAParameters();

                rsaKeyInfo.D = D;
                rsaKeyInfo.DP = DP;
                rsaKeyInfo.DQ = DQ;
                rsaKeyInfo.Exponent = Exponent;
                rsaKeyInfo.InverseQ = InverseQ;
                rsaKeyInfo.Modulus = Modulus;
                rsaKeyInfo.P = P;
                rsaKeyInfo.Q = Q;

                return rsaKeyInfo;
            }

            public byte[] D;
            public byte[] DP;
            public byte[] DQ;
            public byte[] Exponent;
            public byte[] InverseQ;
            public byte[] Modulus;
            public byte[] P;
            public byte[] Q;
        }

        #endregion

        private static RSAParameters rsaKeyParameters;

        static RSAProvider()
        {
            string rsaKeyString = System.Configuration.ConfigurationSettings.AppSettings["RSAKey"];
            if(rsaKeyString != null)
            {
                rsaKeyParameters = GetKeyByString(rsaKeyString);
            }
        }

        private RSAProvider()
        {
        }

        private static RSAParameters RSAKeyInfo
        {
            get
            {
                return rsaKeyParameters;
            }
        }

        private static bool DoOAEPPadding
        {
            get
            {
                return false;
            }
        }

        public static string GenerateKey(int keySize)
        {
            //Create a new instance of RSACryptoServiceProvider to generate
            //public and private key data.
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(keySize);

            RSAParameters rsaKeyInfo = RSA.ExportParameters(true);

            return GetKeyString(rsaKeyInfo);
        }


        #region Encrypt

        public static byte[] Encrypt(byte[] dataToEncrypt, string rsaKeyString)
        {
            RSAParameters rsaKeyInfo = GetKeyByString(rsaKeyString);

            return Encrypt(dataToEncrypt, rsaKeyInfo);
        }

        public static byte[] Encrypt(byte[] dataToEncrypt, RSAParameters rsaKeyInfo)
        {
            try
            {   
                //Create a new instance of RSACryptoServiceProvider.
               // Common.Identity.ImpersonateValidUser("prana", "eetplpvt", "Avdhoota1985");
                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

                //Import the RSA Key information. This only needs
                //toinclude the public key information.
                RSA.ImportParameters(rsaKeyInfo);

                //Encrypt the passed byte array and specify OAEP padding.  
                //OAEP padding is only available on Microsoft Windows XP or
                //later.  
                //return RSA.Encrypt(dataToEncrypt, DoOAEPPadding);
                byte[] data = RSA.Encrypt(dataToEncrypt, DoOAEPPadding);
                RSA.Clear();
                //Common.Identity.UndoImpersonation();
                return data;
            }
                //Catch and display a CryptographicException  
                //to the console.
            catch(CryptographicException e)
            {
                // Updated By Divya Bhalodia on 27th June 2008 for Localization task
                //throw new Exception("Data encryption error.", e);
                Common.EnumLocalization.EnumLocalization loc = new Common.EnumLocalization.EnumLocalization(ASP.BL.ApplicationUsers.ApplicationUserController.CurrentUserCulture.Code, ASP.BL.Applications.ApplicationController.CurrentApplicationInfo.ItemId);
                throw new Exception(loc.LocalizeString("RSA Data encryption error.") + e.Message, e);
                // end Updated - Divya
            }
        }

        public static byte[] Encrypt(byte[] dataToEncrypt)
        {
            return Encrypt(dataToEncrypt, RSAKeyInfo);
        }

        #endregion

        #region Decrypt

        public static byte[] Decrypt(byte[] dataToDecrypt, string rsaKeyString, bool doOAEPPadding)
        {
            RSAParameters rsaKeyInfo = GetKeyByString(rsaKeyString);
            return Decrypt(dataToDecrypt, rsaKeyInfo, doOAEPPadding);
        }

        public static byte[] Decrypt(byte[] dataToDecrypt, RSAParameters rsaKeyInfo, bool doOAEPPadding)
        {
            try
            {
                //Create a new instance of RSACryptoServiceProvider.
                Common.Identity.ImpersonateValidUser();
                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

                //Import the RSA Key information. This needs
                //to include the private key information.
                RSA.ImportParameters(rsaKeyInfo);

                //Decrypt the passed byte array and specify OAEP padding.  
                //OAEP padding is only available on Microsoft Windows XP or
                //later.  
                //return RSA.Decrypt(dataToDecrypt, doOAEPPadding);
                byte[] data = RSA.Decrypt(dataToDecrypt, doOAEPPadding);
                RSA.Clear();
                Common.Identity.UndoImpersonation();
                return data;
            }
                //Catch and display a CryptographicException  
                //to the console.
            catch(CryptographicException e)
            {

                // Updated By Divya Bhalodia on 27th June 2008 for Localization task
                //throw new Exception("Data decryption error.", e);
                Common.EnumLocalization.EnumLocalization loc = new Common.EnumLocalization.EnumLocalization(ASP.BL.ApplicationUsers.ApplicationUserController.CurrentUserCulture.Code, ASP.BL.Applications.ApplicationController.CurrentApplicationInfo.ItemId);
                throw new Exception(loc.LocalizeString("RSA Data decryption error.") + e.Message, e);
                // end Updated - Divya
            }
        }

        public static byte[] Decrypt(byte[] dataToDecrypt)
        {
            return Decrypt(dataToDecrypt, RSAKeyInfo, DoOAEPPadding);
        }
        #endregion

        #region Additional functions

        private static string GetKeyString(RSAParameters rsaKeyInfo)
        {
            byte[] tmp;
            rsaKey k = new rsaKey(rsaKeyInfo);
            BinaryFormatter formater = new BinaryFormatter();

            using(MemoryStream stream = new MemoryStream())
            {
                formater.Serialize(stream, k);
                tmp = stream.ToArray();
            }

            Code(tmp);

            return Convert.ToBase64String(tmp);
        }


        private static RSAParameters GetKeyByString(string rsaKeyString)
        {
            rsaKey k;

            byte[] tmp = Convert.FromBase64String(rsaKeyString);
            Code(tmp);

            BinaryFormatter formater = new BinaryFormatter();

            using(MemoryStream stream = new MemoryStream(tmp))
            {
                k = (rsaKey)formater.Deserialize(stream);
            }
            return k.CreateRSAKey();
        }


        private static void Code(byte[] tmp)
        {
            byte mask1 = 0x55;
            byte mask3 = 0xB9;
            byte mask4 = 0xCF;

            for(int i = 0; i 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:38:56+00:00Added an answer on May 11, 2026 at 9:38 pm

    I’ve encoutered similar problems but you can do two things to help yourself overcome them.

    1. You need to ensure that hte data you are encrypting is shorter than the key that you are using. so if your key is 1024 bits then make sure that you are only bassing in say 1000 bits. To do this you need to get chunk your byte array into smaller chunks, encrypt each chunk and then store the encrypeted value in an array or a string. So instead of encrypting 1 string you encrypt say 5 strings.

    When storing this information as a string make sure that all numbers are the same length, so if the formatter returns 15 you store the string with 015 so that you just divide by 3 later to get the byte to then put into the array.

    To decrypt your data you need to simply read the length of the string and determine how many chunks to decrypt. Decrupt these one by one and then you can recreate the object with the decrupted byte array.

    if you would like actual code please contact me personally and I’ll be able to help you better with some script that can do this for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 147k
  • Answers 147k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Although there are overloads for GetAllUsers() to return a subset… May 12, 2026 at 9:10 am
  • Editorial Team
    Editorial Team added an answer I see your point. When the path is 200 chars… May 12, 2026 at 9:10 am
  • Editorial Team
    Editorial Team added an answer If you are really coding, not just brainteasing never ever… May 12, 2026 at 9:10 am

Related Questions

Not as in can't find the answer on stackoverflow, but as in can't see
I'm trying to encrypt and decrypt a file stream over a socket using RijndaelManaged,
I have a web site with users and their data accordingly. What is the
I'm looking for a solution to perform file encrypting and signing. This would be

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.