-2

This is the code -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace AESFileDecrypt
{
    class Program
    {
        static void Main(string[] args)
        {
            RijndaelManaged aes = new RijndaelManaged();
            aes.Key = Encoding.ASCII.GetBytes("12345678");
            aes.IV = new byte[0x10];
            aes.Padding = PaddingMode.None;
            aes.Mode = CipherMode.ECB;
            ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
            byte[] data = File.ReadAllBytes(@"C:\enc.aes");
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
                {                
                    cs.Write(data, 0, data.Length);
                    cs.FlushFinalBlock();
                    File.WriteAllBytes(@"C:\dec.txt", ms.ToArray());
                }
            }
        }
    }
}

Two errors that I face -

1 - "Length of the data to decrypt is invalid"

This happens at the FlushFinalBlock I guess.

2 - "'System.OutOfMemoryException' "

This happens for large files.

I have read through many pages similar but could not fix it for such a decryption.

Any help ?

Regards

madskillz
  • 1
  • 1
  • Do not use ECB mode in new work and update legacy work ASAP, it is not secure, see [ECB mode](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_Codebook_.28ECB.29), scroll down to the Penguin. Instead use CBC mode with a random IV, just prefix the encrypted data with the IV for use in decryption, it does not need to be secret. – zaph Mar 02 '18 at 12:47
  • `ASCII.GetBytes()` is awfully wrong – H H Mar 02 '18 at 13:15
  • OutOfMemoryException can be prevented by using a FileStream instead of the MemoryStream – H H Mar 02 '18 at 13:15
  • Invalid length could be caused by the encryption code - all those padding and Block modes must match. See answer below. – H H Mar 02 '18 at 13:16
  • Hi this is old work which specifically requires use of aes-128-ecb with no padding. – madskillz Mar 02 '18 at 13:19
  • @HenkHolterman Can you help me out in changing the above code to use FileStream. – madskillz Mar 02 '18 at 13:20
  • 1
    Yes, I could but it's such a good exercise to do it yourself. And oh, the ReadAllBytes should become a Stream too. – H H Mar 02 '18 at 13:23
  • Any reference to do it myself ? – madskillz Mar 02 '18 at 13:24
  • So, unless you're provided with copy&paste code you can't write a program? The first reference for any .NET class is the Microsoft .NET reference pages. – President James K. Polk Mar 02 '18 at 15:19
  • Sorry for being without the knowledge to code. But yes I can try looking at others code. Well I do not know why people are so angry. – madskillz Mar 03 '18 at 15:20

1 Answers1

1

So many security errors:

  1. Unless the data to be encrypted is always an exact multiple of the block size a padding mode must be used for ECB (and CBC) modes. This is because AES is block based.

  2. The out of memory is caused because the entire file is been great at once: File.ReadAllBytes, read smaller chunks in the streaming code..

  3. Use a key of exactly one go the supported key sizes, 128, 192 or 256 bits. There is no standard for padding keys and short keys are not secure.

  4. Assuming ASCII is almost always a bad choice, generally the correct choice is UTF-8.

  5. ECB mode does not use an IV. With CBC mode the IV should be block size and random bytes.

  6. Do not use ECB mode in new work, it is not secure, see ECB mode, scroll down to the Penguin. Instead use CBC mode with a random IV, just prefix the encrypted data with the IV for use in decryption, it does not need to be secret.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • 1. Files which I am currently decrypting do not use padding , 2. Can you guide in changing code to read in chunks ? – madskillz Mar 02 '18 at 13:22
  • 1
    1. Padding is handled by the encryption implementation generally through an option. You have chosen the option `PaddingMode.None`, instead use either 'PaddingMode.PKCS7 ` sometimes misnamed `PaddingMode.PKCS5`.2. For streaming search SO, Java is not my language. – zaph Mar 02 '18 at 13:27
  • As I mentioned this is for decryption of bytes which are encrypted with aes-128-ecb no padding options. and this is c# not java – madskillz Mar 02 '18 at 13:39
  • 1
    See answer comment item 1. Since AES is block based the data to decrypt **must** be a multiple of the block size or it will fail with the error you are getting. AES encryption with ECB (or CBC) mode will produce encrypted data that is a multiple of the block size. Check the encrypted file size. If "no padding options" means the implementation default that is probably PKCS#7. OK, not Java but still not a current language for me. Consider adding a language tag to the question. – zaph Mar 02 '18 at 13:52
  • aes.Padding = PaddingMode.PKCS7; - if i do this then error is padding is invalid , cannot be removed. – madskillz Mar 02 '18 at 14:38
  • [link](https://msdn.microsoft.com/en-us/library/system.security.cryptography.paddingmode(v=vs.110).aspx ) - it says pkcs7 and none are different – madskillz Mar 03 '18 at 15:20