W
W
wersoo2010-10-23 15:24:52
C++ / C#
wersoo, 2010-10-23 15:24:52

Can't get rid of exception in rijndael (AES) implementation in C#?

Apparently the eye is already so blurred that I don’t see the obvious, or is everything not at all obvious here?
Keys are the same everywhere. Padding too.
I ask for help in determining the cause and at least indicating the vector in which direction to look for a solution. Thank you.

// BinaryFormatter binFormat = new BinaryFormatter();
// List<DataTable> tables;
// [...]
using (Rijndael AES = Rijndael.Create())
{
    AES.Padding = PaddingMode.PKCS7;
    PasswordDeriveBytes pdb = new PasswordDeriveBytes(TablesController._baseKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
    using (FileStream fStream = File.Open(TablesController._baseFileName, FileMode.OpenOrCreate, FileAccess.Write))
    {
        using (CryptoStream cStream = new CryptoStream(
            fStream,
            AES.CreateEncryptor(pdb.GetBytes(32), pdb.GetBytes(16)), CryptoStreamMode.Write))
        {
            binFormat.Serialize(cStream, tables);
        }
    }
}
using (Rijndael AES = Rijndael.Create())
{
    AES.Padding = PaddingMode.PKCS7;
    PasswordDeriveBytes pdb = new PasswordDeriveBytes(TablesController._baseKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
    using (FileStream fStream = File.OpenRead(TablesController._baseFileName))
    {
        using (CryptoStream cStream = new CryptoStream(fStream, AES.CreateDecryptor(pdb.GetBytes(32), pdb.GetBytes(16)), CryptoStreamMode.Read))
        {
            object deserialized = binFormat.Deserialize(cStream);
            tables = deserialized as List<DataTable>;
        }   //  System.Security.Cryptography.CryptographicException не обработано 
            //  Message= Padding is invalid and cannot be removed (Заполнение неверно и не может быть удалено.)

    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VenomBlood, 2010-10-23
@VenomBlood

The above code (if you uncomment the declaration and initialize tables) is fully functional.
Perhaps the TablesController'a logic is to blame.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question