Professional Documents
Culture Documents
"""
@author: Abdullah
"""
#RC6 Algorithm have been cloned from an open source Github repository. The algorithm was initially
intended for Python 2 so requisite changes have been made
#https://github.com/shashankrao/RC6-Block-Cipher
import rsa
import time
#Helper Functions
mask = (2**n) - 1
def blockConverter(sentence):
encoded = []
res = ""
for i in range(0,len(sentence)):
encoded.append(res)
res = ""
temp = bin(ord(sentence[i]))[2:]
if len(temp) <8:
return encoded
def deBlocker(blocks):
s = ""
temp =bin(ele)[2:]
if len(temp) <32:
for i in range(0,4):
s=s+chr(int(temp[i*8:(i+1)*8],2))
return s
def generateKeyRC6(userkey):
r=12
w=32
b=len(userkey)
modulo = 2**32
s=(2*r+4)*[0]
s[0]=0xB7E15163
for i in range(1,2*r+4):
s[i]=(s[i-1]+0x9E3779B9)%(2**w)
encoded = blockConverter(userkey)
#print encoded
enlength = len(encoded)
l = enlength*[0]
for i in range(1,enlength+1):
l[enlength-i]=int(encoded[i-1],2)
v = 3*max(enlength,2*r+4)
A=B=i=j=0
i = (i + 1) % (2*r + 4)
j = (j + 1) % enlength
return s
def encryptRC6(sentence,s):
encoded = blockConverter(sentence)
enlength = len(encoded)
A = int(encoded[0],2)
B = int(encoded[1],2)
C = int(encoded[2],2)
D = int(encoded[3],2)
orgi = []
orgi.append(A)
orgi.append(B)
orgi.append(C)
orgi.append(D)
r=12
w=32
modulo = 2**32
lgw = 5
B = (B + s[0])%modulo
D = (D + s[1])%modulo
for i in range(1,r+1):
t = ROL(t_temp,lgw,32)
u = ROL(u_temp,lgw,32)
tmod=t%32
umod=u%32
A = (ROL(A^t,umod,32) + s[2*i])%modulo
(A, B, C, D) = (B, C, D, A)
A = (A + s[2*r + 2])%modulo
C = (C + s[2*r + 3])%modulo
cipher = []
cipher.append(A)
cipher.append(B)
cipher.append(C)
cipher.append(D)
return orgi,cipher
def decryptRC6(esentence,s):
encoded = blockConverter(esentence)
enlength = len(encoded)
A = int(encoded[0],2)
B = int(encoded[1],2)
C = int(encoded[2],2)
D = int(encoded[3],2)
cipher = []
cipher.append(A)
cipher.append(B)
cipher.append(C)
cipher.append(D)
r=12
w=32
modulo = 2**32
lgw = 5
C = (C - s[2*r+3])%modulo
A = (A - s[2*r+2])%modulo
for j in range(1,r+1):
i = r+1-j
(A, B, C, D) = (D, A, B, C)
u = ROL(u_temp,lgw,32)
t = ROL(t_temp,lgw,32)
tmod=t%32
umod=u%32
C = (ROR((C-s[2*i+1])%modulo,tmod,32) ^u)
A = (ROR((A-s[2*i])%modulo,umod,32) ^t)
D = (D - s[1])%modulo
B = (B - s[0])%modulo
orgi = []
orgi.append(A)
orgi.append(B)
orgi.append(C)
orgi.append(D)
return cipher,orgi
def generate_keys_rsa():
try:
except:
return False
try:
except:
return False
pubKey, privKey = generate_keys_rsa()
#Vigenere Encryption
key = list(key)
if len(string) == len(key):
return(key)
else:
key.append(key[i % len(key)])
return("" . join(key))
encrypt_text = []
for i in range(len(string)):
x = (ord(string[i]) +ord(key[i])) % 26
x += ord('A')
encrypt_text.append(chr(x))
return("" . join(encrypt_text))
orig_text = []
for i in range(len(encrypt_text)):
x += ord('A')
orig_text.append(chr(x))
return("" . join(orig_text))
#Generation of Private Key and Plain Text as indicated in the block diagram
print('_________________________________________________')
print('_________________________________________________')
start_time_hybrid = time.time()
start_time = time.time()
encrypt_key = encryption_vigenere(PrivateKey,key)
print('_________________________________________________')
start_time = time.time()
key_RC6 = encrypt_key
#This is to enforce 16 characters
if len(key_RC6) <16:
key_RC6 = key_RC6[:16]
sentence = PlainText
#Enforcement to 16 characters
if len(sentence) <16:
sentence = sentence[:16]
orgi,cipher = encryptRC6(sentence,s)
esentence = deBlocker(cipher)
print('_________________________________________________')
start_time = time.time()
#Decode RSA
#Decode RC6
cipher,orgi = decryptRC6(DeocodedMessageRSA,s)
sentence = deBlocker(orgi)
print('_________________________________________________')
print('____________________________________________________')
print('____________________________________________________')
print("vigenere")
print('____________________________________________________')
print("RC6")
print("RSA")
print('____________________________________________________')
print("Hybrid Model")