MatZeg
MatZeg

Reputation: 478

C# RijndaelManaged to Python equivalent

I have the following C# code (code is inherited and can't compile it). This is used to decrypt and unzip a saved file.

using System.Security.Cryptography;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;

//Not the real key but same amount of chars
private const string kEncyptionKey = "01234567";

public string DecryptAndDecompressText (string strFileName)
{
    // Decryption ///
    FileStream fin = null;
    try
    {
        fin = new FileStream(strFileName, FileMode.Open, FileAccess.Read);
    }
    catch (System.IO.FileNotFoundException)
    {
        return "";
    }

    MemoryStream    memoryToDecompress =  new MemoryStream();

    UnicodeEncoding UE       = new UnicodeEncoding();
    RijndaelManaged RMCrypto = new RijndaelManaged();

    // This is the encryption key for our file 
    byte[] key = UE.GetBytes(kEncyptionKey);

    // Decrypt the data to a stream
    CryptoStream cs = new CryptoStream( memoryToDecompress, 
                                        RMCrypto.CreateDecryptor(key, key),
                                        CryptoStreamMode.Write);
    byte [] fileBuffer = new byte[fin.Length];
    fin.Read(fileBuffer, 0, fileBuffer.Length);
    cs.Write(fileBuffer, 0, fileBuffer.Length);

    fin.Close();

    // Reset the index of the Memory Stream
    memoryToDecompress.Position = 0;

    // Let the GC clean this up, we still need the memory stream
    //cs.Close();   


    // Decompress the File
    ZipInputStream s;
    s = new ZipInputStream(memoryToDecompress);

    ZipEntry theEntry;
    try
    {
        theEntry = s.GetNextEntry();
    }
    catch (System.Exception)
    {
        // Could not open the file...
        return "";
    }
}

I'm trying to create a python program to do the same. This is what I've got:

from Crypto.Cipher import AES

KEY = '01234567'.encode('utf-16be')

_f = open('<file>', 'r')

 _content = _f.read()

_cipher = AES.new(KEY, AES.MODE_CBC, KEY)

_dcontent = _cipher.decrypt(_content)

with open('extract.zip', 'w') as newfile:
    newfile.write(_dcontent)

_f.close()

I'm writing the result to the disk since I expect it to be a zip file (which contains one file). However I can't open the file with Archive Manager.

Any suggestions are welcome!

Upvotes: 4

Views: 1865

Answers (2)

rll
rll

Reputation: 5587

You should use the proper zip file library. I am guessing that is something format specific that is failing on your write statement. Using this library should avoid such drawbacks. The open function can take a password as optional in case it is protected.

Upvotes: 1

Artjom B.
Artjom B.

Reputation: 61902

You have to use the same key. System.Text.UnicodeEncoding is the UTF-16le encoding which also has an equivalent in python:

KEY = '01234567'.encode('utf-16le')

You have to read and write the files in binary mode if you're on Windows:

_f = open('<file>', 'rb')
...
open('extract.zip', 'wb')

Upvotes: 3

Related Questions