|
Hi-
I have a c# program which encrypts a password and stores it in an access database (into a text field with 150 char). When I try to decrypt it, it tells me "bad data".
Here is how I encrypt it:
//Create a UnicodeEncoder to convert between byte array and string.
UnicodeEncoding ByteConverter = new UnicodeEncoding();
//Create byte arrays to hold original, encrypted, and decrypted data.
byte[] dataToEncrypt = ByteConverter.GetBytes(this.editHeartsPsWd.Text.ToString());
byte[] encryptedData;
//Create a new instance of RSACryptoServiceProvider to generate
//public and private key data.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//Pass the data to ENCRYPT, the public key information
//(using RSACryptoServiceProvider.ExportParameters(false),
//and a boolean flag specifying no OAEP padding.
encryptedData = HallenSchool.HallenFunctions.RSAEncrypt(dataToEncrypt,RSA.ExportParameters(false), false);
string sSQL="UPDATE Staff SET Staff.HeartsPsWd = '" + ByteConverter.GetString(encryptedData) + "' WHERE Staff.[Staff Code]= " + this.editStaff_Code.Text.ToString();
Here is how I decrypt it:
//Create a UnicodeEncoder to convert between byte array and string.
UnicodeEncoding ByteConverter = new UnicodeEncoding();
//Create a new instance of RSACryptoServiceProvider to generate
//public and private key data.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
byte[] decryptedData;
byte[] encryptedData;
string strHeartsPsWd = this.editHeartsPsWd.Text.ToString();
strHeartsPsWd = strHeartsPsWd.Trim();
encryptedData=ByteConverter.GetBytes(strHeartsPsWd);
if (!(encryptedData.Length==0))
{
decryptedData = HallenSchool.HallenFunctions.RSADecrypt(encryptedData,RSA.ExportParameters(true), false);
this.editHeartsPsWd.Text= ByteConverter.GetString(decryptedData);
}// not empty password
Thanks for your help!
JK
|
|
|
|
|
First, don't use Text.ToString : Text already returns a string - your call to ToString is completely unnecessary.
Second, you're treating binary data as text, which can cause encoding problems. If you want to encode binary data as text, then you should chain something like the ToBase64Transform or FromBase64Transform or simply use Convert.ToBase64String or Convert.FromBase64String .
Finally, the problem is most likely in your RSADecrypt method that you're not displaying. Everything you're doing besides that is simply encoding and decoding and should not throw such an exception that uses the "Bad data" message.
Please be more specific. What exception is being thrown and from which line? Step through your code and determine exactly where the error occurs.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi -
The RSADecrypt is:
static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo,bool DoOAEPPadding)
{
try
{
//Create a new instance of RSACryptoServiceProvider.
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);
}
//Catch and display a CryptographicException
//to the console.
catch(CryptographicException e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
return null;
}
} //end RSADecrypt
***** The exception is thrown at the return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
statement.
Thanks for your help!
JK
JK
|
|
|
|
|
Like I mentioned before, it's probably because you're treating binary data as text. Try just storing the encrypted data in a byte[] array and then decrypt that, as opposed to putting it in a TextBox (or whatever you are storing it in) and decrypt that and see what happens.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I have class like this
Public class A
{
private string stra1;
public string geta1 {
get{ return stra1;}
}
public void seta1{string sa1)
{
stra1=sa1;
}
public a{
//contructor
}
//Inner class
public class b
{
private string strb1;
public string getb1 {
get{ return strb1;}
}
public void setb1{string sb1)
{
strb1=sb1;
}
public b{
//contructor
}
}
How can i access the inner class now??
|
|
|
|
|
From inside class A , just instantiate B . From outside class A , you can instantiate A.B .
FYI, class B can access private members of class A , and instantiating class A does not instantiate class B .
You typically nest classes to either show dependency of the nested class on the outer class or to mark the nested class as private so that only the outer class can access it (it still needs a public or internal constructor, though).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Consider the following code...
public class Outer
{
public Outer() { }
<br>
public class Inner
{
public Inner() { }
}
}
The Inner class can be accessed as follows...
Outer.Inner i = new Outer.Inner();
|
|
|
|
|
A small annotation that doesn't deal with your question.
Instead of the public get- and set-methods, use properties to offer access to your private class members.
<br />
private string stra1;<br />
public string Stra1<br />
{<br />
get<br />
{<br />
return this.stra1;<br />
}<br />
set<br />
{<br />
this.stra1 = value;<br />
}<br />
}<br />
Afterwards you can access the property like a public variable.
<br />
A obj = new A();<br />
obj.Stra1 = "string";<br />
string x = obj.Stra1;
|
|
|
|
|
Urgent.. need a sw that cuts video into images and compatible with .NET
or
access the video from the program using C#.net then convert the frames into images
count the frames in the video
set a index for each image
|
|
|
|
|
Hi
Well, my question today is how I could use a dataset into a PivotTable??
I try to use a PivotTable v10 into my C# project, and I wanna bind it from a dataset, but I don't know how. The PivotTable just can be binded from a DataSource
If someone work with PivotTable in C#, please help me.
Thanks
----
hxxbin
|
|
|
|
|
It must bind against an ADO data source, not an ADO.NET data source. In order to do this, you must import the ADO typelib. If you have installed VS.NET, it is recommended that you use the Primary Interop Assembly (PIA) created by Microsoft, adodb.dll. This is found by default in Program Files\Microsoft.NET\Primary Interop Assemblies.
You use ADO much like you use ADO.NET: you create connections and commands and execute that command to get back an ADO recordset, which you can then set as the PivotTable.DataSource property value.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks, I better try to use ADO , I don't know how use PIA It's hard to use PIA??
----
hxxbin
|
|
|
|
|
Just add a reference to it and access the classes just like you do with any other assembly.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hey,
I need an example of an application written in C#, using Winforms, and especially separating the three classic layers (Data Access Layer, Business Logic layer, User Interface Layer)..
If you know any please send the link..
It would be of GREAT help to me..
Thank you!
|
|
|
|
|
|
|
this is the code i used
PrintDocument pd=new PrintDocument();
pd.DefaultPageSettings.PaperSize= // don't know what to put here
chad
|
|
|
|
|
Set it to a new instance of the PaperSize , like so:
PrintDocument pd = new PrintDocument();
pd.DefaultPageSettings.PaperSize = new PaperSize("Legal", 8500, 14000); You can also get supported paper sizes from the printer by getting the PrinterSettings in a number of ways and then enumerating through the PaperSizes collection property.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
ok here is my code listing but it dose not print on legal paper is sayes manual feed plain letter on my printer
public void printpages()
{
pages=cnt;
pd.PrintPage += new inting.PrintPageEventHandler .printDocument1_PrintPage);
pd.DocumentName = "Imaging";
pd.DefaultPageSettings.PaperSize = new PaperSize("Legal", 8500, 14000);
if (pd.PrinterSettings.IsValid)
{
pd.Print();
}
private void printDocument1_PrintPage(object sender,System.Drawing.Printing.PrintPageEventArgs e)
{
System.IO.FileStream fs ; //create a filestream to open the image
fs= new FileStream("c:\\temp\\test.tiff",System.IO.FileMode.Open,System.IO.FileAccess.Read);
Image newImage = Image.FromStream(fs);
Rectangle destRect = new Rectangle( 1, 1,1000,1000);
e.Graphics.DrawImage(newImage, destRect);
fs.Close();
}
chad
|
|
|
|
|
Does your printer even support legal paper? As I mentioned before, get the PrinterSettings for your paper and enumerate the PaperSizes collection property.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I'm still finding my feet in the new .NET world, wanting to learn the best practises rather than just using old C++ techniques.
I'm looking for the best way for displaying a list of data records and allow double-click to launch a seperate form, passing in the record that has been selected.
In MFC, I use a ListViewCtrl and fill it with data from a recordset.
What is the best way to do this in C#.
A data-bound ListView control seemed the best way, but that doesn't seem possible in C#. Or is the DataGrid the way it should be done in C#.
I only want to display the data in a columned list, with no editing - possibly may want to have a different background colour on individual rows.
Michael
But you know when the truth is told,
That you can get what you want or you can just get old,
Your're going to kick off before you even get halfway through.
When will you realise... Vienna waits for you? - "The Stranger," Billy Joel
|
|
|
|
|
You can always data-bind the ListView though there are no properties or functionality to do this for you like there is for the DataGrid . You can make either one work, however.
With the DataGrid , you could handle the DoubleClick event. In that handler, get the CurrentRowIndex and use that to get the DataRow in your data source. If you bind against a DataView instead, be sure to get the DataRowView with that index in order to get the row. Then just invoke whatever action you want using the data from that row.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath Stewart wrote:
You can always data-bind the ListView though there are no properties or functionality to do this for you like there is for the DataGrid. You can make either one work, however.
Thanks,
Being a lazy kind of guy, I've been looking for a ListView control that already has the data-binding code implemented. Beyond a VB.NET sample on MSDN (and a poor conversion to C# here at CP), I haven't found anything.
As you seem to be the oracle when it comes to all things C#, do you know of any code that already implements data-binding in a ListView.
I'm probably going to have to write it myself, but I can't believe nobody has done it already. (I'm equally surprised that MS didn't do it either)
Michael
But you know when the truth is told,
That you can get what you want or you can just get old,
Your're going to kick off before you even get halfway through.
When will you realise... Vienna waits for you? - "The Stranger," Billy Joel
|
|
|
|
|
Microsoft is doing it for .NET 2.0. They're adding data-binding to the ListView (among other things, like virtual lists and grouping) and introducing an all-new control called the DataView , which is much more powerful and flexible than the DataGrid . In fact, IIRC, the DataGrid isn't even added to VS.NET 2005's toolbox by default.
There's no easy way to bind the ListView , but you can use the same tactics as the DataGrid . If the DataSource property you'd add to your own ListView derivative implements IListSource , you get the IList . If it implements IList , you simply use that. Then just enumerate that and do a little checking to see if you're enumerating simple objects or DataRow s. You can use that to bind columns.
You could go a step further (albeit a pretty big step) and create your own column handlers that allow you to specify mapping names. The best way to see how this works is to use ildasm.exe from the .NET Framework SDK to see how the DataGrid works.
Consider this, though: after you've finished you're essentially created a DataGrid . You may as well use that until you can upgrade your codebase to .NET 2.0 which should go beta later this summer presumably since it's slated to be released in Q1 or Q2 of 2005.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath Stewart wrote:
Consider this, though: after you've finished you're essentially created a DataGrid.
A very good point.
Thanks for your advice, very educational. I'll take the DataGrid route for now, it should do what I need.
Michael
But you know when the truth is told,
That you can get what you want or you can just get old,
Your're going to kick off before you even get halfway through.
When will you realise... Vienna waits for you? - "The Stranger," Billy Joel
|
|
|
|
|