2

I am implementing my own AES code and I am encountering some problems during the decryption.

byte[] output;
output = Encrypt(EncryptBufferInput);//encrypt "12",output[] is 300532188151293E4ACA3BA529B821C1
str.Append(Encoding.ASCII.GetString(output) );

output = Decrypt(DecryptBufferInput);//in hex, DecryptBufferInput should be "300532188151293E4ACA3BA529B821C1"
str.Append(Encoding.ASCII.GetString(output));//does not decrypt back to "12"

So, if I try to encrypt "12", it gives me a value of "300532188151293E4ACA3BA529B821C1" in hex form. when I try to decrypt it back to "12", it gives me a wrong value because the DecryptBufferInput is not "300532188151293E4ACA3BA529B821C1" but some other value...Only the first few values are same. EncryptBufferInput and DecryptBufferInput are both byte[] arrays, and I use Encoding.ASCII.GetBytes(string) to fill the byte array with the corresponding string(string to encrypt or string to decrypt). What am I doing wrong??

1
  • What are the type of EncryptBufferInput and DecryptBufferInput? What are the signature of Encrypt and Decrypt? Where does the value of DecryptBufferInput come from? Please provide more information. Your code seems to have some flaws, but we cannot guess anything, unless you tell us how are you doing things. Commented Feb 27, 2013 at 9:34

2 Answers 2

5

The binary you get from Encrypt does not represent text data, so you should not use Encoding. Use Convert.ToBase64String instead, and Convert.FromBase64String to reverse it. See also.

In the decode step, you need to reverse the order; so: if you are taking string input, you will need:

to encrypt:

  • start with string: "12"
  • use Encoding (preferably Encoding.UTF8) to get a byte[] to encrypt
  • use Encrypt with the unencrypted byte[] to get the encrypted byte[]
  • use Convert.ToBase64String to represent that as a string if you need
  • end with encrypted string

to decrypt:

  • start with encrypted string
  • use Convert.FromBase64String to get the encrypted byte[]
  • use Decrypt with the encrypted byte[] to get the unencrypted byte[]
  • use the same Encoding to get the string
  • end with string: "12"
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the detailed solution! I am going to try it right away!
It is decrypting to the right value. Thank you @Marc Gravell
@user1109363 tiny.cc/io has more info on when to use Encoding vs when to use base-n
0

Where do you set EncryptBufferInput and DecryptBufferInput? I presume that your Encrypt and Decrypt methods both take strings, so I'd try this:

byte[] output = Encrypt(EncryptBufferInput);
string encryptedOutput = Convert.ToBas64String(output);
str.Append(encryptedOutput);

byte[] decrypted = Decrypt(Convert.FromBase64String(encryptedOutput));
string decryptedOutput = Encoding.ASCII.GetString(decrypted);
str.Append(decryptedOutput);

In case they take byte[]:

byte[] output = Encrypt(EncryptBufferInput);
string encryptedOutput = Convert.ToBase64String(output);
str.Append(encryptedOutput);

byte[] decrypted = Decrypt(output);
string decryptedOutput = Encoding.ASCII.GetString(decrypted);
str.Append(decryptedOutput);

And please note: ASCII and Thai, German, Russian, Greek (and many others) don't play together very well - use Unicode or at least UTF8.

2 Comments

Using ASCII to turn encrypted binary into a string is simply wrong: that is using an encoding entirely backwards; the binary here does not represent text data
You are right - I missed that. Of course one should convert it to BASE64 before. I'll fix my samples.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.