Reputation: 119
I'm not running into any error just the output for my program is doing something strange. I've also noticed this thread here: Encrypt file with AES-256 and Decrypt file to its original format. This is my own approach to this issue, so I hope this isn't considered a duplicate. I'll post my code below, and explain how it functions. (Not including the encryption code)
path = 'files/*'
files = glob.glob(path)
with open('extensions.txt', 'w') as extension:
for listing in files:
endfile = os.path.splitext(listing)[1]
extension.write(endfile + "\n")
extension.close()
for in_filename in files:
out_filename1 = os.path.splitext(in_filename)[0]
out_filename = out_filename1 + '.pycrypt'
with open(in_filename, 'rb') as in_file, open(out_filename, 'wb') as out_file:
encrypt(in_file, out_file, password)
in_file.close()
out_file.close()
os.remove(in_filename)
print 'Files Encrypted'
password = raw_input('Password-> ')
path = 'files/*'
files = glob.glob(path)
for in_filename in files:
f=open('extensions.txt')
lines=f.readlines()
counter+=1
out_filename1 = os.path.splitext(in_filename)[0]
out_filename = out_filename1 + lines[counter]
with open(in_filename, 'rb') as in_file, open(out_filename, 'wb') as out_file:
decrypt(in_file, out_file, password)
in_file.close()
out_file.close()
os.remove(in_filename)
print 'Files Decrypted'
The code takes all the files in a folder, and encrypts them using AES. Then changes all the files extensions to .pycrypt, saving the old extension(s) into a file called "extensions.txt". After decryption it gives the files there extensions back by reading the text file line by line.
Here's the issue, after decryption every file goes from this:
15.png, sam.csv
To this
15.png, sam.csv
I've also noticed that if I re-encrypted the files with the symbol, the "extensions.txt" go from this:
15.png
sam.csv
bill.jpeg
To this (notice the spaces):
15.png
sam.csv
bill.jpeg
Any ideas what is causing this?
Upvotes: 0
Views: 235
Reputation: 61952
Let's read the documentation (emphasis mine):
file.readline([size])
Read one entire line from the file. A trailing newline character is kept in the string (but may be absent when a file ends with an incomplete line). [6] If the size argument is present and non-negative, it is a maximum byte count (including the trailing newline) and an incomplete line may be returned. When size is not 0, an empty string is returned only when EOF is encountered immediately.
file.readlines([sizehint])
Read until EOF using
readline()
and return a list containing the lines thus read. If the optional sizehint argument is present, instead of reading up to EOF, whole lines totalling approximately sizehint bytes (possibly after rounding up to an internal buffer size) are read. Objects implementing a file-like interface may choose to ignore sizehint if it cannot be implemented, or cannot be implemented efficiently.
This means that lines[counter]
doesn't only contain the file extension, but also the newline character after that. You can remove all whitespace at the beginning and end with: lines[counter].strip()
.
A better way to do this is to encrypt a file "a.jpg" as "a.jpg.enc", so you don't need to store the extension in a separate file.
Upvotes: 3