|
Thats a huge help. Thank you.
oooo, the Jedi's will feel this one....
|
|
|
|
|
|
Hi All!!
In code below, i'm encrypting bitmap(24-bit) image, accessed from ms-access database. Here is my code:
private void button1_Click(object sender, System.EventArgs e)
{
string connString = "provider=Microsoft.Jet.OLEDB.4.0;" + @"data
source=D:\\practiceproject\practice.mdb";
OleDbConnection connection = new OleDbConnection(connString);
string commandString = "Select Image from readImage where ID=1";
OleDbCommand cmd = new OleDbCommand(commandString,connection);
byte[] toEncrypt = new byte[120154];
connection.Open();
toEncrypt = (Byte[]) cmd.ExecuteScalar();
connection.Close();
RSAParameters rsaPubParams;
RSAParameters rsaPrivateParams;
RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();
rsaPrivateParams = rsaCSP.ExportParameters(true);
rsaPubParams = rsaCSP.ExportParameters(false);
rsaCSP.ImportParameters(rsaPubParams);
byte[] encrypted = new byte[130000];
encrypted = rsaCSP.Encrypt(toEncrypt, false);
}
But i get
Bad Length error at line
encrypted = rsaCSP.Encrypt(toEncrypt, false);
Please help me to resolve this problem!!!
modified on Thursday, March 19, 2009 11:35 AM
|
|
|
|
|
the way you've posted your code makes it very hard to read... please unbold and use a code block then people will look at it more and not jump it.
|
|
|
|
|
I have not done anything with encryption for a long time. So this is not guaranteed to work.
I think your problem is in the declaration of encrypted .
Instead of:
byte[] encrypted = new byte[130000];
encrypted = rsaCSP.Encrypt(toEncrypt, false);
try:
byte[] encrypted = rsaCSP.Encrypt(toEncrypt, false);
After all you do not know in advance the length of the byte-array thet encrypt will return, so why set a length in the declaration.
Good luck!
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
I tried that but again
Bad Length error is at:
byte[] encrypted = rsaCSP.Encrypt(toEncrypt, false);
Please help me out...
|
|
|
|
|
OK.
After a little research. I think the problem might lie in the length of the input.
From a little reading it seems that RSACryptoServiceProvider was designed for encrypting cryptographic keys, it gets a bit upset if you ask it to do too much work. A bit like me.
There is quite a bit on the web about this problem, and I would suggest that you do a little more research. I Googled "RSACryptoServiceProvider Bad Length", and if you do the same, you might find a solution, but I doubt it.
Choose another CryptoServiceProvider , if you are allowed to. I think that will make your life a whole lot easier.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Hi Henry!!
Thanks for your help...I searched that too...None of the article contains image encryption through RSACryptoserviceprovider. other CryptoServiceProviders are used...
|
|
|
|
|
Hi
can i convert a string to integer variable in C#?If so can you please tell me.I have given the port number <port>10000<port> in an xml file .I want to read this number from xml and assign to int variable so that it can be passed to the IPEndPoint() for client/server commnuication.Whatever i read from xml is retrieved as string.I tried with ConvertToInt() but still im not getting.Can you please explain me the reason?
|
|
|
|
|
int result;
int.TryParse("10000", out result);
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
what's the difference between the method you've used and
result = int.parse("100000");
|
|
|
|
|
|
The difference is that if you pass a string to int.parse() that is not a valid string (contains noon-numeric characters) it will throw an exception that you will need to manually handle.
If you use my method the error is handled automatically and the result is set to 0, its up to you if you want to handle the error yourself thou, depends how much validation control you need.
if you are expecting the result could sometimes be 0 as a result then you may be best to handle the error yourself. but in the case of a port number you shouldnt need to do this, just check if result is 0 for an error.
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Thank you for the reply... next time I convert I'll bare it in mind.
|
|
|
|
|
musefan wrote: just check if result is 0 for an error
Better to assign and check the boolean return value of the TryParse method before working with the value. Still preferable to catching an exception!
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Ahh yes, i forgot TryParse() returns a bool
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
If the input string is not a valid integer, int.Parse throws an exception, while int.TryParse returns false without setting result value.
e.g.
string wrong = "xyz";
result = int.Parse(wrong);
string wrong = "xyz";
int result = -1;
bool success = int.TryParse(wrong, out result);
|
|
|
|
|
Thanks for the reply, however according to the above post the result would be set to zero and not stay at -1 or is it only set to zero if there is no valid number in it?
|
|
|
|
|
Yes, I was wrong, int.TryParse puts 0 in the result if the parsing fails.
For some reason, I tought that the resul value was unchanged in case of failure, my bad.
|
|
|
|
|
No worries thanks for the clarification
|
|
|
|
|
hopingToCode wrote: only set to zero if there is no valid number
... or if the number in the text is zero! Check the return value of the TryParse method to determine if successful.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
musefan has given you the perfect answer. Just be aware that if the number can't be converted to an int you will get zero.
I have an extension method that I use when working with text/xml etc files to simplify stuff.
public static class ExtensionMethods
{
public static Int32 ToInt32(this string value)
{
int result;
Int32.TryParse(value, out result);
return result;
}
} Now I can can just use something like
int intFromFile = stringFromFile.ToInt32();
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
If I might...
Is it considered really bad practice to do Convert.ToInt32 ?
I have seen stuff (and well, I have used it, too) like:
string x = "10";
int xInt;
try
{
xInt = Convert.ToInt32(x);
}
catch
{
xInt = 0;
}
Would that not do the same as TryParse?
Or is it an unnecessary way around it, or simply, crap?
var question = (_2b || !(_2b));
|
|
|
|
|
Not bad practice, possibly a little slower going via the Convert class but I would have to check the implementation to be sure.
The convert class is great for converting from a built in type to another (not just from string), but I would guess the Int32.Parse or Int32.TryParse methods would be perhaps a little more efficient.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
How can i automatically increments the build number in an AssemblyInfo.cs?
Any example? using macro or function method?
Kelvin
|
|
|
|