Skip to content

Commit 7a31ef6

Browse files
authored
Merge branch 'master' into add-ChathuminaVimukthi
2 parents a914b47 + 644641d commit 7a31ef6

32 files changed

+1453
-44
lines changed

CONTRIBUTORS.md

+3
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@ Thanks for all your contributions :heart: :octocat:
99
| [de1ux](https://github.com/de1ux) | [Requirements](https://github.com/prateekiiest/Code-Sleep-Python/pull/22) | Merged |
1010
| [mstuttgart](https://github.com/mstuttgart) | [README adjusts](https://github.com/prateekiiest/Code-Sleep-Python/pull/3) | Merged |
1111
| [blossomica](https://github.com/blossomica) |[Fix Broken Code Links](https://github.com/prateekiiest/Code-Sleep-Python/pull/18) | Merged |
12+
| [jasperdchen](https://github.com/jasperdchen) | [README](https://github.com/prateekiiest/Code-Sleep-Python/pull/31) | Merged |
13+
| [Binsquare](https://github.com/binsquare) | [Added project descriptions](https://github.com/prateekiiest/Code-Sleep-Python/pull/26) | Merged |
1214
| [Chathumina Vimukthi](https://github.com/ChathuminaVimukthi) |[Added hangman game](https://github.com/prateekiiest/Code-Sleep-Python/pull/30) | Merged |
1315

16+

Encryption-Techniques/AES/AES256.py

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
from copy import copy
2+
from tables import *
3+
4+
def subBytes(block):
5+
for i in range(len(block)):
6+
block[i] = sbox[block[i]]
7+
return block
8+
9+
def subBytesInv(block):
10+
for i in range(len(block)):
11+
block[i] = sboxInv[block[i]]
12+
return block
13+
14+
def keyScheduleCore(word, i):
15+
newWord = word[1:]+word[:1]
16+
subBytes(newWord)
17+
newWord[0] = newWord[0] ^ rCon[i]
18+
return newWord
19+
20+
def expandKey(cipherKey):
21+
cipherKeySize = len(cipherKey)
22+
assert cipherKeySize == 32
23+
expandedKey = []
24+
currentSize,rconIter = 0,1
25+
26+
t = [0,0,0,0]
27+
28+
for i in range(cipherKeySize):
29+
expandedKey.append(cipherKey[i])
30+
currentSize += cipherKeySize
31+
32+
while currentSize < 240:
33+
for i in range(4):
34+
t[i] = expandedKey[(currentSize - 4) + i]
35+
if currentSize % cipherKeySize == 0:
36+
t = keyScheduleCore(t, rconIter)
37+
rconIter += 1
38+
if currentSize % cipherKeySize == 16:
39+
for i in range(4):
40+
t[i] = sbox[t[i]]
41+
for i in range(4):
42+
expandedKey.append(((expandedKey[currentSize - cipherKeySize]) ^ (t[i])))
43+
currentSize += 1
44+
return expandedKey
45+
46+
def createRoundKey(expandedKey,i):
47+
return expandedKey[i*16:i*16+16]
48+
49+
def addroundkey(block,roundKey):
50+
for i in range(len(block)):
51+
block[i] = block[i] ^ roundKey[i]
52+
return block
53+
54+
def blockfileColumn(filename):
55+
test2 = filename
56+
testarr = []
57+
for row in range(0,4):
58+
for column in range(0,len(test2),4):
59+
testarr.append(test2[row+column])
60+
return testarr
61+
62+
def rotate(rotate, n):
63+
return rotate[n:]+rotate[0:n]
64+
65+
def shiftRow(blockfile):
66+
block = blockfileColumn(blockfile)
67+
for i in range(4):
68+
block[i*4:i*4+4] = rotate(block[i*4:i*4+4], i)
69+
blockDone = blockfileColumn(block)
70+
return blockDone
71+
72+
def shiftRowInv(blockfile):
73+
block = blockfileColumn(blockfile)
74+
for i in range(4):
75+
block[i*4:i*4+4] = rotate(block[i*4:i*4+4], -i)
76+
block = blockfileColumn(block)
77+
return block
78+
79+
def mixColumn(column):
80+
col = copy(column)
81+
column[0] = table_2[col[0]] ^ table_3[col[1]] ^ col[2] ^ col[3]
82+
column[1] = col[0] ^ table_2[col[1]] ^ table_3[col[2]] ^ col[3]
83+
column[2] = col[0] ^ col[1] ^ table_2[col[2]] ^ table_3[col[3]]
84+
column[3] = table_3[col[0]] ^ col[1] ^ col[2] ^ table_2[col[3]]
85+
return column
86+
87+
88+
def mixColumns(col):
89+
outCol = []
90+
for i in range(0,len(col),4):
91+
outCol.append(mixColumn(col[i:i+4]))
92+
return listoflist2singlelist(outCol)
93+
94+
def mixColumnInv(column):
95+
col = copy(column)
96+
column[0] = table_14[col[0]] ^ table_11[col[1]] ^ table_13[col[2]] ^ table_9[col[3]]
97+
column[1] = table_9[col[0]] ^ table_14[col[1]] ^ table_11[col[2]] ^ table_13[col[3]]
98+
column[2] = table_13[col[0]] ^ table_9[col[1]] ^ table_14[col[2]] ^ table_11[col[3]]
99+
column[3] = table_11[col[0]] ^ table_13[col[1]] ^ table_9[col[2]] ^ table_14[col[3]]
100+
return column
101+
102+
103+
def mixColumnsInv(col):
104+
intList = []
105+
for i in range(0,len(col),4):
106+
intList.append(mixColumnInv(col[i:i+4]))
107+
return listoflist2singlelist(intList)
108+
109+
def listoflist2singlelist(column):
110+
columnList = []
111+
for row in column:
112+
for i in range(0,len(row)):
113+
columnList.append(row[i])
114+
return columnList
115+
116+
def encrypt(block,key):
117+
expandedKey = expandKey(key)
118+
roundKey = createRoundKey(expandedKey,0)
119+
block = addroundkey(block,roundKey)
120+
121+
for i in range(1,14):
122+
roundKey = createRoundKey(expandedKey,i)
123+
block = subBytes(block)
124+
block = shiftRow(block)
125+
block = mixColumns(block)
126+
block = addroundkey(block,roundKey)
127+
128+
roundKey = createRoundKey(expandedKey,14)
129+
block = subBytes(block)
130+
block = shiftRow(block)
131+
block = addroundkey(block,roundKey)
132+
return block
133+
134+
def decrypt(block,key):
135+
expandedKey = expandKey(key)
136+
roundKey = createRoundKey(expandedKey,14)
137+
block = addroundkey(block,roundKey)
138+
block = shiftRowInv(block)
139+
block = subBytesInv(block)
140+
141+
for i in range(13,0,-1):
142+
roundKey = createRoundKey(expandedKey,i)
143+
block = addroundkey(block,roundKey)
144+
block = mixColumnsInv(block)
145+
block = shiftRowInv(block)
146+
block = subBytesInv(block)
147+
148+
roundKey = createRoundKey(expandedKey,0)
149+
block = addroundkey(block,roundKey)
150+
return block

Encryption-Techniques/AES/README

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Instructions:
2+
1. The 16-byte hex you want to encrypt is in file 'testBlock'.
3+
2. The key for this encryption is in file 'testKey'. Key is 32 byte hex.
4+
3. To encrypt run -
5+
python main.py encrypt
6+
--- Encrypted text gets written in file 'encrypted_file'
7+
4. To decrypt run -
8+
python main.py decrypt
9+
--- Decrypted text gets written in file 'decrypted_file'
10+

Encryption-Techniques/AES/demo.png

43.6 KB
Loading

Encryption-Techniques/AES/main.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from readfiles import *
2+
from AES256 import *
3+
import argparse
4+
from argparse import RawTextHelpFormatter
5+
import time
6+
7+
parser = argparse.ArgumentParser(description='AES 256-bit encrypt and decrypt', formatter_class=RawTextHelpFormatter)
8+
parser.add_argument('mode',help="encrypt or decrypt")
9+
args = parser.parse_args()
10+
11+
mode = str(args.mode).lower()
12+
13+
def fileEncrypt():
14+
block = getEncryptBlock("testBlock")
15+
key = getKey("testKey")
16+
outfile = open("encrypted_file","wb")
17+
strangen = ""
18+
cryptLargeblock = []
19+
for i in range(len(block)):
20+
cryptLargeblock.append(encrypt(block[i],key))
21+
22+
while i < len(cryptLargeblock):
23+
for row in cryptLargeblock:
24+
for item in row:
25+
strangen += hex(item)[2:].zfill(2)
26+
i += 1
27+
outfile.write(strangen)
28+
outfile.close()
29+
print strangen
30+
31+
def fileDecrypt():
32+
key = getKey("testKey")
33+
file = open("decrypted_file","wb")
34+
decryptBlock = []
35+
decString = ""
36+
eblock = getDecryptBlock("encrypted_file")
37+
38+
for i in range(len(eblock)):
39+
decryptBlock.append(decrypt(eblock[i],key))
40+
41+
for row in decryptBlock:
42+
for item in row:
43+
decString += chr(item)
44+
45+
file.write(decString)
46+
file.close()
47+
print decString
48+
49+
if mode == "encrypt" or mode == "e":
50+
fileEncrypt()
51+
elif mode == "decrypt" or mode == "d":
52+
fileDecrypt()
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
def getKey(filename):
2+
len_key = 64
3+
keyfile = open(filename, 'r')
4+
hexadecimalkey = keyfile.read()
5+
keyarray = []
6+
for i in range(0, len_key, 2):
7+
keyarray.append(int(hexadecimalkey[i:i+2], 16))
8+
return keyarray
9+
10+
def getDecryptBlock(block):
11+
file = open(block, 'r')
12+
byte = file.read()
13+
blockarray = []
14+
block = []
15+
for i in range(0,len(byte),2):
16+
blockarray.append(int(byte[i:i+2],16))
17+
18+
for i in range(0,len(blockarray),16):
19+
block.append(blockarray[i:i+16])
20+
return block
21+
22+
def getEncryptBlock(block):
23+
file = open(block,'rb')
24+
byte = file.read().encode("hex")
25+
blockarray = []
26+
arr = []
27+
for i in range(0,len(byte),2):
28+
arr.append(int(byte[i:i+2], 16))
29+
30+
while len(arr) >= 16:
31+
blockarray.append(arr[0:16])
32+
arr = arr[16:]
33+
if len(arr) < 16:
34+
temparray = [0]*16
35+
for k in range(0,len(arr)):
36+
temparray[k] = arr[k]
37+
blockarray.append(temparray)
38+
return blockarray

0 commit comments

Comments
 (0)