1
1
from AESdecryptfunc import * #import AESdecryptfunc module to use functions created for this program
2
2
import math #import math module to use function such as ceiling
3
+ import io
3
4
4
5
#check that script is running with the two text files as the two parameters or else quit
5
6
if len (sys .argv ) is not 3 :#takes in two arguments for the ciphertext.txt file name and plainhex.txt file name
9
10
while (len (PassPhrase )!= 16 ):
10
11
print ("Enter in the 16 character passphrase to decrypt your text file %s" % sys .argv [1 ])
11
12
PassPhrase = input ()#takes in user input of char, eg. "Iwanttolearnkung"
12
- print (len (PassPhrase ))
13
+ # print(len(PassPhrase))
13
14
if (len (PassPhrase )< 16 ):#check if less than 16 characters, if so add one space character until 16 chars
14
15
while (len (PassPhrase )!= 16 ):
15
16
PassPhrase = PassPhrase + " "
20
21
#open ciphertext.txt file to read and decrypt
21
22
file = open (sys .argv [1 ], "r" )
22
23
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 ()
24
26
25
27
#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
28
30
length = len (message )#check the entire size of the message
29
31
loopmsg = 0.00 #create a decimal value
30
32
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
33
35
34
36
#need to setup roundkeys here
35
37
PassPhrase = BitVector (textstring = PassPhrase )
44
46
roundkey9 = findroundkey (roundkey8 ,9 )
45
47
roundkey10 = findroundkey (roundkey9 ,10 )
46
48
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
+
48
53
# set up the segement message loop parameters
49
54
for y in range (1 , loopmsg ): # loop to encrypt all segments of the message
50
55
plaintextseg = message [start :end ]
51
- print ("The ciphertext segment to decrypt is: %s" % plaintextseg )
56
+ # print("The ciphertext segment to decrypt is: %s" % plaintextseg)
52
57
# add round key
53
58
bv1 = BitVector (hexstring = plaintextseg )
54
59
bv2 = BitVector (hexstring = roundkeys [9 ])
55
60
resultbv = bv1 ^ bv2
56
61
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)
58
63
#inverse shift row
59
64
myhexstring = invshiftrow (myhexstring )
60
- print ("The output after invshiftrow is: %s" % myhexstring )
65
+ # print("The output after invshiftrow is: %s" % myhexstring)
61
66
#inverse subbyte
62
67
myhexstring = invsubbyte (myhexstring )
63
- print ("The output after invsubbyte is: %s" % myhexstring )
68
+ # print("The output after invsubbyte is: %s" % myhexstring)
64
69
65
70
for x in range (8 , - 1 , - 1 ):
66
- print ("Round: %i" % (x + 1 ))
71
+ # print("Round: %i" % (x+1))
67
72
# add roundkey for current round
68
73
bv1 = BitVector (hexstring = myhexstring )
69
74
bv2 = BitVector (hexstring = roundkeys [x ])
70
75
resultbv = bv1 ^ bv2
71
76
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))
73
78
# mix column
74
79
bv3 = BitVector (hexstring = myhexstring )
75
80
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))
77
82
# shift rows
78
83
myhexstring = invshiftrow (myhexstring )
79
- print ("The output after invshiftrow is: %s" % myhexstring )
84
+ # print("The output after invshiftrow is: %s" % myhexstring)
80
85
# sub byte
81
86
myhexstring = invsubbyte (myhexstring )
82
- print ("The output after invsubbyte is: %s" % myhexstring )
87
+ # print("The output after invsubbyte is: %s" % myhexstring)
83
88
84
89
#add initial round key
85
90
bv1 = BitVector (hexstring = myhexstring )
86
91
bv2 = PassPhrase
87
92
resultbv = bv1 ^ bv2
88
93
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)
90
95
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
93
98
94
- outputhex = myhexstring + outputhex
99
+ outputhex = BitVector (hexstring = myhexstring )
100
+ asciioutput = outputhex .get_bitvector_in_ascii ()
101
+ FILEOUT .write (asciioutput )
95
102
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 )
102
103
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 ()
0 commit comments