-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHomework2.py
More file actions
83 lines (70 loc) · 2.88 KB
/
Homework2.py
File metadata and controls
83 lines (70 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!bin/python3
import numpy as np
UID = 119222119
Last_Name = 'Aaryash Raj'
First_Name = 'Sinha'
def vigenere_enc(plaintext, keyword):
# Converts the plaintext to uppercase and remove spaces
plaintext = plaintext.upper().replace(' ', '')
keyword = keyword.upper().replace(' ','')
# Repeating the keyword to match the length of the plaintext
keyword = (keyword * (len(plaintext) // len(keyword) + 1))[:len(plaintext)]
# Encrypts the plaintext using the Vigenere cipher
ciphertext = ''
for i in range(len(plaintext)):
pi = ord(plaintext[i])
ki = ord(keyword[i])
ci = ((pi + ki) % 26)
ciphertext += chr(ci+65)
return ciphertext
def vigenere_dec(ciphertext, keyword):
# Repeating the keyword to match the length of the ciphertext
keyword = (keyword * (len(ciphertext) // len(keyword) + 1))[:len(ciphertext)]
ciphertext = ciphertext.upper().replace(' ','')
keyword = keyword.upper().replace(' ', '')
# Decrypt the ciphertext using the Vigenere cipher
plaintext = ''
for i in range(len(ciphertext)):
ci = ord(ciphertext[i]) - 65
ki = ord(keyword[i]) - 65
pi = (ci - ki) % 26
plaintext += chr(pi + 65)
return plaintext
def hill_enc(M, plaintext):
# Convert the plaintext to uppercase and remove spaces
plaintext = plaintext.upper().replace(' ', '')
# Pad the plaintext with 'X' characters to make its length a multiple of 3
if len(plaintext) % 3 == 1:
plaintext += 'XX'
elif len(plaintext) % 3 == 2:
plaintext += 'X'
# Convert the plaintext to a matrix of numbers
P = []
for i in range(0, len(plaintext), 3):
P.append([ord(plaintext[i]) - 65, ord(plaintext[i+1]) - 65, ord(plaintext[i+2]) - 65])
# Encrypt the plaintext using the Hill cipher
C = []
for i in range(len(P)):
Pi = np.array(P[i]).T
Ci = np.matmul(Pi, M) % 26
C.append(Ci)
# Convert the ciphertext to a string
ciphertext = ''
for i in range(len(C)):
ciphertext += chr(C[i][0] + 65) + chr(C[i][1] + 65) + chr(C[i][2] + 65)
return ciphertext
if __name__ == "__main__":
#BANNER
print("\n\tAaryash Raj Sinha 119222119")
# Part 1: Vigenere Cipher
input_key = input("\n\n\tEnter a KEY Value: ")
input_str = input("\tEnter a String to Encrypt: ")
encstr = vigenere_enc(input_str, input_key)
print(f"\n\tVignere Cipher generated Ciphertext:\t{encstr}")
decstr = vigenere_dec(encstr, input_key)
print(f"\tVignere Cipher generated Decryptedtext:\t{decstr}")
# Part 2: Hill Cipher
input_matrix = np.array([[17, 17, 5], [21, 18, 21], [2, 2, 19]])
input_plaintext = 'Test String'
ciphertext = hill_enc(input_matrix, input_plaintext)
print(f"\n\tHill Cipher generated Ciphertext:\t{ciphertext}\n\n")