Question

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)
Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top