1

I am building a C# application that encrypt text and display the result in a text box using System.Security.Cryptography.Rijndael so the receiver copy the results and decrypt it using the same key.

The problem is happening when I am converting the encrypted text from byte[] returned by EncryptStringToBytes function to a string that can be displayed in a textBox in order to be copied and decrypted later.

I have used the below conversion methods but none of them where able to display a meaningful string that can be used later for decryption and can be re-converted to the ORIGINAL byte shape returned previously from EncryptStringToBytes.

Below are the methods used to perform the conversions:

Converting from byte[] to string in order to be displayed in the textBox:

textBox3.Text = Encoding.Default.GetString(encryptionResult)

Converting from string copied from the textBox to byte[] in order to be sent as argument to DecryptStringFromBytes in order to complete the decrytion process:

byte[] textToByte = Encoding.Default.GetBytes(textToDecrypt)
Ihab Agha
  • 137
  • 1
  • 6
  • 1
    The easiest way to get meaningful text from any binary sequence is to use a ["hexstring"](http://stackoverflow.com/questions/623104/byte-to-hex-string), i.e. "87160a20a03daec4adc1934" and similar. If you use an encoder to get actual text, you'll run into problems with binary sequences that cannot easily be represented. – bzlm Jan 19 '14 at 12:02

1 Answers1

6

You should try encode and decode in 64encoding. Use Convert Class.

textBox3.Text =Convert.ToBase64String(encryptionResult)

byte[] textToByte = Convert.FromBase64String(textToDecrypt);

If you still want to use encode, choose:

string decodedString = Encoding.UTF8.GetString(textToDecrypt);
BestR
  • 669
  • 2
  • 6
  • 17