Skip to content

Commit 61c53f7

Browse files
committed
V0.1.6 clean up p2
1 parent 268072b commit 61c53f7

File tree

7 files changed

+71
-64
lines changed

7 files changed

+71
-64
lines changed

AESdecrypt.py

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from AESdecryptfunc import * #import AESdecryptfunc module to use functions created for this program
22
import math #import math module to use function such as ceiling
3+
import io
34

45
#check that script is running with the two text files as the two parameters or else quit
56
if len(sys.argv) is not 3:#takes in two arguments for the ciphertext.txt file name and plainhex.txt file name
@@ -9,7 +10,7 @@
910
while(len(PassPhrase)!=16):
1011
print("Enter in the 16 character passphrase to decrypt your text file %s" %sys.argv[1])
1112
PassPhrase=input()#takes in user input of char, eg. "Iwanttolearnkung"
12-
print(len(PassPhrase))
13+
#print(len(PassPhrase))
1314
if(len(PassPhrase)<16):#check if less than 16 characters, if so add one space character until 16 chars
1415
while(len(PassPhrase)!=16):
1516
PassPhrase=PassPhrase+" "
@@ -20,16 +21,17 @@
2021
#open ciphertext.txt file to read and decrypt
2122
file=open(sys.argv[1], "r")
2223
message=(file.read())
23-
print("Inside your ciphertext message is: %s" % message)
24+
print("Inside your ciphertext message is:\n%s\n" % message)
25+
file.close()
2426

2527
#set up some parameters
26-
start=len(message)-32#set starting pointer for the part to decrypt of the ciphertext
27-
end=len(message)#set ending pointer for the part to decrypt of the plaintex
28+
start=0#set starting pointer for the part to decrypt of the ciphertext
29+
end=32#set ending pointer for the part to decrypt of the plaintex
2830
length=len(message)#check the entire size of the message
2931
loopmsg=0.00#create a decimal value
3032
loopmsg=math.ceil(length/32)+1#use formula to figure how long the message is and how many 16 character segmentss must be decrypted
31-
outputhex=""#setup output message in hex
32-
asciioutput=""
33+
outputhex=""#setup output message segment in hex
34+
asciioutput=""#setup compilation of output message in ascii
3335

3436
#need to setup roundkeys here
3537
PassPhrase=BitVector(textstring=PassPhrase)
@@ -44,59 +46,62 @@
4446
roundkey9=findroundkey(roundkey8,9)
4547
roundkey10=findroundkey(roundkey9,10)
4648
roundkeys=[roundkey1,roundkey2,roundkey3,roundkey4,roundkey5,roundkey6,roundkey7,roundkey8,roundkey9,roundkey10]
47-
print("Roundkey 10 is: %s" % roundkeys[9])
49+
#print("Roundkey 10 is: %s" % roundkeys[9])
50+
51+
FILEOUT = io.open(sys.argv[2], 'w', encoding='utf-8')
52+
4853
# set up the segement message loop parameters
4954
for y in range(1, loopmsg): # loop to encrypt all segments of the message
5055
plaintextseg = message[start:end]
51-
print("The ciphertext segment to decrypt is: %s" % plaintextseg)
56+
#print("The ciphertext segment to decrypt is: %s" % plaintextseg)
5257
# add round key
5358
bv1 = BitVector(hexstring=plaintextseg)
5459
bv2 = BitVector(hexstring=roundkeys[9])
5560
resultbv = bv1 ^ bv2
5661
myhexstring = resultbv.get_bitvector_in_hex()
57-
print("The output after adding the roundkey 10 is: %s" % myhexstring)
62+
#print("The output after adding the roundkey 10 is: %s" % myhexstring)
5863
#inverse shift row
5964
myhexstring=invshiftrow(myhexstring)
60-
print("The output after invshiftrow is: %s" % myhexstring)
65+
#print("The output after invshiftrow is: %s" % myhexstring)
6166
#inverse subbyte
6267
myhexstring=invsubbyte(myhexstring)
63-
print("The output after invsubbyte is: %s" % myhexstring)
68+
#print("The output after invsubbyte is: %s" % myhexstring)
6469

6570
for x in range(8, -1, -1):
66-
print("Round: %i" % (x+1))
71+
#print("Round: %i" % (x+1))
6772
# add roundkey for current round
6873
bv1 = BitVector(hexstring=myhexstring)
6974
bv2 = BitVector(hexstring=roundkeys[x])
7075
resultbv = bv1 ^ bv2
7176
myhexstring = resultbv.get_bitvector_in_hex()
72-
print("The output after adding the roundkey %i is: %s" %((x+1),myhexstring))
77+
#print("The output after adding the roundkey %i is: %s" %((x+1),myhexstring))
7378
# mix column
7479
bv3 = BitVector(hexstring=myhexstring)
7580
myhexstring=invmixcolumn(bv3)
76-
print("The output after invmixcolumn %i is: %s" % ((x + 1), myhexstring))
81+
#print("The output after invmixcolumn %i is: %s" % ((x + 1), myhexstring))
7782
# shift rows
7883
myhexstring = invshiftrow(myhexstring)
79-
print("The output after invshiftrow is: %s" % myhexstring)
84+
#print("The output after invshiftrow is: %s" % myhexstring)
8085
# sub byte
8186
myhexstring = invsubbyte(myhexstring)
82-
print("The output after invsubbyte is: %s" % myhexstring)
87+
#print("The output after invsubbyte is: %s" % myhexstring)
8388

8489
#add initial round key
8590
bv1 = BitVector(hexstring=myhexstring)
8691
bv2 = PassPhrase
8792
resultbv = bv1 ^ bv2
8893
myhexstring = resultbv.get_bitvector_in_hex()
89-
print("The output after adding the initial roundkey is: %s" % myhexstring)
94+
#print("The output after adding the initial roundkey is: %s" % myhexstring)
9095

91-
start = start - 32 #increment start pointer
92-
end = end - 32 #increment end pointer
96+
start = start + 32 #increment start pointer
97+
end = end + 32 #increment end pointer
9398

94-
outputhex = myhexstring+outputhex
99+
outputhex = BitVector(hexstring=myhexstring)
100+
asciioutput = outputhex.get_bitvector_in_ascii()
101+
FILEOUT.write(asciioutput)
95102

96-
print(myhexstring)
97-
outputhex = BitVector(hexstring=outputhex)
98-
asciioutput = outputhex.get_bitvector_in_ascii()
99-
print("The decrypted message for the entire ciphertext is: %s" % asciioutput)
100-
FILEOUT = open(sys.argv[2], 'w')
101-
FILEOUT.write(asciioutput)
102103
FILEOUT.close()
104+
105+
file2=io.open(sys.argv[2], "r", encoding='utf-8')
106+
print("The decrypted message for the entire ciphertext is:\n%s\n" % file2.read())
107+
file2.close()

AESdecryptfunc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def subbyte(myhexstring):
6969

7070
lookuptable=[part0,part1,part2,part3,part4,part5,part6,part7,part8,part9,part10,part11,part12,part13,part14,part15]
7171

72-
print("The string size is ", len(myhexstring), " and the loop will run", math.ceil(len(myhexstring)/2), " times." )
72+
#print("The string size is ", len(myhexstring), " and the loop will run", math.ceil(len(myhexstring)/2), " times." )
7373
for loop in range(0, math.ceil(len(myhexstring)/2) ):
7474
x = ""
7575
y = ""
@@ -205,7 +205,7 @@ def invsubbyte(myhexstring):
205205

206206
lookuptable=[part0,part1,part2,part3,part4,part5,part6,part7,part8,part9,part10,part11,part12,part13,part14,part15]
207207

208-
print("The string size is ", len(myhexstring), " and the loop will run", math.ceil(len(myhexstring)/2), " times." )
208+
#print("The string size is ", len(myhexstring), " and the loop will run", math.ceil(len(myhexstring)/2), " times." )
209209
for loop in range(0, math.ceil(len(myhexstring)/2) ):
210210
x = ""
211211
y = ""

AESencrypt.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
while(len(PassPhrase)!=16):
1111
print("Enter in the 16 character passphrase to encrypt your text file %s" %sys.argv[1])
1212
PassPhrase=input()#takes in user input of char, eg. "Iwanttolearnkung"
13-
print(len(PassPhrase))
13+
#print(len(PassPhrase))
1414
if(len(PassPhrase)<16):#check if less than 16 characters, if so add one space character until 16 chars
1515
while(len(PassPhrase)!=16):
1616
PassPhrase=PassPhrase+" "
@@ -21,7 +21,8 @@
2121
#open plaintext.txt file to read and encrypt
2222
file=open(sys.argv[1], "r")
2323
message=(file.read())
24-
print("Inside your plaintext message is: %s" % message)
24+
print("Inside your plaintext message is:\n%s\n" % message)
25+
file.close()
2526

2627
#set up some parameters
2728
start=0#set starting pointer for the part to encrypt of the plaintext
@@ -45,6 +46,8 @@
4546
roundkey10=findroundkey(roundkey9,10)
4647
roundkeys=[roundkey1,roundkey2,roundkey3,roundkey4,roundkey5,roundkey6,roundkey7,roundkey8,roundkey9,roundkey10]
4748

49+
FILEOUT = open(sys.argv[2], 'w')
50+
4851
# set up the segement message loop parameters
4952
for y in range(1, loopmsg): # loop to encrypt all segments of the message
5053
if(end+16<length): #if the end pointer is less than the size of the message, then set the segment to be 16 characters
@@ -56,70 +59,71 @@
5659
plaintextseg=plaintextseg.get_bitvector_in_hex()+"00"
5760
plaintextseg=BitVector(hexstring=plaintextseg).get_bitvector_in_ascii()
5861
#add round key zero/ find round key one
59-
print("The round key string is : %s" % PassPhrase)
60-
print("The part of the message to be encrypted is : %s" % plaintextseg)
62+
#print("The round key string is : %s" % PassPhrase)
63+
#print("The part of the message to be encrypted is : %s" % plaintextseg)
6164
bv1 = BitVector(textstring=plaintextseg)
62-
print("The plaintext message in hex is : %s" % bv1.get_bitvector_in_hex())
65+
#print("The plaintext message in hex is : %s" % bv1.get_bitvector_in_hex())
6366
bv2 = PassPhrase
64-
print("The password in hex/ roundkey zero is : %s" % bv2.get_bitvector_in_hex())
67+
#print("The password in hex/ roundkey zero is : %s" % bv2.get_bitvector_in_hex())
6568
resultbv=bv1^bv2
6669
myhexstring = resultbv.get_bitvector_in_hex()
67-
print("The initial adding round key output is : %s" % myhexstring)
70+
#print("The initial adding round key output is : %s" % myhexstring)
6871

6972
for x in range(1, 10): # loop through 9 rounds
7073
# sub byte
71-
print("Round: %i" % x)
74+
#print("Round: %i" % x)
7275
myhexstring = resultbv.get_bitvector_in_hex()
7376
temp1=subbyte(myhexstring)
74-
print("The subbyte output is: %s" % temp1)
77+
#print("The subbyte output is: %s" % temp1)
7578

7679
# shift rows
7780
temp2=shiftrow(temp1)
78-
print("The shiftrow output is: %s" % temp2)
81+
#print("The shiftrow output is: %s" % temp2)
7982

8083
# mix column
8184
bv3 = BitVector(hexstring=temp2)
8285
newbvashex=mixcolumn(bv3)
8386
newbv=BitVector(hexstring=newbvashex)
84-
print("The Mixcolumn Output is: %s" % newbvashex)
87+
#print("The Mixcolumn Output is: %s" % newbvashex)
8588

8689
#add roundkey for current round
8790
bv1 = BitVector(bitlist=newbv)
8891
bv2 = BitVector(hexstring=roundkeys[x-1])
8992
resultbv = bv1 ^ bv2
9093
myhexresult = resultbv.get_bitvector_in_hex()
91-
print("The round key %i is " % (x) + roundkeys[x-1])
94+
#print("The round key %i is " % (x) + roundkeys[x-1])
9295

93-
print("The output after adding the round key: %s" % myhexresult)
96+
#print("The output after adding the round key: %s" % myhexresult)
9497

9598
#start round 10
9699
# sub byte round 10
97-
print("Round 10:")
100+
#print("Round 10:")
98101
myhexstring = resultbv.get_bitvector_in_hex()
99102
temp1=subbyte(myhexstring)
100-
print("The subbyte output of round 10 is: %s" % temp1)
103+
#print("The subbyte output of round 10 is: %s" % temp1)
101104

102105
# shift rows round 10
103106
temp2=shiftrow(temp1)
104-
print("The output after shiftrow is: %s" % temp2)
107+
#print("The output after shiftrow is: %s" % temp2)
105108

106109
# addround key round 10
107110
newbv = BitVector(hexstring=temp2)
108111
bv1 = BitVector(bitlist=newbv)
109112
bv2 = BitVector(hexstring=roundkeys[9])
110113
resultbv = bv1 ^ bv2
111114
myhexstring = resultbv.get_bitvector_in_hex()
112-
print("The output after adding the roundkey is: %s" % myhexstring)
113-
115+
#print("The output after adding the roundkey is: %s" % myhexstring)
114116
#set encrypted hex segement of message to output string
115117
outputhextemp = resultbv.get_hex_string_from_bitvector()
116-
print("The outputhex value for this part of the message is: %s" % outputhextemp)
117-
outputhex = outputhex + outputhextemp
118+
#print("The outputhex value for this part of the message is: %s" % outputhextemp)
119+
FILEOUT.write(outputhextemp)
118120
start = start + 16 #increment start pointer
119121
end = end + 16 #increment end pointer
120122

121-
# encrypted output hex string to specified cipherhex file
122-
print("The outputhex value for the entire message is: %s" % outputhex)
123-
FILEOUT = open(sys.argv[2], 'w')
124-
FILEOUT.write(outputhex)
123+
# encrypted output hex string to specified cipherhex file
125124
FILEOUT.close()
125+
126+
file2=open(sys.argv[2], "r")
127+
print("The output hex value for the entire message is:\n%s\n" % file2.read())
128+
file2.close()
129+

AESencryptfunc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def subbyte(myhexstring):
2626

2727
lookuptable=[part0,part1,part2,part3,part4,part5,part6,part7,part8,part9,part10,part11,part12,part13,part14,part15]
2828

29-
print("The string size is ", len(myhexstring), " and the loop will run", math.ceil(len(myhexstring)/2), " times." )
29+
#print("The string size is ", len(myhexstring), " and the loop will run", math.ceil(len(myhexstring)/2), " times." )
3030
for loop in range(0, math.ceil(len(myhexstring)/2) ):
3131
x = ""
3232
y = ""

message.txt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
hi wats up this is a test to see if it works. this message will be quite long. its only to be sure that its works completely
2-
i really want it to work.
3-
if not
4-
idk wat ill do
5-
>0.0>
6-
this is my lifes work
7-
shaking intensifies
8-
hands sweaty
9-
knees weak
10-
Panic
1+
my name is katia
2+
and i like
3+
big dog noses
4+
and i cannot lie
5+
not other dogs can deny
6+
when a dog walks in
7+
with itty bitty waist
8+
and round nose in my face

output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8d7595f9499e787e998d39df57beaaadf34ac83183b19613835a63d0887b4b17c58351051594ee15c7bf388cbd601970d3ee27dbc480a910ff5ba8dfe540093bc426cae5cf357b3f72ecd7dcf12e4c3307f869f0ba5a3bf82951e5f1220cab1303e9ea3b91b7eae74bfa371992ae542eb5ceed62aaef6f0a6635868b2d38eb1c22dde4b6072f25d3030e11296201cfb5c429dea8d23347f262bcf8b106a0f7ff7e20a529e99cbbfdca4855f8f7564d4d00fe8c169d72ba0fda130f81b86132c86e30182357e8a3ff404b01b0592fbc283e20900ea1223d36ff2eeeaa2d1c947b84bcf01111cabb358012d42e133b6624c1a267f513b0241a2ac5c1b07feff227
1+
fae009acc4b24b17e5fdc13935ba6bb78b3ae40d6149c773505180aa1db892324810562d531c6b79f33e81843ed2d5311031a7cf1b69af856968ac4cb7c5d31963b2534759f5246f2537bd7ad145aa73e77c2a7eddf95c122cfec2e1fa9a46d982f2d543296edd343e16de093ef464112d8fd82191cdff98742d9016af73adc4be20026de125f00dffc0c61a9ca84a913dfb5cc7db95b3cdba09edba104bfad4

output2.txt

-97 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)