-1

I need to set up a Python process which is able to get every ZIP file in a specific folder and to unzip it in order to access and process the data inside. People who provide me the ZIP files apparently use a Java routine in order to do so, but as I have no knowledge of Java (or even cryptography), I would like to use Python. They gave me :

  • a password
  • an initialization vector

They also specified that they use this type of command line : openssl enc -d -aes-128-cbc -K XXXXXX -iv YYYYYYYYY -in in.zip -out out.zip

Amongst other, I tried the following script, but:

  • It doesn't use initialization vector
  • It ends up telling me that the Testfile is not a .zip file. (I tried with a no crypted .zip file and it works fine).
    import pyzipper

     Testfile = Local_path + 'Testfile.zip' with
     pyzipper.AESZipFile(Testfile) as f:
         f.pwd = b'XXXXXXXXXXXXXXXXX'
         print(f.infolist())
         file_content = f.read('testfile.txt')

I can't find any solution that works with my files, so:

  • do you think it's possible to do so with python
  • if it's possible, would you be kind enough to help me?

Thanks a lot.

char
  • 2,063
  • 3
  • 15
  • 26
user37103
  • 21
  • 1
  • 9
  • Try [Pycrypto](https://www.novixys.com/blog/using-aes-encryption-decryption-python-pycrypto/#5_Decrypting_with_AES) instead of Pyzipper. – char Mar 26 '20 at 08:44
  • Ok, I had some difficulties installing pycrypto but I finally got it. Do you have any example of the syntax in order to take the encrypted zipfile path as input, along with the password and initialization vector (iv) and to get the decrypted file as output ? The only examples I could find are suited for encrypted strings, but not handling encrypted zipfiles, and through this I can't really understand how it works and how to use the right syntax. – user37103 Mar 26 '20 at 18:00
  • From what I understood, first I would need to decrypt the password with the initialization vector, and then open the zip file with the decrypted password. But it doesn't seem to be ok as in every example I need a phrase to decrypt, a key and an initialization vector. But in my case, I only have a password and an initialization vector to decrypt. – user37103 Mar 26 '20 at 19:47
  • [This link](https://eli.thegreenplace.net/2010/06/25/aes-encryption-of-files-in-python-with-pycrypto) has an example for a decryption function, which should work on ZIP files - according to their comment. You might be able to use the password as the key, but if that doesn't work they will need to provide you with the key. – char Mar 27 '20 at 08:45
  • Does this answer your question? [How to decrypt OpenSSL AES-encrypted files in Python?](https://stackoverflow.com/questions/16761458/how-to-decrypt-openssl-aes-encrypted-files-in-python) – char Mar 27 '20 at 08:50

1 Answers1

1

Thank you for your help. Indeed I gave up on using pycrypto and used subprocess. If it may be of any use in the future for anyone, here is the solution that I used in order to unzip the files whose name is contained in dico_zipfilename. Those zipfiles are all encrypted through 128-AES with a known key and iv. Before that I download openssl and Opensslexe_path is the path to the openssl.exe.

for i in range(0, len(v_dico_raw)):
     Name_zipfile = str(v_dico_raw[i][3])
     Path_fileinput = Local_path_zipped + str(Name_zipfile)
     Path_fileoutput = Local_path_unzipped + str(Name_zipfile)
     cmd_line = ['openssl', 'enc', '-d', '-aes-128-cbc', '-K', str(key), '-iv', str(iv), '-in', str(Path_fileinput), '-out', str(Path_fileoutput)]
        p = subprocess.Popen(cmd_line, cwd = Opensslexe_path)

It's solved for me :)

user37103
  • 21
  • 1
  • 9
  • Zip format has built in AES encryption using PKZIP standard, or the more commonly used WinZip standard. The latter is the same AES-256 bit encryption supported by free software like 7-zip. Your zip library should support making password protected zip files (make sure it's not using ZipCrypto format which is weak and outdated) – Barmak Shemirani Apr 04 '20 at 03:30