Skip to content

Instantly share code, notes, and snippets.

@revanthpobala
Created August 13, 2016 20:47
Show Gist options
  • Save revanthpobala/a3dc791cf69e8a11dfda756bd236a7d9 to your computer and use it in GitHub Desktop.
Save revanthpobala/a3dc791cf69e8a11dfda756bd236a7d9 to your computer and use it in GitHub Desktop.
Encrypt a Folder
from hashlib import md5
from Crypto.Cipher import AES
from Crypto import Random
import base64
import os
def derive_key_and_iv(password, salt, key_length, iv_length):
d = d_i = ''
while len(d) < key_length + iv_length:
d_i = md5(d_i + password + salt).digest()
d += d_i
return d[:key_length], d[key_length:key_length+iv_length]
def encrypt(in_file, out_file, password, key_length=32):
bs = AES.block_size
salt = Random.new().read(bs - len('Salted__'))
key, iv = derive_key_and_iv(password, salt, key_length, bs)
cipher = AES.new(key, AES.MODE_CBC, iv)
#print in_file
in_file = file(in_file, 'rb')
out_file = file(out_file, 'wb')
out_file.write('Salted__' + salt)
finished = False
while not finished:
chunk = in_file.read(1024 * bs)
if len(chunk) == 0 or len(chunk) % bs != 0:
padding_length = bs - (len(chunk) % bs)
chunk += padding_length * chr(padding_length)
finished = True
out_file.write(cipher.encrypt(chunk))
in_file.close()
out_file.close()
def decrypt(in_file, out_file, password, key_length=32):
bs = AES.block_size
in_file = file(in_file, 'rb')
out_file = file(out_file, 'wb')
salt = in_file.read(bs)[len('Salted__'):]
key, iv = derive_key_and_iv(password, salt, key_length, bs)
cipher = AES.new(key, AES.MODE_CBC, iv)
next_chunk = ''
finished = False
while not finished:
chunk, next_chunk = next_chunk, cipher.decrypt(in_file.read(1024 * bs))
if len(next_chunk) == 0:
padding_length = ord(chunk[-1])
if padding_length < 1 or padding_length > bs:
raise ValueError("bad decrypt pad (%d)" % padding_length)
# all the pad-bytes must be the same
if chunk[-padding_length:] != (padding_length * chr(padding_length)):
# this is similar to the bad decrypt:evp_enc.c from openssl program
raise ValueError("bad decrypt")
chunk = chunk[:-padding_length]
finished = True
out_file.write(chunk)
in_file.close()
out_file.close()
def encode(in_file, out_file):
in_file = file(in_file, 'rb')
out_file = file(out_file, 'wb')
data = in_file.read()
out_file.write(base64.b64encode(data))
in_file.close()
out_file.close()
def decode(in_file, out_file):
in_file = file(in_file, 'rb')
out_file = file(out_file, 'wb')
data = in_file.read()
out_file.write(base64.b64decode(data))
in_file.close()
out_file.close()
def create_new_file( _file):
return _file.replace('.','__e.')
def restore_file( _file):
return _file.replace('__e.','.')
def encrypt_folder( path, password):
for i in os.walk(path):
for _file in i[2]:
try:
print "File Encrypting is ",path+_file
new_file = path+create_new_file(_file)
old_file = path+_file
encrypt(old_file,new_file,password)
print "Deleting old file"
os.remove(old_file)
except:
print "Please check your input files"
pass
def decrypt_folder(path, password):
for _files in os.walk(path):
for _file in _files[2]:
try:
print "Decrypting the file", path+_file
new_file = path+restore_file(_file)
old_file = path+_file
decrypt(old_file,new_file,password)
print "Deleting the encrypted file"
os.remove(old_file)
except:
print "Please check your input files"
pass
__password = raw_input("Please provide a password:\n")
__path = raw_input("Please provide a path to the folder to Encrypt/Decrypt")
print "1. Encrypt"
print "2. Decrypt"
print "Please note that do not encrypt the encrypted files"
print "The path you provided is:\n", __path
option = input("Please provide an option only in Integers ")
if option == 1:
encrypt_folder(__path, __password)
if option == 2:
decrypt_folder(__path, __password)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment