0

I have a function which try to decyrpt a byte array in C#.

Decryption function:

string Decrypt(byte[] cipherText, byte[] Key, byte[] IV)
    {
        string plaintext = null;
        // Create AesManaged    
        using (AesManaged aes = new AesManaged())
        {
            aes.Mode = CipherMode.ECB;
            aes.Padding = PaddingMode.None;
            // Create a decryptor    
            ICryptoTransform decryptor = aes.CreateDecryptor(Key, IV);
            // Create the streams used for decryption.    
            using (MemoryStream ms = new MemoryStream(cipherText))
            {
                // Create crypto stream    
                using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                {
                    // Read crypto stream    
                    using (StreamReader reader = new StreamReader(cs))
                        plaintext = reader.ReadToEnd();
                }
            }
        }
        return plaintext;
    }

Below is how I implement the function:

byte[] app_key = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
byte[] data    = { 0xB6 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x73 ,0x69 ,0x2D ,0x01 ,0x00 ,0x01 ,0x18 ,0x4F ,0x84 ,0xE8 };
var sts = Decrypt(data, app_key, new byte[16]);
Console.WriteLine(BitConverter.ToString(Encoding.ASCII.GetBytes(sts)));

But the output is 3F-3F-2F-31-3F-3F-3F-5E-61-2A-21-42-3F-2B which is wrong!!!
The correct output is d4-f7-2f-31-a3-a8-d4-a3-5e-61-2a-21-42-88-2b-dc.

Online Decryptor Link: http://aes.online-domain-tools.com/run/?inputType=frm-text&text=B6-00-00-00-00-00-73-69-2D-01-00-01-18-4F-84-E8&text_type=hex&function=aes&mode=ecb&key=01010101010101010101010101010101&key_type=hex&do=form-submit&decrypt=do

What am I doing wrong here? Is there any other AES128 decryption function?

Thanks...

Akif Çelebi
  • 41
  • 2
  • 8
  • 3
    You’re processing a Unicode string in .NET, not raw bytes - which will throw everything off. – Dai Aug 15 '20 at 11:25
  • 2
    The issue was already identified in the previous comment. Your code works as long as the binary plaintext can be decoded with UTF8. But this is not the case with the posted plaintext `d4f7...`. Therefore you have to replace the `StreamReader` (which uses UTF8 encoding by default) e.g. with a `MemoryStream msout` (so that no encoding is applied), which can most easily be filled with `cs.CopyTo(msout)` and converted into a `byte[]` with `msout.ToArray()` (and from there if desired into a string e.g. with `BitConverter.ToString(...)`). – Topaco Aug 15 '20 at 15:52

1 Answers1

0

Try this code; This is a part of my code

AES Decryption

public string AESDecrypt(string cryptText){

   // Seed and construct the transformation used for decrypting
   AESCryptoAlgorithm.GenerateIV();
   AESCryptoAlgorithm.GenerateIV();
   byte[] iv = AESCryptoAlgorithm.IV;
   int ivSize = iv.Length;
   ICryptoTransform decryptor = AESCryptoAlgorithm.CreateDecryptor(AESKey, iv);

   // The crypt text is expected to be encoded in base64 format, decode it...
   byte[] cryptBytes = Convert.FromBase64String(cryptText);
   byte[] cryptBytes = Convert.FromBase64String(cryptText);
   byte[] clearBytes = decryptor.TransformFinalBlock(cryptBytes, 0, cryptBytes.Length);

   return Encoding.ASCII.GetString(clearBytes, ivSize, clearBytes.Length - ivSize).TrimEnd('\0');
}

AESAlgorithm

protected Rijndael AESCryptoAlgorithm {
   get
   {
      if(_AESCryptoAlogrithm == null){
            _AESCryptoAlogrithm = Rijndael.Create();
            _AESCryptoAlogrithm.BlockSize = 128;
            _AESCryptoAlogrithm.Mode = CipherMode.CBC;
            _AESCryptoAlogrithm.Padding = PaddingMode.Zeros;
      }

      return _AESCryptoAlogrithm;
   }
}

HEX Decode

public static byte[] HexDecode(string hex){
   int len = hex.Length;
   byte[] bytes = new byte[len / 2];
   for (int i = 0; i < len; i += 2)
        bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
   return bytes;
}
Ilnam Jeong
  • 401
  • 2
  • 6