-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.py
executable file
·133 lines (106 loc) · 3.72 KB
/
encrypt.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
"""Pseudo-secure Monoalphabetic Cipher Encryption
Usage:
encrypt.py <plaintext> <ciphertext> [--random | --caesar=<shift>]
encrypt.py (-h | --help)
encrypt.py --version
Options:
-h --help Show this screen.
--version Show version.
--random Randomly generate the substitution letters [default: true]
--caesar=<shift> Use simple Caesar Cipher with given shift
"""
from docopt import docopt
from collections import deque
from colorama import Fore, Back, Style
import random
class Cipher:
"""Cipher Base Class"""
def __init__(self, arguments):
"""Constructor"""
self.arguments = arguments
self.plaintext_file = self.arguments['<plaintext>']
self.ciphertext_file = self.arguments['<ciphertext>']
self.alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
def GenerateKey(self):
"""Generate Key"""
self.key = list(deque(self.alphabet).rotate(-3))
def GenerateKeymap(self):
"""Generate Keymap"""
self.keymap = dict(zip(self.alphabet, self.key))
def Encrypt(self):
"""Encrypt Plaintext"""
plaintext = list(self.plaintext)
ciphertext = plaintext
# Perform replacement
index = 0
for letter in plaintext:
if letter.lower() in self.keymap:
ciphertext[index] = self.keymap[letter.lower()]
index += 1
self.ciphertext = "".join(ciphertext)
def ReadPlaintext(self):
"""Read plaintext file into string"""
with open(self.plaintext_file, 'r') as ptf:
self.plaintext = ptf.read()
def WriteCipherText(self):
"""Write ciphertext to the file"""
with open(self.ciphertext_file, 'w') as ctf:
ctf.write(self.ciphertext)
def Run(self):
"""Run"""
self.ReadPlaintext()
self.Encrypt()
self.WriteCipherText()
self.Print()
def PrintPlaintext(self):
"""Print Plaintext"""
print("PLAINTEXT\n")
print(Fore.GREEN + self.plaintext + Fore.RESET)
def PrintCiphertext(self):
"""Print Ciphertext"""
print("CIPHERTEXT\n")
print(Fore.RED + self.ciphertext + Fore.RESET)
def Print(self):
"""Print Plaintext and Ciphertext"""
self.PrintPlaintext()
self.PrintCiphertext()
class MonoalphabeticCipher(Cipher):
"""Implements a monoalphabetic cipher"""
def __init__(self, arguments):
"""Constructor"""
super().__init__(arguments)
self.random = arguments['--random']
self.shift = arguments['--caesar']
if self.shift:
self.shift = int(self.shift)
else:
self.shift = 0
# Generate a key and keymap
self.GenerateKey()
self.GenerateKeymap()
def GenerateKey(self):
"""Generate a random/shifted key"""
# Generate key (substitution letters)
key = list(self.alphabet)
if self.random:
# Generate random key
random.shuffle(key)
elif self.shift:
d = deque(key)
d.rotate(-1 * self.shift)
key = list(d)
else:
# Default to shift of 3 (i.e., Caesar Cipher)
self.shift = 3
d = deque(key)
d.rotate(-1 * self.shift)
key = list(d)
self.key = list(key)
if __name__ == '__main__':
arguments = docopt(__doc__, version='0.1.0')
# print(arguments)
# Run the Monoalphabetic Cipher
cipher = MonoalphabeticCipher(arguments)
cipher.Run()