Skip to content

Different encryption techniques added. #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions Encryption-Techniques/AES/AES256.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
from copy import copy
from tables import *

def subBytes(block):
for i in range(len(block)):
block[i] = sbox[block[i]]
return block

def subBytesInv(block):
for i in range(len(block)):
block[i] = sboxInv[block[i]]
return block

def keyScheduleCore(word, i):
newWord = word[1:]+word[:1]
subBytes(newWord)
newWord[0] = newWord[0] ^ rCon[i]
return newWord

def expandKey(cipherKey):
cipherKeySize = len(cipherKey)
assert cipherKeySize == 32
expandedKey = []
currentSize,rconIter = 0,1

t = [0,0,0,0]

for i in range(cipherKeySize):
expandedKey.append(cipherKey[i])
currentSize += cipherKeySize

while currentSize < 240:
for i in range(4):
t[i] = expandedKey[(currentSize - 4) + i]
if currentSize % cipherKeySize == 0:
t = keyScheduleCore(t, rconIter)
rconIter += 1
if currentSize % cipherKeySize == 16:
for i in range(4):
t[i] = sbox[t[i]]
for i in range(4):
expandedKey.append(((expandedKey[currentSize - cipherKeySize]) ^ (t[i])))
currentSize += 1
return expandedKey

def createRoundKey(expandedKey,i):
return expandedKey[i*16:i*16+16]

def addroundkey(block,roundKey):
for i in range(len(block)):
block[i] = block[i] ^ roundKey[i]
return block

def blockfileColumn(filename):
test2 = filename
testarr = []
for row in range(0,4):
for column in range(0,len(test2),4):
testarr.append(test2[row+column])
return testarr

def rotate(rotate, n):
return rotate[n:]+rotate[0:n]

def shiftRow(blockfile):
block = blockfileColumn(blockfile)
for i in range(4):
block[i*4:i*4+4] = rotate(block[i*4:i*4+4], i)
blockDone = blockfileColumn(block)
return blockDone

def shiftRowInv(blockfile):
block = blockfileColumn(blockfile)
for i in range(4):
block[i*4:i*4+4] = rotate(block[i*4:i*4+4], -i)
block = blockfileColumn(block)
return block

def mixColumn(column):
col = copy(column)
column[0] = table_2[col[0]] ^ table_3[col[1]] ^ col[2] ^ col[3]
column[1] = col[0] ^ table_2[col[1]] ^ table_3[col[2]] ^ col[3]
column[2] = col[0] ^ col[1] ^ table_2[col[2]] ^ table_3[col[3]]
column[3] = table_3[col[0]] ^ col[1] ^ col[2] ^ table_2[col[3]]
return column


def mixColumns(col):
outCol = []
for i in range(0,len(col),4):
outCol.append(mixColumn(col[i:i+4]))
return listoflist2singlelist(outCol)

def mixColumnInv(column):
col = copy(column)
column[0] = table_14[col[0]] ^ table_11[col[1]] ^ table_13[col[2]] ^ table_9[col[3]]
column[1] = table_9[col[0]] ^ table_14[col[1]] ^ table_11[col[2]] ^ table_13[col[3]]
column[2] = table_13[col[0]] ^ table_9[col[1]] ^ table_14[col[2]] ^ table_11[col[3]]
column[3] = table_11[col[0]] ^ table_13[col[1]] ^ table_9[col[2]] ^ table_14[col[3]]
return column


def mixColumnsInv(col):
intList = []
for i in range(0,len(col),4):
intList.append(mixColumnInv(col[i:i+4]))
return listoflist2singlelist(intList)

def listoflist2singlelist(column):
columnList = []
for row in column:
for i in range(0,len(row)):
columnList.append(row[i])
return columnList

def encrypt(block,key):
expandedKey = expandKey(key)
roundKey = createRoundKey(expandedKey,0)
block = addroundkey(block,roundKey)

for i in range(1,14):
roundKey = createRoundKey(expandedKey,i)
block = subBytes(block)
block = shiftRow(block)
block = mixColumns(block)
block = addroundkey(block,roundKey)

roundKey = createRoundKey(expandedKey,14)
block = subBytes(block)
block = shiftRow(block)
block = addroundkey(block,roundKey)
return block

def decrypt(block,key):
expandedKey = expandKey(key)
roundKey = createRoundKey(expandedKey,14)
block = addroundkey(block,roundKey)
block = shiftRowInv(block)
block = subBytesInv(block)

for i in range(13,0,-1):
roundKey = createRoundKey(expandedKey,i)
block = addroundkey(block,roundKey)
block = mixColumnsInv(block)
block = shiftRowInv(block)
block = subBytesInv(block)

roundKey = createRoundKey(expandedKey,0)
block = addroundkey(block,roundKey)
return block
10 changes: 10 additions & 0 deletions Encryption-Techniques/AES/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Instructions:
1. The 16-byte hex you want to encrypt is in file 'testBlock'.
2. The key for this encryption is in file 'testKey'. Key is 32 byte hex.
3. To encrypt run -
python main.py encrypt
--- Encrypted text gets written in file 'encrypted_file'
4. To decrypt run -
python main.py decrypt
--- Decrypted text gets written in file 'decrypted_file'

Binary file added Encryption-Techniques/AES/demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions Encryption-Techniques/AES/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from readfiles import *
from AES256 import *
import argparse
from argparse import RawTextHelpFormatter
import time

parser = argparse.ArgumentParser(description='AES 256-bit encrypt and decrypt', formatter_class=RawTextHelpFormatter)
parser.add_argument('mode',help="encrypt or decrypt")
args = parser.parse_args()

mode = str(args.mode).lower()

def fileEncrypt():
block = getEncryptBlock("testBlock")
key = getKey("testKey")
outfile = open("encrypted_file","wb")
strangen = ""
cryptLargeblock = []
for i in range(len(block)):
cryptLargeblock.append(encrypt(block[i],key))

while i < len(cryptLargeblock):
for row in cryptLargeblock:
for item in row:
strangen += hex(item)[2:].zfill(2)
i += 1
outfile.write(strangen)
outfile.close()
print strangen

def fileDecrypt():
key = getKey("testKey")
file = open("decrypted_file","wb")
decryptBlock = []
decString = ""
eblock = getDecryptBlock("encrypted_file")

for i in range(len(eblock)):
decryptBlock.append(decrypt(eblock[i],key))

for row in decryptBlock:
for item in row:
decString += chr(item)

file.write(decString)
file.close()
print decString

if mode == "encrypt" or mode == "e":
fileEncrypt()
elif mode == "decrypt" or mode == "d":
fileDecrypt()
38 changes: 38 additions & 0 deletions Encryption-Techniques/AES/readfiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
def getKey(filename):
len_key = 64
keyfile = open(filename, 'r')
hexadecimalkey = keyfile.read()
keyarray = []
for i in range(0, len_key, 2):
keyarray.append(int(hexadecimalkey[i:i+2], 16))
return keyarray

def getDecryptBlock(block):
file = open(block, 'r')
byte = file.read()
blockarray = []
block = []
for i in range(0,len(byte),2):
blockarray.append(int(byte[i:i+2],16))

for i in range(0,len(blockarray),16):
block.append(blockarray[i:i+16])
return block

def getEncryptBlock(block):
file = open(block,'rb')
byte = file.read().encode("hex")
blockarray = []
arr = []
for i in range(0,len(byte),2):
arr.append(int(byte[i:i+2], 16))

while len(arr) >= 16:
blockarray.append(arr[0:16])
arr = arr[16:]
if len(arr) < 16:
temparray = [0]*16
for k in range(0,len(arr)):
temparray[k] = arr[k]
blockarray.append(temparray)
return blockarray
Loading