Solved

Encrypting a string and the Euro € character

Posted on 2012-04-09
4
363 Views
Last Modified: 2012-04-12
I've inherited some code part of which is to encrypt a string, and then it is saved to a file. See below.

The problem is that when the EURO € (and I presume some other characters) are included in string the are changed to a question mark ?


DES.Key = MD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(key))
DES.Mode = CipherMode.ECB
Dim DESEncrypt As ICryptoTransform = DES.CreateEncryptor()
Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(stringToEncrypt)
Return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length))

I'd appreciate help on this as to how to avoid problem.  The fundamental of code can't really change as there are files already saved using this method

Thanks in advance
0
Comment
Question by:rwallacej
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37823980
The problem with the System.Text.ASCIIEncoding.ASCII.GetBytes(), because the EURO € is not defined in the ASCII encoding.  You should use System.Text.Encoding.UTF8.GetBytes() instead.  You probably need to update your decrypt code to use System.Text.Encoding.UTF8.GetString() also.
0
 

Author Comment

by:rwallacej
ID: 37824223
thanks for help,

I changed encrypt code per your comments to
            DES.Key = MD5.ComputeHash(Encoding.UTF8.GetBytes(key))
            DES.Mode = CipherMode.ECB
            Dim DESEncrypt As ICryptoTransform = DES.CreateEncryptor()
            Dim Buffer As Byte() = ASCIIEncoding.UTF32.GetBytes(stringToEncrypt)
            Return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length))

the encrypted string is the now different.



I changed the decrypt code from what it was to
                DES.Key = MD5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(key))
                DES.Mode = CipherMode.ECB
                Dim DESDecrypt As Security.Cryptography.ICryptoTransform = DES.CreateDecryptor()
                Dim Buffer As Byte() = Convert.FromBase64String(encryptedString)
                Return ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length))

but this does not decrypt string right

further help appreciated, thanks
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 37824259
Your code is still has ASCII encoding references.  Please try this:

Encrypt:
DES.Key = MD5.ComputeHash(Encoding.UTF8.GetBytes(key))
DES.Mode = CipherMode.ECB
Dim DESEncrypt As ICryptoTransform = DES.CreateEncryptor()
Dim Buffer As Byte() = Encoding.UTF8.GetBytes(stringToEncrypt)
Return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length))

Open in new window

Decrypt:
DES.Key = MD5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(key))
DES.Mode = CipherMode.ECB
Dim DESDecrypt As Security.Cryptography.ICryptoTransform = DES.CreateDecryptor()
Dim Buffer As Byte() = Convert.FromBase64String(encryptedString)
Return Encoding.UTF8.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length))

Open in new window

0
 

Author Closing Comment

by:rwallacej
ID: 37838965
Thanks
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question