0

I have coded a little class for crypting/decrypting byte[] data with AES (System.Security.Cryptography.RijndaelManaged).

The problem is: if i try to encrypt and then decrypt 256-length byte array, it works fine. If i use any other length (probably any) of data, it raises errors like "Padding is incorrect and cannot be removed" or "Incorrect length of data to en/de crypt".

I tried many configurations for Padding/Mode property for RijndaelManaged instance. Here is the code (uses default config Mode.CBC+Padding.PCKS7).

int m_keySize;
byte[] m_key;
byte[] m_iv;

public AESEncryption(string key, int keySize = 128)
{
    m_keySize = keySize;

    byte[] entropy = Convert.FromBase64String(key);

    int keySizeBytes = (int)m_keySize / 8;
    if (entropy.Length == keySizeBytes)
        m_key = entropy;
    else
    {
        m_key = new byte[keySizeBytes];
        Buffer.BlockCopy(entropy, 0, m_key, 0, keySizeBytes);
    }

    // Generate IV
    RijndaelManaged temp = new RijndaelManaged { KeySize = m_keySize };
    temp.GenerateIV();
    m_iv = temp.IV;
}

// Simple Encrypt(byte[]) method
byte[] Encrypt(byte[] sourceData)
{
    try
    {
        MemoryStream resultStream = new MemoryStream();

        using (RijndaelManaged m_aes = new RijndaelManaged { KeySize = m_keySize })
        using (ICryptoTransform crypto = m_aes.CreateEncryptor(m_key, m_iv))
        using (CryptoStream cryptoStream = new CryptoStream(resultStream, crypto, CryptoStreamMode.Write))
            cryptoStream.Write(sourceData, 0, sourceData.Length);

        return resultStream.ToArray();
    }
    catch { return null; }
}

// Simple Decrypt(byte[]) method
byte[] Decrypt(byte[] sourceData)
{
    try
    {
        MemoryStream resultStream = new MemoryStream();

        using (RijndaelManaged m_aes = new RijndaelManaged { KeySize = m_keySize })
        using (ICryptoTransform crypto = m_aes.CreateDecryptor(m_key, m_iv))
        using (CryptoStream cryptoStream = new CryptoStream(resultStream, crypto, CryptoStreamMode.Write))
            cryptoStream.Write(sourceData, 0, sourceData.Length);

        return resultStream.ToArray();
    }
    catch { return null; }
}
Marc
  • 3,905
  • 4
  • 21
  • 37
Loryan55
  • 325
  • 4
  • 13

1 Answers1

-1

After many useless solutions i have found mine. Quite stupid. I set padding to None, and use own padding method, calling Pad(ref sourceData) in EncrypMethod before encryption.

// TODO: I don't know what's wrong, it is only one working solution to make data decryptable. Enablind AES padding causes exceptions.
        void Pad(ref byte[] source)
        {
            int sourceLength = source.Length;
            while (Math.IEEERemainder(sourceLength, 256) != 0 || sourceLength < 256)
                sourceLength++;

            if (sourceLength == source.Length)
                return;

            int padAmount = (sourceLength - source.Length);

            MemoryStream stream = new MemoryStream(sourceLength);
            stream.Write(source, 0, source.Length);
            stream.Write(new byte[padAmount], 0, padAmount);

            source = stream.ToArray();
        }
Loryan55
  • 325
  • 4
  • 13
  • The padding error is most likely an artifact of a decryption failure, not the root cause. – CodesInChaos Sep 14 '13 at 12:08
  • @CodesInChaos I know, thanks, but i am just running simple code that uses actually the same class instance to crypt/decrypt test empty data. For now, only my PAD() method is the solution, but i really don't like this. – Loryan55 Sep 14 '13 at 17:41