473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

DESCryptoServiceProvider used to encrypt a 64 length string, funny output

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
2 9165

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: MLH | last post by:
I'm working with lots of long strings now, it seems. I have to import them & parse them constantly. The A97 memo field type supports only 32768 chars. What happens when this is processed... Dim...
0
by: Salman | last post by:
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...
7
by: Matthias S. | last post by:
Hi, I had a look at the vast information on encryption in the MSDN and got pretty confused. All I want to do is to encrypt a string into an encrypted string and later decrypt that (encrypted)...
8
by: toupeira23 | last post by:
Hello, I'm trying to encrypt passwords in my app. After discovering that there's no simple function to do this, I wrote a wrapper class which decodes a string using UTF8, encrypts it with...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
1
by: Jon Paal | last post by:
need help with code to encrypt a string and write it out to a file. code does not write out to file =========code============== Private Shared ReadOnly encryptionKeyBytes() As Byte = New...
1
by: Sathyaish | last post by:
I have the following scenario: Algorithm: 3DES Cipher Mode: CBC Key Size: 128-bit Block Size: 64 bit IV: 0x0000000000000000 (an eight byte array of zeros) The results I get using .NET with...
6
by: nickyeng | last post by:
i have a midlet which will generate a string (message ) and send to our gateway. but before sending, i will need to encrypt the string. i've checked anywhere in internet but couldn't find j2me...
1
Prakash Gnana
by: Prakash Gnana | last post by:
Is it possible to encrypt a string in php? Is yes, then how to encrypt a string in php?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.