rwallacej
asked on
Encrypting a string and the Euro € character
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.Tex t.ASCIIEnc oding.ASCI I.GetBytes (key))
DES.Mode = CipherMode.ECB
Dim DESEncrypt As ICryptoTransform = DES.CreateEncryptor()
Dim Buffer As Byte() = System.Text.ASCIIEncoding. ASCII.GetB ytes(strin gToEncrypt )
Return Convert.ToBase64String(DES Encrypt.Tr ansformFin alBlock(Bu ffer, 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
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.Tex
DES.Mode = CipherMode.ECB
Dim DESEncrypt As ICryptoTransform = DES.CreateEncryptor()
Dim Buffer As Byte() = System.Text.ASCIIEncoding.
Return Convert.ToBase64String(DES
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
The problem with the System.Text.ASCIIEncoding. ASCII.GetB ytes(), 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.
ASKER
thanks for help,
I changed encrypt code per your comments to
DES.Key = MD5.ComputeHash(Encoding.U TF8.GetByt es(key))
DES.Mode = CipherMode.ECB
Dim DESEncrypt As ICryptoTransform = DES.CreateEncryptor()
Dim Buffer As Byte() = ASCIIEncoding.UTF32.GetByt es(stringT oEncrypt)
Return Convert.ToBase64String(DES Encrypt.Tr ansformFin alBlock(Bu ffer, 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.Tex t.Encoding .UTF8.GetB ytes(key))
DES.Mode = CipherMode.ECB
Dim DESDecrypt As Security.Cryptography.ICry ptoTransfo rm = DES.CreateDecryptor()
Dim Buffer As Byte() = Convert.FromBase64String(e ncryptedSt ring)
Return ASCIIEncoding.ASCII.GetStr ing(DESDec rypt.Trans formFinalB lock(Buffe r, 0, Buffer.Length))
but this does not decrypt string right
further help appreciated, thanks
I changed encrypt code per your comments to
DES.Key = MD5.ComputeHash(Encoding.U
DES.Mode = CipherMode.ECB
Dim DESEncrypt As ICryptoTransform = DES.CreateEncryptor()
Dim Buffer As Byte() = ASCIIEncoding.UTF32.GetByt
Return Convert.ToBase64String(DES
the encrypted string is the now different.
I changed the decrypt code from what it was to
DES.Key = MD5.ComputeHash(System.Tex
DES.Mode = CipherMode.ECB
Dim DESDecrypt As Security.Cryptography.ICry
Dim Buffer As Byte() = Convert.FromBase64String(e
Return ASCIIEncoding.ASCII.GetStr
but this does not decrypt string right
further help appreciated, thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks