Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have implemented the diffie - hellman key exchange, Vigenere cipher, and the Playfair cipher algorithms in python and they all worked well. Now I

I have implemented the diffie-hellman key exchange, Vigenere cipher, and the Playfair cipher algorithms in python and they all worked well. Now I need to combine them to make a three layer security program but I am having issues with that. Can someone help connocting the codes together?
Here is th diffie-hellman code:
#Following the steps of Diffie-Hellman Key Exchange Algorithm
#Step 1: All users agree on glodabl (prime) parameters q and g
q = int(input("
Please enter first prime number (q): "))
g = int(input("
Please enter second prime number (g): "))
#Step 2: Each user selects random secret key
sender_key = int(input("
enter sender public key (must be greater than "+ str(g)+"): "))
receiver_key = int(input("
enter receiver public key (must be greater than "+ str(g)+"): "))
#Step 3: Each user computes respective public keys (Ys = g^Xs mod q) and (Yr = g^Xr mod q)
senderPublic = g**sender_key % q
receiverPublic = g**receiver_key % q
#Step 4: Compute shared session key (Ksr = g^XsXr mod q)
#(Ys^Xr mod q) which receiver can compute, and (Yr^Xs mod q) which sender can compute
senderShared = receiverPublic**sender_key % q
receiverShared = senderPublic**receiver_key % q
print("
The public key (Sender): "+ str(senderPublic))
print("
The public key (Receiver): "+ str(receiverPublic))
print("
The shared key (Sender): "+ str(senderShared))
print("
The shared key (Receiver): "+ str(receiverShared))
print("
")
Here is the vigenere code:
alphabet = "abcdefghijklmnopqrstuvwxyz "
letter_to_index = dict(zip(alphabet, range(len(alphabet))))
index_to_letter = dict(zip(range(len(alphabet)), alphabet))
# For the decryption proccess (add) the key letter's index to the message letter's index
def encrypt(message, key):
encrypted =""
split_message =[
message[i : i + len(key)] for i in range(0, len(message), len(key))
]
for each_split in split_message:
i =0
for letter in each_split:
number =(letter_to_index[letter]+ letter_to_index[key[i]])% len(alphabet)
encrypted += index_to_letter[number]
i +=1
return encrypted
# For the decryption proccess (subtract) the key letter's index from the message letter's index
def decrypt(encrypted_message, key):
decrypted =""
split_message =[
encrypted_message[i : i + len(key)] for i in range(0, len(encrypted_message), len(key))
]
for each_split in split_message:
i =0
for letter in each_split:
number =(letter_to_index[letter]- letter_to_index[key[i]])% len(alphabet)
decrypted += index_to_letter[number]
i +=1
return decrypted
def main():
message = "i love information security"
key = "subject"
encrypted_message = encrypt(message, key)
decrypted_message = decrypt(encrypted_message, key)
print("
Original message: "+ message)
print("
Encrypted message: "+ encrypted_message)
print("
Decrypted message: "+ decrypted_message)
print("
")
main()
And here is the playfair code:
import string
import itertools
def chunker(seq, size):
it = iter(seq)
while True:
chunk = tuple(itertools.islice(it, size))
if not chunk:
return
yield chunk
def prepare_input(dirty):
dirty ="".join([c.upper() for c in dirty if c in string.ascii_letters])
clean =""
if len(dirty)<2:
return dirty
for i in range(len(dirty)-1):
clean += dirty[i]
if dirty[i]== dirty[i +1]:
clean +="X"
clean += dirty[-1]
if len(clean) & 1:
clean +="X"
return clean
def generate_table(key):
alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ"
table =[]
for char in key.upper():
if char not in table and char in alphabet:
table.append(char)
for char in alphabet:
if char not in table:
table.append(char)
return table
def encode(plaintext, key):
table = generate_table(key)
plaintext = prepare_input(plaintext)
ciphertext =""
for char1, char2 in chunker(plaintext,2):
row1, col1= divmod(table.index(char1),5)
row2, col2= divmod(table.index(char2),5)
if row1== row2:
ciphertext += table[row1*5+(col1+1)%5]
ciphertext += table[row2*5+(col2+1)%5]
elif col1== col2:
ciphertext += table[((row1+1)%5)*5+ col1]
ciphertext += table[((row2+1)%5)*5+ col2]
else:
ciphertext += table[row1*5+ col2]
ciphertext += table[row2*5+ col1]
return ciphertext

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions