You are on page 1of 12

PrivateKey & PlainText

Encrypt Private Key using


Vigenere Encryption

Use Encrypted Prviate Key as


keytext to the RC6 algorithm
and Excrypt PlainText
(message(

Encrypt the RC6 excrypted


message once again using RSA

Code is given as below:

# -*- coding: utf-8 -*-

"""

Created on Tue Jul 26 11:14:37 2022

@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

#rotate right input x, by n bits

def ROR(x, n, bits = 32):

mask = (2**n) - 1

mask_bits = x & mask

return (x >> n) | (mask_bits << (bits - n))

#rotate left input x, by n bits

def ROL(x, n, bits = 32):

return ROR(x, bits - n,bits)

#convert input sentence into blocks of binary

#creates 4 blocks of binary each of 32 bits.

def blockConverter(sentence):

encoded = []

res = ""

for i in range(0,len(sentence)):

if i%4==0 and i!=0 :

encoded.append(res)

res = ""

temp = bin(ord(sentence[i]))[2:]

if len(temp) <8:

temp = "0"*(8-len(temp)) + temp

res = res + temp


encoded.append(res)

return encoded

#converts 4 blocks array of long int into string

def deBlocker(blocks):

s = ""

for ele in blocks:

temp =bin(ele)[2:]

if len(temp) <32:

temp = "0"*(32-len(temp)) + temp

for i in range(0,4):

s=s+chr(int(temp[i*8:(i+1)*8],2))

return s

#generate key s[0... 2r+3] from given input string userkey

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

for index in range(0,v):

A = s[i] = ROL((s[i] + A + B)%modulo,3,32)

B = l[j] = ROL((l[j] + A + B)%modulo,(A+B)%32,32)

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_temp = (B*(2*B + 1))%modulo

t = ROL(t_temp,lgw,32)

u_temp = (D*(2*D + 1))%modulo

u = ROL(u_temp,lgw,32)

tmod=t%32

umod=u%32

A = (ROL(A^t,umod,32) + s[2*i])%modulo

C = (ROL(C^u,tmod,32) + s[2*i+ 1])%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_temp = (D*(2*D + 1))%modulo

u = ROL(u_temp,lgw,32)

t_temp = (B*(2*B + 1))%modulo

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

#Below are the RSA Functions

def generate_keys_rsa():

(pubKey, privKey) = rsa.newkeys(1024)

return pubKey, privKey

def encrypt_rsa(msg, key):

return rsa.encrypt(msg.encode('utf-8'), key)

def decrypt_rsa(ciphertext, key):

try:

return rsa.decrypt(ciphertext, key).decode('utf-8')

except:

return False

def sign_sha1(msg, key):

return rsa.sign(msg.encode('utf-8'), key, 'SHA-1')

def verify_sha1(msg, signature, key):

try:

return rsa.verify(msg.encode('utf-8'), signature, key) == 'SHA-1'

except:

return False
pubKey, privKey = generate_keys_rsa()

#Vigenere Encryption

def generateKey_vigenere(string, key):

key = list(key)

if len(string) == len(key):

return(key)

else:

for i in range(len(string) -len(key)):

key.append(key[i % len(key)])

return("" . join(key))

def encryption_vigenere(string, 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))

def decryption_vigenere(encrypt_text, key):

orig_text = []

for i in range(len(encrypt_text)):

x = (ord(encrypt_text[i]) -ord(key[i]) + 26) % 26

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

PrivateKey= input('Enter a Private Key: ')

print('_________________________________________________')

print('Private Key is: ', PrivateKey)

PlainText = input('Enter a Message: ')

print('_________________________________________________')

print('Plain Text is: ', PlainText)

start_time_hybrid = time.time()

start_time = time.time()

#Key Encryption using Vigenere

keyword = 'THISISVIEGENERE' #vigenere local key

key = generateKey_vigenere(PrivateKey, keyword)

encrypt_key = encryption_vigenere(PrivateKey,key)

print('_________________________________________________')

print("Private Key Encrypted:", encrypt_key)

time_vigenere = time.time() - start_time

start_time = time.time()

#Encrypt Message with RC6 using the encrypted key of Vigenere

key_RC6 = encrypt_key
#This is to enforce 16 characters

if len(key_RC6) <16:

key_RC6 = key_RC6 + " "*(16-len(key_RC6))

key_RC6 = key_RC6[:16]

sentence = PlainText

#Enforcement to 16 characters

if len(sentence) <16:

sentence = sentence + " "*(16-len(sentence))

sentence = sentence[:16]

s = generateKeyRC6(key_RC6) #This is the private key for RC6

#Encryption of PlainText with RC6 using the encrypted key

orgi,cipher = encryptRC6(sentence,s)

esentence = deBlocker(cipher)

#Message Encrypted using RC6

print('_________________________________________________')

print('Message Encrypted using RC6: ', esentence)

time_RC6 = time.time() - start_time

start_time = time.time()

#Encryption of Key and Message using RSA

DataEncryption2 = encrypt_rsa(esentence, pubKey)

signature = sign_sha1(esentence, privKey)


print('_________________________________________________')

print('Message Encrypted using RSA: ', DataEncryption2)

time_RSA = time.time() - start_time

time_hybrid = time.time() - start_time_hybrid

#Decryption Process Starts Here

#Decode RSA

DeocodedMessageRSA = decrypt_rsa(DataEncryption2, privKey)

#Decode RC6

cipher,orgi = decryptRC6(DeocodedMessageRSA,s)

sentence = deBlocker(orgi)

print('_________________________________________________')

print('Decoded Message: ', sentence)

print('____________________________________________________')

print('Time Parameters (Encryption')

print('____________________________________________________')

print("vigenere")

print("--- %s seconds ---" % (time_vigenere))

print('____________________________________________________')

print("RC6")

print("--- %s seconds ---" % (time_RC6))


print('____________________________________________________')

print("RSA")

print("--- %s seconds ---" % (time_RSA))

print('____________________________________________________')

print("Hybrid Model")

print("--- %s seconds ---" % (time_hybrid))

You might also like