DESCryptoServiceProvider used to encrypt a 64 length string, funny output

  • Thread starter Thread starter Salman
  • Start date Start date
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
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top