1

I developed a code based on information available online regarding an AES Encryption and Decryption routine.

Language: Python 2.7.x

Complete Code -

#!/usr/bin/python

import sys, os
import hashlib
import base64
from Crypto.Cipher import AES

## Variables in computation.
IV = u'1234567890123456'
BLOCK_SIZE = 32
INTERRUPT = u'\u0001'
PAD = u'\u0000'
SECRET = os.urandom(32)
filename=sys.argv[1]

def AddPadding(data, interrupt, pad, block_size):
   new_data = ''.join([data, interrupt])
   new_data_len = len(new_data)
   remaining_len = block_size - new_data_len
   to_pad_len = remaining_len % block_size
   pad_string = pad * to_pad_len
   return ''.join([new_data, pad_string])

def StripPadding(data, interrupt, pad):
   return data.rstrip(pad).rstrip(interrupt)

def encAES(cipher_code, file_data):
   data_padded = AddPadding(file_data, INTERRUPT, PAD, BLOCK_SIZE)
   encrypted = cipher_code.encrypt(data_padded)
   return encrypted

def decAES(cipher_code, file_data):
   decrypted  = cipher_code.decrypt(file_data)
   return StripPadding(decrypted, INTERRUPT, PAD)

def FileSave(fwname, fwdata):
   f = open(fwname, 'w')
   f.write(fwdata)
   f.close

def FileRead(frname):
   f = open(frname, 'rb')
   frdata = f.read()
   return frdata

cipher = AES.new(SECRET, AES.MODE_CBC, IV)

## Encryption
data2encrypt = base64.b64encode(FileRead(filename))
encrypted_data = encAES(cipher, data2encrypt)
encrypted_content = base64.b64encode(encrypted_data)

encrypted_filename = "enc_"+filename
FileSave(encrypted_filename, encrypted_content)
print "Encryption complete. File saved as: "+ encrypted_filename

## Decryption
data2decrypt = base64.b64decode(FileRead(encrypted_filename))
decrypted_data = decAES(cipher, data2decrypt)
decrypted_content = base64.b64decode(decrypted_data)

decrypted_filename = "dec_"+filename
FileSave(decrypted_filename, decrypted_content)
print "Decryption complete. File saved as: "+ decrypted_filename

Now, the encryption routine is working fine but Decryption Routine is giving an error -

Commandline - python test.py sample.txt

ERROR:

Traceback (most recent call last):
  File "test.py", line 65, in <module>
    decrypted_data = decAES(cipher, data2decrypt)
  File "test.py", line 38, in decAES
    return StripPadding(decrypted, INTERRUPT, PAD)
  File "test.py", line 29, in StripPadding
    return data.rstrip(pad).rstrip(interrupt)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 3: ordinal not in range(128)

What can be the possible workaround?

3
  • Is this Python 3 or Python 2? Commented Jul 8, 2014 at 18:07
  • You are probably trying to interpret the byte sequence (the result of encryption) as a text string. Use proper encoding like Base64 or HEX. Commented Jul 8, 2014 at 18:16
  • @OlegEstekhin: no, the OP is concatenating Unicode and a byte string, triggering an implicit decode. Commented Jul 8, 2014 at 18:30

1 Answer 1

2

You are concatenating a byte string with a Unicode value, triggering an automatic decode of the bytestring. This fails, as your decrypted text is not decodable as ASCII.

Don't use Unicode INTERRUPT and PAD values here; you are not reading Unicode data from the file here anyway:

INTERRUPT = '\1'
PAD = '\0'

You'll have to create a new instance of the AES object to decrypt; you cannot reuse the object you used for encrypting as it's IV state has been altered by the encryption:

decrypt_cipher = AES.new(SECRET, AES.MODE_CBC, IV)
decrypted_data = decAES(decrypt_cipher, data2decrypt)

With those changes your code works and can encrypt and again decrypt the data.

Sign up to request clarification or add additional context in comments.

2 Comments

I want this to work with all kind of files. I have to use the same logic for .DAT files and media files (mp4, wtv, wav etc.).
@pwn.star: then open your files in binary mode, always. Currently your FileSave function saves data in text mode; that's fine here, but on reading you always read in binary mode, on Windows this'll get you some extra newline characters. Your data will otherwise always be bytes, so don't use Unicode strings here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.