-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAESencrypt.py
173 lines (148 loc) · 6.53 KB
/
AESencrypt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from AESencryptfunc import * #import AESencryptfunc module to use functions created for this program
import math #import math module to use function such as ceiling
import scrypt
#check that script is running with the two text files as the two parameters or else quit
if len(sys.argv) is not 3:#takes in two arguments for the plaintext.txt file name and cipherhex.txt file name
sys.exit("Error, script needs two command-line arguments. (Plaintext.txt File and cipherhex.txt File)")
# set password to be a 16 characters, 16 characters * 8 bits = 128 bits strength
PassPhrase=""
def string2bits(par=""):
return [bin(ord(x))[2:].zfill(8) for x in par]
def xor_two_str(a,b):
return ''.join([hex(ord(a[i%len(a)]) ^ ord(b[i%(len(b))]))[2:] for i in range(max(len(a), len(b)))])
def Master_Key_128(K1,K2):
#Key Mixing-Start
K1_1=string2bits(K1)[0:8]
K1_2=string2bits(K2)[8:16]
K2_1=string2bits(K2)[0:8]
K2_2=string2bits(K2)[8:16]
result=[]
for i in range(8):
result.append(xor_two_str(K2_1[i],xor_two_str(K1_1[i],xor_two_str(K1_2[i],K2_2[i]))))
#Key Mixing-End
#Key Expansion-Start
K1_1_result=''.join(i for i in K1_1)
K1_2_result=''.join(i for i in K1_2)
K2_1_result=''.join(i for i in K2_1)
K2_2_result=''.join(i for i in K2_2)
temp=""
for i in range(1,17):
temp=temp+"".join(K1_1_result[4*i-1])+"".join(K1_2_result[4*i-1])+"".join(K2_1_result[4*i-1])+"".join(K2_2_result[4*i-1])
for i in range(8):
result.append(("".join(j for j in list(temp)[8*i:8*i+8])))
#KeyExpansion-End
#Converting Final Key into characters from binary
finalkey=''
for i in range(16):
if(int(result[i],2)<32):
finalkey=finalkey+chr(int(result[i],2)+33)
else:
finalkey=finalkey+chr(int(result[i],2))
salt = 'd7d42612c5ff4625'
hashed_key=scrypt.hash(finalkey,salt)
return [finalkey,hashed_key]
K1=input("Enter K1: ")
K2=input("Enter K2: ")
print("\n")
k=Master_Key_128(K1,K2)
print("Hashed Key using Scrypt",k[1])
print("\n")
while(len(PassPhrase)!=16):
print("Enter in the 16 character passphrase to encrypt your text file %s" %sys.argv[1])
PassPhrase=k[0] #takes in user input of char, eg. "Iwanttolearnkung"
if(len(PassPhrase)<16):#check if less than 16 characters, if so add one space character until 16 chars
while(len(PassPhrase)!=16):
PassPhrase=PassPhrase+"\00"
if(len(PassPhrase)>16):#check if bigger than 16 characters, if so then truncate it to be only 16 chars from [0:16]
print("Your passphrase was larger than 16, truncating passphrase.")
PassPhrase=PassPhrase[0:16]
#open plaintext.txt file to read and encrypt
file=open(sys.argv[1], "r")
message=(file.read())
print("Inside your plaintext message is:\n%s\n" % message)
file.close()
message=BitVector(textstring=message)
message=message.get_bitvector_in_hex()
replacementptr=0
while(replacementptr<len(message)):
if(message[replacementptr:replacementptr+2]=='0a'):
message=message[0:replacementptr]+'0d'+message[replacementptr:len(message)]
replacementptr=replacementptr+4
else:
replacementptr=replacementptr+2
message=BitVector(hexstring=message)
message=message.get_bitvector_in_ascii()
#set up some parameters
start=0#set starting pointer for the part to encrypt of the plaintext
end=0#set ending pointer for the part to encrypt of the plaintex
length=len(message)#check the entire size of the message
loopmsg=0.00#create a decimal value
loopmsg=math.ceil(length/16)+1#use formula to figure how long the message is and how many 16 character segmentss must be encrypted
outputhex=""#setup output message in hex
#need to setup roundkeys here
PassPhrase=BitVector(textstring=PassPhrase)
roundkey1=findroundkey(PassPhrase.get_bitvector_in_hex(),1)
roundkey2=findroundkey(roundkey1,2)
roundkey3=findroundkey(roundkey2,3)
roundkey4=findroundkey(roundkey3,4)
roundkey5=findroundkey(roundkey4,5)
roundkey6=findroundkey(roundkey5,6)
roundkey7=findroundkey(roundkey6,7)
roundkey8=findroundkey(roundkey7,8)
roundkey9=findroundkey(roundkey8,9)
roundkey10=findroundkey(roundkey9,10)
roundkeys=[roundkey1,roundkey2,roundkey3,roundkey4,roundkey5,roundkey6,roundkey7,roundkey8,roundkey9,roundkey10]
#set up FILEOUT to write
FILEOUT = open(sys.argv[2], 'w')
# set up the segement message loop parameters
for y in range(1, loopmsg): # loop to encrypt all segments of the message
if(end+16<length): #if the end pointer is less than the size of the message, then set the segment to be 16 characters
plaintextseg = message[start:end + 16]
else: #or else if the end pointer is equal to or greator than the size of the message
plaintextseg = message[start:length]
for z in range(0,((end+16)-length),1): #run a while loop to pad the message segement to become 16 characters, if it is 16 already the loop will not run
plaintextseg = plaintextseg+"\00"
#plaintextseg2=BitVector(textstring=plaintextseg)
#print(plaintextseg2.get_bitvector_in_hex())
#add round key zero/ find round key one
bv1 = BitVector(textstring=plaintextseg)
bv2 = PassPhrase
resultbv=bv1^bv2
myhexstring = resultbv.get_bitvector_in_hex()
for x in range(1, 10): # loop through 9 rounds
# sub byte
myhexstring = resultbv.get_bitvector_in_hex()
temp1=subbyte(myhexstring)
# shift rows
temp2=shiftrow(temp1)
# mix column
bv3 = BitVector(hexstring=temp2)
newbvashex=mixcolumn(bv3)
newbv=BitVector(hexstring=newbvashex)
#add roundkey for current round
bv1 = BitVector(bitlist=newbv)
bv2 = BitVector(hexstring=roundkeys[x-1])
resultbv = bv1 ^ bv2
myhexresult = resultbv.get_bitvector_in_hex()
#start round 10
# sub byte round 10
myhexstring = resultbv.get_bitvector_in_hex()
temp1=subbyte(myhexstring)
# shift rows round 10
temp2=shiftrow(temp1)
# add round key round 10
newbv = BitVector(hexstring=temp2)
bv1 = BitVector(bitlist=newbv)
bv2 = BitVector(hexstring=roundkeys[9])
resultbv = bv1 ^ bv2
myhexstring = resultbv.get_bitvector_in_hex()
#set encrypted hex segement of message to output string
outputhextemp = resultbv.get_hex_string_from_bitvector()
FILEOUT.write(outputhextemp)
start = start + 16 #increment start pointer
end = end + 16 #increment end pointer
# encrypted output hex string to specified cipherhex file
FILEOUT.close()
file2=open(sys.argv[2], "r")
print("The output hex value for the entire message is:\n%s\n" % file2.read())
file2.close()