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

  • Home
  • SEARCH
  • 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 154245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:56:41+00:00 2026-05-11T09:56:41+00:00

The class that produces Bad Data errors: using System; using System.Collections.Generic; using System.Security.Cryptography; using

  • 0

The class that produces ‘Bad Data’ errors:

  using System; using System.Collections.Generic; using System.Security.Cryptography; using System.Text; using System.Windows.Forms;  namespace MyNameSpace {    public class RSAcrypt   {     private string _encryptedData;     private string _decryptedData;      public string EncryptedData     {       get { return _encryptedData; }       set { _encryptedData  = value; }     }      public string DecryptedData     {       get { return _decryptedData; }       set { _decryptedData  = value; }     }      public RSAcrypt()     {     }     /// <param name='CryptAction'> The action to perform on the string {Encrypt|Decrypt} </param >     /// <param name='StringToCrypt'> A string to perform the Action on </param>     public RSAcrypt(string CryptAction, string StringToCrypt)     {         UnicodeEncoding thisUnicodeEncoding = new UnicodeEncoding();         RSACryptoServiceProvider thisRSACryptoServiceProvider = new RSACryptoServiceProvider();         byte[] _stringToCrypt = thisUnicodeEncoding.GetBytes(StringToCrypt);          switch (CryptAction)         {             case 'Encrypt':                 byte[] encryptedData = Encrypt(_stringToCrypt, thisRSACryptoServiceProvider.ExportParameters(false));                 _encryptedData = thisUnicodeEncoding.GetString(encryptedData);             break;              case 'Decrypt':                 byte[] decryptedData = Decrypt(_stringToCrypt, thisRSACryptoServiceProvider.ExportParameters(true));                 _decryptedData = thisUnicodeEncoding.GetString(decryptedData);             break;              default:              break;         }      }      static private byte[] Encrypt(byte[] DataToEncrypt, RSAParameters keyInfo)     {         RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();         RSA.ImportParameters(keyInfo);         return RSA.Encrypt(DataToEncrypt, false);     }      static private byte[] Decrypt(byte[] DataToDecrypt, RSAParameters keyInfo)     {       #region Temporary Assignment - Remove before build        byte[] tmpVal = null;        #endregion        RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();        try       {           RSA.ImportParameters(keyInfo);            #region Temporary Assignment - Remove before build            tmpVal = RSA.Decrypt(DataToDecrypt, false);            #endregion       }       catch (Exception ex)       {            MessageBox.Show('Error: ' + ex.Message, 'Exception Thrown');        }        #region Temporary Assignment - Remove before build        return tmpVal;        #endregion     }   } }  

Is there anything that I can change in this class that would allow me to check the encoding prior to passing the byte array to Encrypt / Decrypt?

It seems like I have a reference around here somewhere, but I am becoming frustrated, so I thought it would at least help if I stopped to do something other than reading and compiling…

BTW, I am calling this class to write to a password to an XML file using the Nini initialization framework. http://nini.sourceforge.net/manual.php#ASimpleExample

Also, I used Notepad2 to change the file encoding (UTF-8) before I wrote to the XML file.

That was after the program halted after I compiled the first time. Using the debugger, I was able to see that the encoding was different between the XML data in memory (UTF-8) and the data on disk (ANSI).

That does not appear to be the case now, but the program still halts, referencing bad data returned from the Decrypt portion of RSAcrypt().

(also note that Encrypt and Decrypt were identical methods before my frustration set in, they do function the same, but I wanted to try to capture addition exception information related to the bad data claim. Of course, you will notice that I allowed my frustration to handicap my code 😉 )

Any suggestions, ideas or references would be great.

TIA,

E

  • 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. 2026-05-11T09:56:42+00:00Added an answer on May 11, 2026 at 9:56 am

    Inside your constructor you generate a new RSA keypair each time when you do:

       RSACryptoServiceProvider thisRSACryptoServiceProvider = new RSACryptoServiceProvider(); 

    Since your constructor is where you encrypt and decrypt, you are encrypting with an RSA Key, and decrypting with a completely different one.

    To make this work, you have several options based on how you plan to use your code.

    One option is to export the RSA key, and use that for all encryption/decryption operations. This is the only option if you plan on decrypting/encrypting data between different runs of your executable.

    Of course this completely glosses over how you will store your public/private key (I recommend DPAPI on windows), for use by your application.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I've found the answer: no, tab cannot be highlighted in… May 12, 2026 at 9:22 pm
  • Editorial Team
    Editorial Team added an answer Is the desired touch behavior especially complex? If not, just… May 12, 2026 at 9:22 pm
  • Editorial Team
    Editorial Team added an answer Take a look at the Three20 library in particular the… May 12, 2026 at 9:21 pm

Related Questions

I am writing a video application in Java by executing ffmpeg and capturing its
Why is the Visual C++ compiler calling the wrong overload here? I am have
I'd like to serialize Python objects to and from the plist format (this can
I want my Rails 2.3.2 app to respond to and generate URLs like so:

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.