Note that there are some explanatory texts on larger screens.

plurals
  1. POSystem.Security.Cryptography and "Bad Data" - character encoding?
    primarykey
    data
    text
    <p>The class that produces "Bad Data" errors:</p> <pre> <code> 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() { } /// &lt;param name="CryptAction"&gt; The action to perform on the string {Encrypt|Decrypt} &lt;/param &gt; /// &lt;param name="StringToCrypt"&gt; A string to perform the Action on &lt;/param&gt; 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 } } } </code> </pre> <p><b> 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? </b></p> <p>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...</p> <p>BTW, I am calling this class to write to a password to an XML file using the Nini initialization framework. <a href="http://nini.sourceforge.net/manual.php#ASimpleExample" rel="nofollow noreferrer"><a href="http://nini.sourceforge.net/manual.php#ASimpleExample" rel="nofollow noreferrer">http://nini.sourceforge.net/manual.php#ASimpleExample</a></a></p> <p>Also, I used <a href="http://www.flos-freeware.ch/notepad2.html" rel="nofollow noreferrer">Notepad2</a> to change the file encoding (UTF-8) before I wrote to the XML file.</p> <p>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).</p> <p>That does not appear to be the case now, but the program still halts, referencing bad data returned from the Decrypt portion of RSAcrypt().</p> <p>(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 ;-) )</p> <p>Any suggestions, ideas or references would be great.</p> <p>TIA,</p> <p>E</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload