Skip to content
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

Transposition Encryption #1

Merged
merged 1 commit into from
Sep 30, 2020
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
65 changes: 62 additions & 3 deletions enyo/enyodecryption.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import math
class EnyoDecryption:
numSet = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', 5: 'F', 6: 'G', 7: 'H', 8: 'I', 9: 'J', 10: 'K', 11: 'L', 12: 'M', 13: 'N', 14: 'O', 15: 'P', 16: 'Q', 17: 'R', 18: 'S', 19: 'T', 20: 'U', 21: 'V', 22: 'W', 23: 'X', 24: 'Y', 25: 'Z', 26: 'a', 27: 'b', 28: 'c', 29: 'd', 30: 'e', 31: 'f', 32: 'g', 33: 'h', 34: 'i', 35: 'j', 36: 'k', 37: 'l', 38: 'm', 39: 'n', 40: 'o', 41: 'p', 42: 'q', 43: 'r', 44: 's', 45: 't', 46: 'u', 47: 'v', 48: 'w', 49: 'x', 50: 'y', 51: 'z', 52: '0', 53: '1', 54: '2', 55: '3', 56: '4', 57: '5', 58: '6', 59: '7', 60: '8', 61: '9', 62:'$',63:'@'}

charSet = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7, 'I': 8, 'J': 9, 'K': 10, 'L': 11, 'M': 12, 'N': 13, 'O': 14, 'P': 15, 'Q': 16, 'R': 17, 'S': 18, 'T': 19, 'U': 20, 'V': 21, 'W': 22, 'X': 23, 'Y': 24, 'Z': 25, 'a': 26, 'b': 27, 'c': 28, 'd': 29, 'e': 30, 'f': 31, 'g': 32, 'h': 33, 'i': 34, 'j': 35, 'k': 36, 'l': 37, 'm': 38, 'n': 39, 'o': 40, 'p': 41, 'q': 42, 'r': 43, 's': 44, 't': 45, 'u': 46, 'v': 47, 'w': 48, 'x': 49, 'y': 50, 'z': 51, '0': 52, '1': 53, '2': 54, '3': 55, '4': 56, '5': 57, '6': 58, '7': 59, '8': 60, '9': 61, '$':62, '@':63 }

def __init__(self,text,secret,partition=2):
self.encrypted = self.moduloDecryption(text) # Modular Decryption
self.secret = secret # Secret key for decryption
self.part = self.partitionChecker(partition)
self.ekey = self.keyPartioning(self.encode(self.secret),self.part) # Partition based encoded secret key
self.newCharSet, self.newNumSet = self.charSetModifier(self.ekey) # Modified character sets
self.key = self.partition(self.ekey,self.part) # Final secret key array for encryption
self.decrypted = self.decode(self.decryption()) # Multistage XOR Decryption + Decoding
self.encrypted = self.moduloDecryption(self.transpositionDecryption(text)) # Modular + Transposition Decryption
self.decrypted = self.decode(self.decryption()) # Multistage XOR Decryption + Decoding

def encode(self,str):
bits = ""
Expand Down Expand Up @@ -95,7 +96,65 @@ def partitionChecker(self,part):
raise Exception("Number of partitions not possible.")
else:
return part


def findMatrixSize(self,encrypt):
size = math.sqrt(len(encrypt))
return min(int(size),len(self.key[0]))

def sequenceGenerator(self,key):
initialSequence = []
for i in range(0,len(key)):
initialSequence.append([self.newCharSet[key[i]]])
rank = 0
while(True):
flag = 0
minimum = 64 # Values can't be greater than 63 in initialSequence
for i in range(0,len(initialSequence)):
if(initialSequence[i][0]<minimum and len(initialSequence[i])!=2):
minimum = initialSequence[i][0]
position = i
initialSequence[position].append(rank)
rank += 1
for i in range(0,len(initialSequence)):
if(len(initialSequence[i])!=2):
flag += 1
if(flag==0):
break
finalSequence = []
for i in range(0,len(initialSequence)):
finalSequence.append(initialSequence[i][1])
return finalSequence

def transpositionDecryption(self,encrypt):
decrypted = ""
size = self.findMatrixSize(encrypt) #Dimension of transposition matrix
matrix = []
# Initializing matrix
for i in range(size):
temp = []
for j in range(size):
temp.append('0')
matrix.append(temp)


for i in range(len(self.key)-1,-1,-1):
keyWindow = self.key[i][0:size]
sequence = self.sequenceGenerator(keyWindow)
for x in range(len(encrypt)-size*size,-1,-1):
window = encrypt[x:x+size*size]
c = 0
for j in sequence:
for y in range(size):
matrix[y][j] = window[c]
c += 1
c = 0
window = ""
for m in range(size):
for n in range(size):
window += matrix[m][n]
encrypt = encrypt[0:x]+window+encrypt[x+size*size:]
return encrypt

def decryption(self):
index = 0
decrypted = ""
Expand Down
68 changes: 62 additions & 6 deletions enyo/enyoencryption.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math
class EnyoEncryption:
numSet = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', 5: 'F', 6: 'G', 7: 'H', 8: 'I', 9: 'J', 10: 'K', 11: 'L', 12: 'M', 13: 'N', 14: 'O', 15: 'P', 16: 'Q', 17: 'R', 18: 'S', 19: 'T', 20: 'U', 21: 'V', 22: 'W', 23: 'X', 24: 'Y', 25: 'Z', 26: 'a', 27: 'b', 28: 'c', 29: 'd', 30: 'e', 31: 'f', 32: 'g', 33: 'h', 34: 'i', 35: 'j', 36: 'k', 37: 'l', 38: 'm', 39: 'n', 40: 'o', 41: 'p', 42: 'q', 43: 'r', 44: 's', 45: 't', 46: 'u', 47: 'v', 48: 'w', 49: 'x', 50: 'y', 51: 'z', 52: '0', 53: '1', 54: '2', 55: '3', 56: '4', 57: '5', 58: '6', 59: '7', 60: '8', 61: '9', 62:'$',63:'@'}

Expand All @@ -11,8 +12,8 @@ def __init__(self,text,secret,partition=2):
self.ekey = self.keyPartioning(self.encode(self.secret),self.part) # Partition based encoded secret key
self.newCharSet, self.newNumSet = self.charSetModifier(self.ekey) # Modified character sets
self.key = self.partition(self.ekey,self.part) # Final secret key array for encryption
self.encrypted = self.moduloEncryption(self.encryption()) # Multistage XOR + Modular Encryption
self.encrypted = self.transpositionEncryption(self.moduloEncryption(self.encryption())) # Multistage XOR + Modular Encryption + Transposition

def encode(self,str):
bits = ""
encodedWord = ""
Expand Down Expand Up @@ -85,7 +86,64 @@ def partitionChecker(self,part):
raise Exception("Number of partitions not possible.")
else:
return part


def findMatrixSize(self,encrypt):
size = math.sqrt(len(encrypt))
return min(int(size),len(self.key[0]))

def sequenceGenerator(self,key):
initialSequence = []
for i in range(0,len(key)):
initialSequence.append([self.newCharSet[key[i]]])
rank = 0
while(True):
flag = 0
minimum = 64 # Values can't be greater than 63 in initialSequence
for i in range(0,len(initialSequence)):
if(initialSequence[i][0]<minimum and len(initialSequence[i])!=2):
minimum = initialSequence[i][0]
position = i
initialSequence[position].append(rank)
rank += 1
for i in range(0,len(initialSequence)):
if(len(initialSequence[i])!=2):
flag += 1
if(flag==0):
break
finalSequence = []
for i in range(0,len(initialSequence)):
finalSequence.append(initialSequence[i][1])
return finalSequence

def transpositionEncryption(self,encrypt):
encrypted = ""
size = self.findMatrixSize(encrypt) #Dimension of transposition matrix
matrix = []
# Initializing matrix
for i in range(size):
temp = []
for j in range(size):
temp.append('0')
matrix.append(temp)


for keyPart in self.key:
keyWindow = keyPart[0:size]
sequence = self.sequenceGenerator(keyWindow)
for i in range(len(encrypt)-size*size+1):
window = encrypt[i:size*size+i]
x = 0
for j in range(size):
for k in range(size):
matrix[j][k] = window[x]
x += 1
for t in sequence:
for tt in range(size):
encrypted += matrix[tt][t]
encrypt = encrypt[0:i]+encrypted+encrypt[size*size+i:]
encrypted = ""
return encrypt

def encryption(self):
# Encryption function
index = 0
Expand All @@ -111,6 +169,4 @@ def encryption(self):
index += 1
if(index>len(self.key[0])-1):
index = 0
return encrypted


return encrypted