DESCryptoServiceProvider used to encrypt a 64 length string, funny output

S

Salman

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

sr.Encrypt("1234567890123456789012345678901234567890123456789012345678904444");

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
 
B

Bret Mulvey [MS]

Salman said:
When I run the below method with a 64 character string as input such
as:

sr.Encrypt("1234567890123456789012345678901234567890123456789012345678904444
");

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.
 
Top