IS Lab 1 - Progress So Far
This commit is contained in:
parent
e14d40f9b2
commit
518399472f
7 changed files with 348 additions and 1 deletions
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()
|
Loading…
Add table
Add a link
Reference in a new issue