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)
1
  • 1
    The easiest way to get meaningful text from any binary sequence is to use a "hexstring", 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. Commented Jan 19, 2014 at 12:02

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.