By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,543 Members | 2,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,543 IT Pros & Developers. It's quick & easy.

DESCryptoServiceProvider used to encrypt a 64 length string, funny output

P: n/a
When I run the below method with a 64 character string as input such
as:

sr.Encrypt("12345678901234567890123456789012345678 90123456789012345678904444");

I get square boxes as my output, which means that some characters are
not being mapped in the encodings. What could be the problem?

Since my output is funny characters, my decrption will as a result
fail since it contiains invalid characters when I do a
FromBase64String.

TIA
(Code Below)
************************************************** ***
public string TestEncrypt(string StrintToEncrpt)
{
string strOutput;

UTF8Encoding uTF8Encoding = new UTF8Encoding();
DESCryptoServiceProvider dESCryptoServiceProvider = new
DESCryptoServiceProvider();

dESCryptoServiceProvider.Key = uTF8Encoding.GetBytes("12345678");
dESCryptoServiceProvider.IV = dESCryptoServiceProvider.Key;

ICryptoTransform iCryptoTransform =
dESCryptoServiceProvider.CreateEncryptor();

byte[] byte1 = Convert.FromBase64String(StrintToEncrpt);

byte[] byte2 = iCryptoTransform.TransformFinalBlock(byte1, 0,
byte1.GetLength(0));

strOutput = Encoding.ASCII.GetString(byte2);
return strOutput;
}

http://www.csharpfriends.com
~Salman
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Salman" <ad***@csharpfriends.com> wrote in message
news:f6*************************@posting.google.co m...
When I run the below method with a 64 character string as input such
as:

sr.Encrypt("12345678901234567890123456789012345678 90123456789012345678904444
");
I get square boxes as my output, which means that some characters are
not being mapped in the encodings. What could be the problem?

Since my output is funny characters, my decrption will as a result
fail since it contiains invalid characters when I do a
FromBase64String.

TIA
(Code Below)
************************************************** ***
public string TestEncrypt(string StrintToEncrpt)
{
string strOutput;

UTF8Encoding uTF8Encoding = new UTF8Encoding();
DESCryptoServiceProvider dESCryptoServiceProvider = new
DESCryptoServiceProvider();

dESCryptoServiceProvider.Key = uTF8Encoding.GetBytes("12345678");
dESCryptoServiceProvider.IV = dESCryptoServiceProvider.Key;

ICryptoTransform iCryptoTransform =
dESCryptoServiceProvider.CreateEncryptor();

byte[] byte1 = Convert.FromBase64String(StrintToEncrpt);

byte[] byte2 = iCryptoTransform.TransformFinalBlock(byte1, 0,
byte1.GetLength(0));

strOutput = Encoding.ASCII.GetString(byte2);
return strOutput;
}

http://www.csharpfriends.com
~Salman


You're trying to convert your byte2 array into ASCII even though it contains
non-ASCII code values. If you want to store the output of an arbitrary array
of bytes as an ASCII string you'll need to use Convert.ToBase64String() or
some other method.
Nov 15 '05 #2

P: n/a

"Salman" <ad***@csharpfriends.com> wrote in message
news:f6*************************@posting.google.co m...
When I run the below method with a 64 character string as input such
as:

sr.Encrypt("12345678901234567890123456789012345678 90123456789012345678904444
");
I get square boxes as my output, which means that some characters are
not being mapped in the encodings. What could be the problem?

Since my output is funny characters, my decrption will as a result
fail since it contiains invalid characters when I do a
FromBase64String.

TIA
(Code Below)
************************************************** ***
public string TestEncrypt(string StrintToEncrpt)
{
string strOutput;

UTF8Encoding uTF8Encoding = new UTF8Encoding();
DESCryptoServiceProvider dESCryptoServiceProvider = new
DESCryptoServiceProvider();

dESCryptoServiceProvider.Key = uTF8Encoding.GetBytes("12345678");
dESCryptoServiceProvider.IV = dESCryptoServiceProvider.Key;

ICryptoTransform iCryptoTransform =
dESCryptoServiceProvider.CreateEncryptor();

byte[] byte1 = Convert.FromBase64String(StrintToEncrpt);

byte[] byte2 = iCryptoTransform.TransformFinalBlock(byte1, 0,
byte1.GetLength(0));

strOutput = Encoding.ASCII.GetString(byte2);
return strOutput;
}

http://www.csharpfriends.com
~Salman


You're trying to convert your byte2 array into ASCII even though it contains
non-ASCII code values. If you want to store the output of an arbitrary array
of bytes as an ASCII string you'll need to use Convert.ToBase64String() or
some other method.
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.