-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
205 lines (176 loc) · 9.69 KB
/
main.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import sys
import argparse
# Cipher modules
import caesar_cipher
import rsa
import simple_substitution
import feistel_cipher
import vigenere_cipher
import elgamal
import massey_omura
### --- Cipher calls
def call_caesar(args):
cipher = caesar_cipher
match args.action.lower():
case "crack":
print(cipher.crack(str(args.text), bool(args.digits)))
case "decrypt":
print(cipher.decrypt(str(args.text), int(args.shift), bool(args.digits)))
case "encrypt":
print(cipher.encrypt(str(args.text), int(args.shift), bool(args.digits)))
case _:
print("Unsupported operation:", args.action)
def call_rsa(args):
cipher = rsa
match args.action.lower():
case "crack":
print(cipher.crack(int(args.text), int(args.n), int(args.e), bool(args.factor_db)))
case "decrypt":
print(cipher.decrypt(int(args.text), int(args.n), int(args.d)))
case "encrypt":
print(cipher.encrypt(int(args.text), int(args.n), int(args.e))) #p and q are optional
case "generate":
if args.weak:
print("Generating key where p and q are close together...")
print(cipher.generate_weak_key(int(args.min), int(args.max)))
else:
print("Generating random p and q...")
print(cipher.generate_strong_key(int(args.min), int(args.max)))
case _:
print("Unsupported operation:", args.action)
def call_massey_omura(args):
cipher = massey_omura
match args.action.lower():
case "crack":
print(cipher.crack(int(args.step1), int(args.step2), int(args.step3), int(args.prime)))
case "decrypt":
print(cipher.decrypt(int(args.text), int(args.receiver), int(args.prime)))
case "encrypt":
print(cipher.encrypt(int(args.text), int(args.sender), int(args.receiver), int(args.prime)))
case _:
print("Unsupported operation:", args.action)
def call_simple_substitution(args):
cipher = simple_substitution
match args.action.lower():
case "crack":
print(cipher.crack(str(args.text)))
case "decrypt":
print(cipher.decrypt(str(args.text), str(args.key)))
case "encrypt":
print(cipher.encrypt(str(args.text), str(args.key)))
case _:
print("Unsupported operation:", args.action)
def call_feistel(args):
cipher = feistel_cipher
match args.action.lower():
case "crack":
print(cipher.crack(str(args.text)))
case "decrypt":
print(cipher.decrypt(str(args.text), str(args.key), int(args.rounds)))
case "encrypt":
print(cipher.encrypt(str(args.text), str(args.key), int(args.rounds)))
case _:
print("Unsupported operation:", args.action)
def call_elgamal(args):
cipher = elgamal
match args.action.lower():
case "crack":
print(cipher.crack(int(args.text), int(args.text2), int(args.receiver), int(args.root), int(args.modulus), bool(args.crack_b) or False))
case "decrypt":
print(cipher.decrypt(int(args.text), int(args.text2), int(args.private), int(args.root), int(args.modulus)))
case "encrypt":
print(cipher.encrypt(int(args.text), int(args.receiver), int(args.root), int(args.modulus), int(args.little_k if args.little_k else -1) ))
case _:
print("Unsupported operation:", args.action)
def call_vigenere(args):
cipher = vigenere_cipher
match args.action.lower():
case "crack":
print(cipher.crack(args.text))
case "decrypt":
print(cipher.decrypt(args.text, args.key))
case "encrypt":
print(cipher.encrypt(args.text, args.key))
case _:
print("Unsupported operation:", args.action)
### ----- Arguments handling functions -----
# define command line arguments and subparsers
parser = argparse.ArgumentParser(prog="main.py", description="Encrypt, decrypt or crack a cipher text.")
# required cipher type first
individual_cipher_arg_parsers = parser.add_subparsers(title="cipher", dest="cipher", required=True, help="Cipher types - rsa, caesar, simple, feistel")
# give selection of actions - encrypt, decrypt, crack
action_group = parser.add_argument_group("action", argument_default="encrypt")
action_group.add_argument("--encrypt", "-e", action="store_const", const="encrypt", dest="action", default="encrypt", help="Encrypt the plain text")
action_group.add_argument("--decrypt", "-d", action="store_const", const="decrypt", dest="action", help="Decrypt the cipher text")
action_group.add_argument("--crack", "-c", action="store_const", const="crack", dest="action", help="Crack the cipher text")
action_group.add_argument("--generate", "-g", action="store_const", const="generate", dest="action", help="Generate a key")
# caesar - shift based key 0-26
caesar_parser = individual_cipher_arg_parsers.add_parser("caesar", help="Caesar Cipher")
caesar_parser.add_argument("-t","-m", "--text", required=True, help="*Text to encrypt/decrypt/crack")
caesar_parser.add_argument("-s", "--shift", default=0, type=int, choices=range(-26, 27), help="Caesar shift ±(0-26)")
caesar_parser.add_argument("-d", "--digits", action="store_true", help="Shift the digits in addition to characters")
# rsa
rsa_parser = individual_cipher_arg_parsers.add_parser("rsa", help="RSA Cipher")
rsa_parser.add_argument("-t", "-m", "--text", help="Text to encrypt/decrypt", type=int)
rsa_parser.add_argument("-n", "--n", help="N value from p*q (Encrypt/Decrypt/Crack)", type=int)
rsa_parser.add_argument("-e", "--e", help="E value (inverse of d) (Encrypt/Crack)", type=int)
rsa_parser.add_argument("-d", "--d", help="D value (Private) (Decrypt)", type=int)
rsa_parser.add_argument("--min", help="Minimum prime value for key generation", type=int)
rsa_parser.add_argument("--max", help="Maximum prime value for key generation", type=int)
rsa_parser.add_argument("--factor-db", help="Use the factor db to crack the key (Crack)", action="store_true", dest="factor_db")
rsa_parser.add_argument("--weak", help="Generate a weak key (Crack)", action="store_true", dest="weak")
# elgamal
elgamal_parser = individual_cipher_arg_parsers.add_parser("elgamal", help="RSA Cipher")
elgamal_parser.add_argument("-t", "-m", "-t1", "-m1", "--text", "--text1", required=True, help="Text to encrypt", type=int)
elgamal_parser.add_argument("-t2", "-m2", "--text2", help="Text Km to decrypt (Decrypt/Crack)", type=int)
elgamal_parser.add_argument("-k", "--little-k", help="Little k/random number (Encrypt optional)", type=int)
elgamal_parser.add_argument("-y", "--receiver", help="Receiver's y value (Encrypt/Crack)", type=int)
elgamal_parser.add_argument("-b", "-x", "--private", help="Receiver's private key (Decrypt)", type=int)
elgamal_parser.add_argument("-a", "-r", "--root", required=True, help="Root value", type=int)
elgamal_parser.add_argument("--crack-b", "--crack-x", help="Only crack via receiver's key (Crack)", action="store_true", dest="crack_b")
elgamal_parser.add_argument("-p", "--modulus", required=True, help="Modulus value", type=int)
# maassey-omura
massey_omura_parser = individual_cipher_arg_parsers.add_parser("massey", help="Massey-Omura cryptosystem")
massey_omura_parser.add_argument("-t","-m", "--text", help="*Number to encrypt/decrypt")
massey_omura_parser.add_argument("-p", "--prime", required=True, help="Prime number")
massey_omura_parser.add_argument("-a", "-s", "--sender", help="Sender/Alice's key")
massey_omura_parser.add_argument("-b", "-r","--receiver", help="Receiver/Bob's key")
massey_omura_parser.add_argument("-1", "--step1", help="m^sender - used by crack")
massey_omura_parser.add_argument("-2", "--step2", help="m^(sender*receiver) - used by crack")
massey_omura_parser.add_argument("-3", "--step3", help="m^receiver - used by crack")
# simple sub - text based key
simple_parser = individual_cipher_arg_parsers.add_parser("simple", help="Simple Substitution Cipher")
simple_parser.add_argument("-t","--text", required=True, help="*Text to encrypt/decrypt")
simple_parser.add_argument("-k", "--key", default="abcdefghijklmnopqrstuvwxyz", help="Substitution key - default: 'abcdefghijklmnopqrstuvwxyz'")
# feistel - text based key, rounds integer, and keylength integer
feistel_parser = individual_cipher_arg_parsers.add_parser("feistel", help="Feistel Cipher")
feistel_parser.add_argument("-t", "-m", "--text", "--hex", required=True, dest="text", help="Plaintext to encrypt/Hex to decrypt")
feistel_parser.add_argument("-k", "--key", default="abc123", help="Key - default: 'abc123' ")
feistel_parser.add_argument("-r", "--rounds", default=16, help="# of rounds to run the feistel cipher for - default: 16", type=int)
# vigenere - text based key
vigenere_parser = individual_cipher_arg_parsers.add_parser("vigenere", help="Vigenere Cipher")
vigenere_parser.add_argument("-t","--text", required=True, help="*Text to encrypt/decrypt")
vigenere_parser.add_argument("-k", "--key", default="cars", help="Key - default: 'cars'")
def main():
prog_args = parser.parse_args()
cipher_type = None
match prog_args.cipher.lower():
case "caesar":
call_caesar(prog_args)
case "simple":
call_simple_substitution(prog_args)
case "feistel":
call_feistel(prog_args)
case "vigenere":
call_vigenere(prog_args)
case "elgamal":
call_elgamal(prog_args)
case "massey":
call_massey_omura(prog_args)
case "rsa":
call_rsa(prog_args)
case _:
print("Unsupported cipher type:", prog_args.cipher)
sys.exit(1)
if __name__ == "__main__":
main()