If you are using one of .NET CryptoService providers (like DESCryptoServiceProvider or RijndaelManaged), there's a trap that you can easily fall in (I did).
To encrypt data (a string converted previously into a byte array), you create a CryptoStream that reads bytes from this array, transforms them, and then writes encrypted data to a stream.
Then you can use ToArray() method of the stream to obtain encrypted bytes.
And what do you do next?
You run something like:
Encoding.Unicode.GetString(cipherBytes)
And you have a problem. The encrypted string is encoded in Unicode. That's fine as long as you want to write it into a file or store in a database, but sometimes you want to be able to enter it using the keyboard.
To solve this problem use:
Convert.ToBase64String(cipherBytes)
This way the encrypted string will be encoded with ASCII characters only.
If you want to know more, read this article.
No comments:
Post a Comment