Forum Thread: Help.. With My Python3.4 Encryption Program

This is my first Tool written in Python, just finished my python course at stack skills

So I've been writing this Encryption python program(with pycrypto) for a week or two now, and I've hit a problem. I've tried to fix this many time and searched google alot but nothing helped me. I'm able to encrypt files with AES and 32 bit key using AES.MODECBC , it seems to work fine but when I go to decrypt it the decryption code runs and writes it to a file but dosen't show the plaintext just ciphertext but not the one of the file im trying to decrypt ... Anyway here's the code, I would love to know where I went wrong and help with my error ... enough rambling here :

#!/usr/bin/env python
# -- coding: utf-8 --
# Copyright 2016 WireWise <>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import struct, hashlib, os, Crypto, random, sys, argparse, string, base64, io

class bcolors:
HEADER = '\03395m'
OKBLUE = '\03394m'
OKGREEN = '\03392m'
WARNING = '\03393m'
FAIL = '\03391m'
ENDC = '\0330m'
BOLD = '\0331m'
UNDERLINE = '\0334m'

#Random Generated Keys
8BitKey = ''.join(random.choice(string.ascii_letters + string.digits) for n in range(8))
16BitKey = ''.join(random.choice(string.ascii_letters + string.digits) for n in range(16))
32BitKey = ''.join(random.choice(string.ascii_letters + string.digits) for n in range(32))
64BitKey = ''.join(random.choice(string.ascii_letters + string.digits) for n in range(64))
128BitKey = ''.join(random.choice(string.ascii_letters + string.digits) for n in range(128))
256BitKey = ''.join(random.choice(string.ascii_letters + string.digits) for n in range(256))
512BitKey = ''.join(random.choice(string.ascii_letters + string.digits) for n in range(512))
p = argparse.ArgumentParser(description='Simple Encryption Program.' )

p.addargument('-e', '--encryption',type=str, help='-e <encryption algorithm> , Example: ... -e AES', choices='AES','DES3', 'Blowfish', 'CAST')

p.addargument('-d', '--decryption', type=str, help='-d <decryption algorithm>, Example: ... -d AES', choices='AES','DES3', 'Blowfish', 'CAST')

p.addargument('-ea', '--encryptionalgorithms', help='Displays all avaliable Encryption Algorithm')

p.addargument('-kr', '--keyrandom', help='-kr <number of bits for key>, look at the README to know how many bits each encryption can take.', type=str, choices='8','16','32', '64', '128', '256', '512')

p.addargument('-k','--key', help='Enter the Key you used to encrypt the file Example: -k <key>')
argument('-f', '--filename', help='-f <filename>')
args = p.parseargs()

global UserChoiceKS
UserChoiceKS = ''
global fileEnc
fileEnc = str(args.filename)

#If options for -kr parse
if args.keyrandom == '8':
UserChoiceKS = 8BitKey
if args.keyrandom == '32':
UserChoiceKS = 32BitKey
if args.keyrandom == '64':
UserChoiceKS =
if args.keyrandom == '128':
UserChoiceKS = 128BitKey
if args.keyrandom == '256':
UserChoiceKS =
if args.keyrandom == '512':
UserChoiceKS = 512BitKey
if args.keyrandom == '':
UserChoiceKS =

#AES encryption
def AESENC():
from Crypto import Random
from Crypto.Cipher import AES
key = UserChoiceKS
filename = fileEnc
outfilename = os.path.join(os.path.dirname(infilename), os.path.basename(infilename)+(".cb"))
iv =
aes =, AES.MODE
CFB, iv)
chunksize = 64 1024
filename, 'rb') as infile:
filename, 'wb') as outfile:
while True:
chunk = in
if len(chunk) == 0:
elif len(chunk) % 16 != 0:
chunk += b" " (16 - len(chunk) % 16)
#AES decryption
def AESDEC():
from Crypto import Random
from Crypto.Cipher import AES
filename = fileEnc
L = infilename.split('.')
key = args.key
filename = 'a'+str(L0)+'.txt'
iv =
aes =, AES.MODE
CFB, iv)
chunksize = 64 1024
with open(in
filename, 'rb') as infile:
with open(out
filename, 'wb') as outfile:
chunk = in
if len(chunk) % 16!= 0:
chunk += b" " (16 - len(chunk) % 16)
D = aes.decrypt(chunk)
print(bcolors.OKBLUE + "*Done Decrypting:" + bcolors.OKBLUE + fileEnc)
if args.encryption == 'AES':
print(bcolors.OKBLUE + "*KEY:" + bcolors.ENDC + UserChoiceKS)
except TypeError as t:
print("Check if you filled every field required")

if args.decryption == 'AES':

if you want to download the text below:

Fast Files - Share your files for FREE!

Fast Files - Share your files for FREE!

Join the Next Reality AR Community

Get the latest in AR — delivered straight to your inbox.

3 Responses

You are creating a new random key and IV each time you start your script, and consequently you are trying to decrypt your ciphertext with a different key. You have to save the key somewhere, or always use the same one.

I guess use the file write commands to save keys to unique files, then read the key from that file.

Thank you very much for your help :D

Share Your Thoughts

  • Hot
  • Active