-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHillCipher.py
More file actions
87 lines (67 loc) · 2.43 KB
/
HillCipher.py
File metadata and controls
87 lines (67 loc) · 2.43 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
84
85
86
87
import sys
import numpy as np
# Function to read only the lowercase alphabetic letters from the input file
def read_plaintext(input_file):
# Opens the plaintext file
with open(input_file, 'r') as plaintxt:
# replace new line w space, removes non-alphabet characters, coverts to lowercase
data = plaintxt.read().replace('\n', '')
data = ''.join(e for e in data if e.isalpha())
data = data.lower()
return data
# Pads plaintext with 'x'
def padPlaintext(plaintext, n):
pad_len = (n - len(plaintext) % n) % n
return plaintext + 'x' * pad_len
# Hill encryption algorithm
def hill_cipher_encrypt(plaintext, key_matrix):
# Get size of key
n = key_matrix.shape[0]
# Create ints to represent plaintext & append values to list
plaintext_ints = []
for char in plaintext:
if char.isalpha():
plaintext_ints.append(ord(char) - ord('a'))
# Convert the list of integers to a matrix
plaintext_matrix = np.array(plaintext_ints).reshape(-1, n).T
# Multiply plaintext matrix by the key matrix
ciphertext_matrix = np.dot(key_matrix, plaintext_matrix) % 26
# Convert ciphertext matrix to a string
ciphertext = ''
for row in ciphertext_matrix.T:
for char_int in row:
ciphertext += chr(char_int + ord('a'))
return ciphertext
# Function to output ciphertext
def outputCiphertext(ciphertext):
print("\nCiphertext:")
# print 80 char per line
for i in range(0, len(ciphertext), 80):
print(ciphertext[i:i+80])
# Main- tells user how to exec
if len(sys.argv) != 3:
print("\nTo execute, input your key file and plaintext file.\n")
print("like this -> Python pa01.py [keyfile.txt] [plaintext.txt]\n")
exit(1)
# Reads key file
with open(sys.argv[1], 'r') as file: #takes first arg as key
n = int(file.readline())
key_matrix = []
for line in file:
row = line.split()
row_int = [int(i) % 26 for i in row]
key_matrix.append(row_int)
key_matrix = np.array(key_matrix)
# Reads input file
plaintext = read_plaintext(sys.argv[2])
# Encrypts plaintext using Hill cipher and key matrix
ciphertext = hill_cipher_encrypt(plaintext, key_matrix)
# Output key matrix
print("\nKey matrix:")
for row in key_matrix:
row_str = " ".join(str(x) for x in row)
print(row_str)
# Outputs plaintext and ciphertext
print("\nPlaintext:")
print(plaintext)
outputCiphertext(ciphertext)