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??

coffeeak
  • 2,980
  • 7
  • 44
  • 87
  • 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. – Mohammad Dehghan Feb 27 '13 at 09:34

2 Answers2

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"
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
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.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • 2
    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* – Marc Gravell Feb 27 '13 at 09:37
  • You are right - I missed that. Of course one should convert it to BASE64 before. I'll fix my samples. – Thorsten Dittmar Feb 27 '13 at 09:45