Help.. With My Python3.4 Encryption Program

Feb 29, 2016 09:02 PM
635923477884898293.jpg

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 , Example: ... -e AES', choices='AES','DES3', 'Blowfish', 'CAST')

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

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

p.addargument('-kr', '--keyrandom', help='-kr , 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 ')

p.add
argument('-f', '--filename', help='-f ')

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:

Just updated your iPhone? You'll find new Apple Intelligence capabilities, sudoku puzzles, Camera Control enhancements, volume control limits, layered Voice Memo recordings, and other useful features. Find out what's new and changed on your iPhone with the iOS 18.2 update.

Related Articles

637263493835297420.jpg

How to Use Zero-Width Characters to Hide Secret Messages in Text (& Even Reveal Leaks)

636455706472146367.jpg

How to Hide DDE-Based Attacks in MS Word

Comments

No Comments Exist

Be the first, drop a comment!