Why is python adding "ï £" to my filenames when decrypting AES?
<p>I'm not running into any error just the output for my program is doing something strange. I've also noticed this thread here: <a href="http://stackoverflow.com/questions/30070309/encrypt-file-with-aes-256-and-decrypt-file-to-its-original-format">Encrypt file with AES-256 and Decrypt file to its original format</a>. 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)</p>

<h1>For Encryption</h1>

<pre><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'
</code></pre>

<h1>For Decryption</h1>

<pre><code>password = raw_input('Password-&gt; ') 
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'
</code></pre>

<p>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.</p>

<p>Here's the issue, after decryption every file goes from this:</p>

<p>15.png, sam.csv</p>

<p>To this</p>

<pre><code>15.pngï £, sam.csvï £
</code></pre>

<p>I've also noticed that if I re-encrypted the files with the ï £ symbol, the "extensions.txt" go from this:</p>

<pre><code>15.png
sam.csv
bill.jpeg
</code></pre>

<p>To this (notice the spaces):</p>

<pre><code>15.png

sam.csv

bill.jpeg
</code></pre>

<p>Any ideas what is causing this?</p>

<p>Let's read the <a href="https://docs.python.org/2/library/stdtypes.html#bltin-file-objects" rel="nofollow">documentation</a> (emphasis mine):</p>

<blockquote>
<pre><code>file.readline([size])
</code></pre>
  
  <p>Read one entire line from the file. <strong><em>A trailing newline character is kept in the string (but may be absent when a file ends with an
  incomplete line).</em></strong> [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.</p>

<pre><code>file.readlines([sizehint])
</code></pre>
  
  <p>Read until EOF using <strong><em><code>readline()</code></em></strong> 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.</p>
</blockquote>

<p>This means that <code>lines[counter]</code> 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: <code>lines[counter].strip()</code>.</p>

<p>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.</p>

python linux encryption cryptography filenames