Archived Forum Post

Index of archived forum posts


Decrypt chilkat AES encrypted files with native .NET

Oct 13 '15 at 12:26

We have encrypted files (Encrypted with Chilkat AES Encryption)

We're trying to decrypt them now with native .NET and we've hit an odditiy:

this is the original file (between the --------- markers):

hello world

this is a demo for the encryption module

pretty please Work

3k more text data

With the Chilkat old VB6 code it decrypts fine:

Dim crypt As New ChilkatCrypt2
Dim bArr() As Byte

crypt.CryptAlgorithm = "aes"
crypt.CipherMode = "cbc"
crypt.KeyLength = 512

crypt.SetEncodedKey "#khRT>&Gl:]6nl:g;ED)@u5i]mBJFqI,a]m7-ZT2}kA7:BjvSh$PW(cFm[J7[%WX", "ascii"
crypt.SetEncodedIV  "$[Mcj&g]<[w3PHmQ;L-G-TfmjeI3nHqptRhbP7/FDY>T%KMytU*LYs:{2WQNS#Z*", "ascii"

bArr = crypt.DecryptBytesENC(Base64FileContent)

When I decrypt it using .Net the beginning of the file is wrong but the rest is perfectly fine First thing I found out is that there is no such thing as a keylength of 512. AES support sup to 256 So I had to be a bit creative and "crop" the KEY & IV values. However I Believe this happens as well in the chilkat implementation

AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
byte[] Key = {35,107,104,82,84,62,38,71,108,58,93,54,110,108,58,103,59,69,68,41,64,117,53,105,93,109,66,74,70,113,73,44};
byte[] IV = {36,91,77,99,106,38,103,93,60,91,119,51,80,72,109,81};
aes.KeySize = Key.Length*8;
aes.BlockSize = IV.Length*8;
aes.Mode = CipherMode.CBC;
aes.Key = Key;
aes.IV = IV;
//Any other Key or IV lenghts fail

string encodedFile = File.ReadAllText(sourceFilename);
byte[] encryptedString = Convert.FromBase64String(encodedFile);

ICryptoTransform transform = aes.CreateDecryptor(aes.Key, aes.IV);
        using (FileStream destination = new FileStream(destinationFilename, FileMode.CreateNew, FileAccess.Write, FileShare.None))
            using (CryptoStream cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))
                    //using (FileStream source = new FileStream(sourceFilename2, FileMode.Open, FileAccess.Read, FileShare.Read))
                    using (MemoryStream source = new MemoryStream(encryptedString))
                catch (CryptographicException exception)
                    if (exception.Message == "Padding is invalid and cannot be removed.")
                        throw new ApplicationException("Universal Microsoft Cryptographic Exception (Not to be believed!)", exception);

With this code it outputs: (there are non printable binary characters in the first line of the output) ------------- START ------------------ {F!bB((T{e his is a demo for the encryption module

pretty please Work

3k more text data perfectly decrypted

Anyone an idea?