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
p.addargument('-d', '--decryption', type=str, help='-d
p.addargument('-ea', '--encryptionalgorithms', help='Displays all avaliable Encryption Algorithm')
p.addargument('-kr', '--keyrandom', help='-kr
p.addargument('-k','--key', help='Enter the Key you used to encrypt the file Example: -k
p.add
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
infilename = fileEnc
outfilename = os.path.join(os.path.dirname(infilename), os.path.basename(infilename)+(".cb"))
iv = Random.new().read(16)
aes = AES.new(key, AES.MODECFB, iv)
chunksize = 64 1024
with io.open(infilename, 'rb') as infile:
with io.open(outfilename, 'wb') as outfile:
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += b" " (16 - len(chunk) % 16)
outfile.write(aes.encrypt(chunk))
#AES decryption
def AESDEC():
from Crypto import Random
from Crypto.Cipher import AES
infilename = fileEnc
L = infilename.split('.')
key = args.key
outfilename = 'a'+str(L0)+'.txt'
iv = Random.new().read(AES.blocksize)
aes = AES.new(key, AES.MODECFB, iv)
chunksize = 64 1024
with open(infilename, 'rb') as infile:
with open(outfilename, 'wb') as outfile:
chunk = infile.read(chunksize)
if len(chunk) % 16!= 0:
chunk += b" " (16 - len(chunk) % 16)
D = aes.decrypt(chunk)
outfile.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:
Comments
No Comments Exist
Be the first, drop a comment!