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 --
#
# CryptoBird.py
#
# 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
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# 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

#Colors
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))
#Parsing
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>')
p.add
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 =
64BitKey
if args.keyrandom == '128':
UserChoiceKS = 128BitKey
if args.keyrandom == '256':
UserChoiceKS =
256BitKey
if args.keyrandom == '512':
UserChoiceKS = 512BitKey
if args.keyrandom == '':
UserChoiceKS =
32BitKey

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

if args.decryption == 'AES':
AESDEC()

if you want to download the text below:

Fast Files - Share your files for FREE!

Fast Files - Share your files for FREE!

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