IS Lab 1 - Progress So Far
This commit is contained in:
parent
e14d40f9b2
commit
518399472f
7 changed files with 348 additions and 1 deletions
|
@ -1 +0,0 @@
|
|||
def
|
127
IS/Lab/Lab1/q1_monosub.py
Normal file
127
IS/Lab/Lab1/q1_monosub.py
Normal file
|
@ -0,0 +1,127 @@
|
|||
## ptext = plaintext
|
||||
## ak = additive key
|
||||
from collections import defaultdict
|
||||
|
||||
def add_cipher_en(ptext, ak):
|
||||
result = ""
|
||||
for i in range(len(ptext)):
|
||||
ch = ptext[i]
|
||||
if 32 <= ord(ch) <= 47:
|
||||
result += ch
|
||||
elif 90 >= ord(ch) >= 65:
|
||||
result += chr((ord(ch) + ak - 65) % 26 + 65)
|
||||
else:
|
||||
result += chr((ord(ch) + ak - 97) % 26 + 97)
|
||||
return result
|
||||
|
||||
def add_cipher_de(ctext, ak):
|
||||
result = ""
|
||||
for i in range(len(ctext)):
|
||||
ch = ctext[i]
|
||||
if 32 <= ord(ch) <= 47:
|
||||
result += ch
|
||||
elif 90 >= ord(ch) >= 65:
|
||||
result += chr((ord(ch) - ak - 65) % 26 + 65)
|
||||
else:
|
||||
result += chr((ord(ch) - ak - 97) % 26 + 97)
|
||||
return result
|
||||
|
||||
def mult_cipher_en(ptext, mk):
|
||||
result = ""
|
||||
for i in range(len(ptext)):
|
||||
ch = ptext[i]
|
||||
if 32 <= ord(ch) <= 47:
|
||||
result += ch
|
||||
elif 90 >= ord(ch) >= 65:
|
||||
result += chr((ord(ch) * mk - 65) % 26 + 65)
|
||||
else:
|
||||
result += chr((ord(ch) * mk - 97) % 26 + 97)
|
||||
return result
|
||||
|
||||
def mult_cipher_de(ctext, mk):
|
||||
result = ""
|
||||
inverse = pow(mk, -1, 26)
|
||||
for i in range(len(ctext)):
|
||||
ch = ctext[i]
|
||||
if 32 <= ord(ch) <= 47:
|
||||
result += ch
|
||||
elif 90 >= ord(ch) >= 65:
|
||||
result += chr((ord(ch) * inverse - 65) % 26 + 65)
|
||||
else:
|
||||
result += chr((ord(ch) * inverse - 97) % 26 + 97)
|
||||
return result
|
||||
|
||||
def affine_en(ptext, ak, mk):
|
||||
result = ""
|
||||
for i in range(len(ptext)):
|
||||
ch = ptext[i]
|
||||
if 32 <= ord(ch) <= 47:
|
||||
result += ch
|
||||
elif 90 >= ord(ch) >= 65:
|
||||
result += chr((((ord(ch) - 65) * mk) + ak) % 26 + 65)
|
||||
else:
|
||||
result += chr((((ord(ch) - 97) * mk) + ak) % 26 + 97)
|
||||
return result
|
||||
|
||||
def affine_de(ctext, ak, mk):
|
||||
result = ""
|
||||
inverse = pow(mk, -1, 26)
|
||||
for i in range(len(ctext)):
|
||||
ch = ctext[i]
|
||||
if 32 <= ord(ch) <= 47:
|
||||
result += ch
|
||||
elif 90 >= ord(ch) >= 65:
|
||||
result += chr((((ord(ch) - 65 - ak) * inverse) % 26) + 65)
|
||||
else:
|
||||
result += chr((((ord(ch) - 97 - ak) * inverse) % 26) + 97)
|
||||
return result
|
||||
|
||||
def operator(argument,ptext,ak,mk):
|
||||
match argument:
|
||||
case '1':
|
||||
print("Additive Cipher")
|
||||
print("Plaintext: ", ptext)
|
||||
print("Additive Key: ", ak)
|
||||
ctext = add_cipher_en(ptext, ak)
|
||||
print("Ciphertext: ", ctext)
|
||||
print("Decrypted Text: ", add_cipher_de(ctext, ak))
|
||||
case '2':
|
||||
print("Multiplicative Cipher")
|
||||
print("Plaintext: ", ptext)
|
||||
print("Multiplicative Key: ", mk)
|
||||
ctext = mult_cipher_en(ptext, mk)
|
||||
print("Ciphertext: ", ctext)
|
||||
print("Decrypted Text: ", mult_cipher_de(ctext, mk))
|
||||
case '3':
|
||||
print("Affine Cipher")
|
||||
print("Plaintext: ", ptext)
|
||||
print("Additive Key: ", ak)
|
||||
print("Multiplicative Key: ", mk)
|
||||
ctext = affine_en(ptext, ak, mk)
|
||||
print("Ciphertext: ", ctext)
|
||||
print("Decrypted Text: ", affine_de(ctext, ak, mk))
|
||||
case '4':
|
||||
print("Goodbye")
|
||||
exit()
|
||||
case _:
|
||||
print("Invalid Choice, please try again.")
|
||||
|
||||
def main():
|
||||
ptext = input("Kindly enter your desired plaintext: ")
|
||||
ak = 20
|
||||
mk = 15
|
||||
|
||||
print("Welcome to the substitution cipher system.")
|
||||
print("Enter your choice of algorithm")
|
||||
print("1. Additive Cipher")
|
||||
print("2. Multiplicative Cipher")
|
||||
print("3. Affine Cipher")
|
||||
print("4. Exit")
|
||||
|
||||
while True:
|
||||
op = input("Enter your choice of operation: ")
|
||||
operator(op, ptext, ak, mk)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
118
IS/Lab/Lab1/q2_autokey.py
Normal file
118
IS/Lab/Lab1/q2_autokey.py
Normal file
|
@ -0,0 +1,118 @@
|
|||
def vigenere_en(ptext, vk):
|
||||
result = ""
|
||||
ptext = ptext.upper()
|
||||
vk = vk.upper()
|
||||
|
||||
kl = len(vk) ## key length
|
||||
|
||||
for i in range(len(ptext)):
|
||||
ch = ptext[i]
|
||||
if 32 <= ord(ch) <= 47:
|
||||
result += ch
|
||||
elif 65 <= ord(ch) <= 90:
|
||||
shift = (ord(vk[i % kl]) - ord('A')) % 26
|
||||
result += chr((ord(ch) - ord('A') + shift) % 26 + ord('A'))
|
||||
else:
|
||||
result += ch
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def vigenere_de(ctext, vk):
|
||||
result = ""
|
||||
ctext = ctext.upper()
|
||||
vk = vk.upper()
|
||||
|
||||
kl = len(vk)
|
||||
|
||||
for i in range(len(ctext)):
|
||||
ch = ctext[i]
|
||||
if 32 <= ord(ch) <= 47:
|
||||
result += ch
|
||||
elif 65 <= ord(ch) <= 90:
|
||||
shift = (ord(vk[i % kl]) - ord('A')) % 26
|
||||
result += chr((ord(ch) - ord('A') - shift + 26) % 26 + ord('A'))
|
||||
else:
|
||||
result += ch
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def autokey_en(ptext, ak):
|
||||
result = ""
|
||||
ptext = ptext.upper()
|
||||
current_key = ak
|
||||
|
||||
for i in range(len(ptext)):
|
||||
ch = ptext[i]
|
||||
if 32 <= ord(ch) <= 47:
|
||||
result += ch
|
||||
elif 65 <= ord(ch) <= 90:
|
||||
shift = (current_key - ord('A')) % 26
|
||||
cipher_char = chr((ord(ch) - ord('A') + shift) % 26 + ord('A'))
|
||||
result += cipher_char
|
||||
current_key = ord(cipher_char)
|
||||
else:
|
||||
result += ch
|
||||
|
||||
return result
|
||||
|
||||
def autokey_de(ctext, ak):
|
||||
result = ""
|
||||
ctext = ctext.upper()
|
||||
current_key = ak
|
||||
|
||||
for i in range(len(ctext)):
|
||||
ch = ctext[i]
|
||||
if 32 <= ord(ch) <= 47:
|
||||
result += ch
|
||||
elif 65 <= ord(ch) <= 90:
|
||||
shift = (current_key - ord('A')) % 26
|
||||
plain_char = chr((ord(ch) - ord('A') - shift + 26) % 26 + ord('A'))
|
||||
result += plain_char
|
||||
current_key = ord(plain_char)
|
||||
else:
|
||||
result += ch
|
||||
|
||||
return result
|
||||
|
||||
def operator(argument,ptext,ak,vk):
|
||||
match argument:
|
||||
case '1':
|
||||
print("Vigenere Cipher")
|
||||
print("Plaintext: ", ptext)
|
||||
print("Vigenere Key: ", vk)
|
||||
ctext = vigenere_en(ptext, vk)
|
||||
print("Ciphertext: ", ctext)
|
||||
print("Decrypted Text: ", vigenere_de(ctext, vk))
|
||||
case '2':
|
||||
print("Autokey Cipher")
|
||||
print("Plaintext: ", ptext)
|
||||
print("Auto Key: ", ak)
|
||||
ctext = autokey_en(ptext, ak)
|
||||
print("Ciphertext: ", ctext)
|
||||
print("Decrypted Text: ", autokey_de(ctext, ak))
|
||||
case '3':
|
||||
print("Goodbye")
|
||||
exit()
|
||||
case _:
|
||||
print("Invalid Choice, please try again.")
|
||||
|
||||
def main():
|
||||
ptext = input("Kindly enter your desired plaintext: ")
|
||||
vk = input("Kindly enter the Vigenere Key: ")
|
||||
ak = int(input("Kindly enter the Autokey: "))
|
||||
|
||||
print("Welcome to the Autokey cipher system.")
|
||||
print("Enter your choice of algorithm")
|
||||
print("1. Vigenere Cipher")
|
||||
print("2. Autokey Cipher")
|
||||
print("3. Exit")
|
||||
|
||||
while True:
|
||||
op = input("Enter your choice of operation: ")
|
||||
operator(op, ptext, ak, vk)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
87
IS/Lab/Lab1/q3_playfair.py
Normal file
87
IS/Lab/Lab1/q3_playfair.py
Normal file
|
@ -0,0 +1,87 @@
|
|||
def generate_playfair_matrix(key):
|
||||
key = ''.join(sorted(set(key.upper()), key=key.upper().index))
|
||||
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
matrix = []
|
||||
|
||||
for char in key:
|
||||
if char not in matrix and char != 'J':
|
||||
matrix.append(char)
|
||||
|
||||
for char in alphabet:
|
||||
if char not in matrix and char != 'J':
|
||||
matrix.append(char)
|
||||
|
||||
playfair_matrix = [matrix[i:i+5] for i in range(0, len(matrix), 5)]
|
||||
return playfair_matrix
|
||||
|
||||
def find_pos(matrix, char):
|
||||
for row_idx, row in enumerate(matrix):
|
||||
if char in row:
|
||||
col_idx = row.index(char)
|
||||
return row_idx, col_idx
|
||||
return None
|
||||
|
||||
def prepare_text(text):
|
||||
text = ''.join(filter(str.isalpha, text.upper()))
|
||||
text = text.replace('J', 'I')
|
||||
pairs = []
|
||||
i = 0
|
||||
while i < len(text):
|
||||
if i + 1 < len(text) and text[i] != text[i+1]:
|
||||
pairs.append(text[i:i+2])
|
||||
i += 2
|
||||
else:
|
||||
pairs.append(text[i]+'X')
|
||||
i += 1
|
||||
return pairs
|
||||
|
||||
def playfair_en(ptext, pfk):
|
||||
matrix = generate_playfair_matrix(pfk)
|
||||
ptext_pairs = prepare_text(ptext)
|
||||
|
||||
ctext = []
|
||||
for pair in ptext_pairs:
|
||||
r1, c1 = find_pos(matrix, pair[0])
|
||||
r2, c2 = find_pos(matrix, pair[1])
|
||||
|
||||
if r1 == r2:
|
||||
ctext.append(matrix[r1][(c1 + 1) % 5] + matrix[r2][(c2 + 1) % 5])
|
||||
elif c1 == c2:
|
||||
ctext.append(matrix[(r1 + 1) % 5][c1] + matrix[(r2 + 1) % 5][c2])
|
||||
else:
|
||||
ctext.append(matrix[r1][c2] + matrix[r2][c1])
|
||||
|
||||
return ''.join(ctext)
|
||||
|
||||
def playfair_de(ctext, pfk):
|
||||
matrix = generate_playfair_matrix(pfk)
|
||||
ctext_pairs = prepare_text(ctext)
|
||||
|
||||
ptext = []
|
||||
for pair in ctext_pairs:
|
||||
r1, c1 = find_pos(matrix, pair[0])
|
||||
r2, c2 = find_pos(matrix, pair[1])
|
||||
|
||||
if r1 == r2:
|
||||
ptext.append(matrix[r1][(c1 - 1) % 5] + matrix[r2][(c2 - 1) % 5])
|
||||
elif c1 == c2:
|
||||
ptext.append(matrix[(r1 - 1) % 5][c1] + matrix[(r2 - 1) % 5][c2])
|
||||
else:
|
||||
ptext.append(matrix[r1][c2] + matrix[r2][c1])
|
||||
|
||||
return ''.join(ptext)
|
||||
|
||||
def main():
|
||||
ptext = input("Kindly enter your desired plaintext: ")
|
||||
pfk = input("Kindly enter the Playfair Key: ")
|
||||
|
||||
print("Welcome to the Playfair cipher system.")
|
||||
print("Plaintext: ", ptext)
|
||||
print("Playfair Key: ", pfk)
|
||||
ctext = playfair_en(ptext, pfk)
|
||||
print("Ciphertext: ", ctext)
|
||||
decrypted_text = playfair_de(ctext, pfk)
|
||||
print("Decrypted Text: ", decrypted_text)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
16
IS/Lab/Lab1/q4_hill.py
Normal file
16
IS/Lab/Lab1/q4_hill.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
def print_mat(hk):
|
||||
|
||||
def main():
|
||||
ptext = input("Kindly enter your desired plaintext: ")
|
||||
hk = input("Kindly enter the Hill Key: ")
|
||||
|
||||
print("Welcome to the Hill cipher.")
|
||||
print("Plaintext: ", ptext)
|
||||
print("Hill Key: ", print_mat(hk))
|
||||
ctext = hill_en(ptext, hk)
|
||||
print("Ciphertext: ", ctext)
|
||||
decrypted_text = hill_de(ctext, hk)
|
||||
print("Decrypted Text: ", decrypted_text)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
0
IS/Lab/Lab1/q5_john.py
Normal file
0
IS/Lab/Lab1/q5_john.py
Normal file
0
IS/Lab/Lab1/q6_affine_bf.py
Normal file
0
IS/Lab/Lab1/q6_affine_bf.py
Normal file
Loading…
Add table
Add a link
Reference in a new issue