Uso python 2.7.13.
Código:
# -*- coding: utf8 -*-
import os, fnmatch, struct, random, string, base64, platform, sys, time, socket, json, urllib, ctypes, urllib
from Crypto import Random
from Crypto.Cipher import AES
KEY = ''.join([ random.choice(string.ascii_letters + string.digits) for n in xrange(32) ])
def encrypt(key, in_filename, newfilename, out_filename = None, chunksize = 65536, Block = 16):
if not out_filename:
out_filename = newfilename
iv = Random.new().read(AES.block_size)
encryptor = AES.new(key, AES.MODE_CBC, iv)
filesize = os.path.getsize(in_filename)
with open(in_filename, 'rb') as infile:
with open(out_filename, 'wb') as outfile:
outfile.write(struct.pack('<Q', filesize))
outfile.write(iv)
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += ' ' * (16 - len(chunk) % 16)
outfile.write(encryptor.encrypt(chunk))
import os, fnmatch, struct, random, string, base64, platform, sys, time, socket, json, urllib, ctypes, urllib
from Crypto import Random
from Crypto.Cipher import AES
KEY = ''.join([ random.choice(string.ascii_letters + string.digits) for n in xrange(32) ])
def encrypt(key, in_filename, newfilename, out_filename = None, chunksize = 65536, Block = 16):
if not out_filename:
out_filename = newfilename
iv = Random.new().read(AES.block_size)
encryptor = AES.new(key, AES.MODE_CBC, iv)
filesize = os.path.getsize(in_filename)
with open(in_filename, 'rb') as infile:
with open(out_filename, 'wb') as outfile:
outfile.write(struct.pack('<Q', filesize))
outfile.write(iv)
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += ' ' * (16 - len(chunk) % 16)
outfile.write(encryptor.encrypt(chunk))