help with python and pycrypto

3

my headache is this I want to make a program that encrypts certain files and then delete the original version, because I tried it only works with only one file and creates a new one, I work with delicate files and when I finish to moficarlos is to encrypt and delete the unencrypted version.

> home = expanduser("~")

Here begins the code

## def encrypt(key, filename):
    chunksize = 64*1024
    outputFile = "(encrypted)"+filename
    filesize = str(os.path.getsize(filename)).zfill(16)
    IV = ''

    for i in range(16):
        IV += chr(random.randint(0, 0xFF))

    encryptor = AES.new(key, AES.MODE_CBC, IV)

    with open(filename, 'rb') as infile:
        with open(outputFile, 'wb') as outfile:
            outfile.write(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))

This code I found in a forum since I do not find anything relevant in pycrypto

> encrypt(getKey(password), lista_archivos)

With this I call to be able to encrypt the bad thing is that above they only accept a file and I have a list

> 
> lista_archivos =  [ [os.path.join(root,file),root.split(path)[1]] for root,dirs,files in os.walk(path)
                    for file in files if os.path.splitext(file)[-1] in extensiones ]

That's how I encrypt the key

password = "xx"

> def getKey(password):

    hasher = SHA256.new(password)
    return hasher.digest()

I'm summarizing it as much as I can

Well the fact is that, the list goes through all files with extensions .xx, and then I stored it in a list I want to encrypt those files and then delete the original, above the encryption code only allows me to encrypt a single file, if someone helps me, I'd really appreciate it.

    
asked by Jayson Jose 18.08.2016 в 17:49
source

1 answer

1

Your method encrypt(key, filename) only processes a file.

If you want to process a list you should add a loop in the body of the function. Or directly invoke it like this:

for f in lista_archivos:
    encrypt(getKey(password), f)
    
answered by 22.09.2016 / 16:29
source