Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File encryption with Python

Is there a way to encrypt files (.zip, .doc, .exe, ... any type of file) with Python?

I've looked at a bunch of crypto libraries for Python including pycrypto and ezpycrypto but as far as I see they only offer string encryption.

like image 979
Pinkie Avatar asked May 30 '10 13:05

Pinkie


2 Answers

In Python versions prior to version 3.0, the read method of a file object will return a string, provide this string to the encryption library of your choice, the resulting string can be written to a file.

Keep in mind that on Windows-based operating systems, the default mode used when reading files may not accurately provide the contents of the file. I suggest that you be familiar with the nuances of file modes and how they behave on Windows-based OSes.

like image 113
Noah McIlraith Avatar answered Oct 05 '22 23:10

Noah McIlraith


You can read the complete file into a string, encrypt it, write the encrypted string in a new file. If the file is too large, you can read in chunks.

Every time you .read from a file, you get a string (in Python < 3.0).

like image 28
tzot Avatar answered Oct 06 '22 00:10

tzot