-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCipherDriver.py
161 lines (140 loc) · 6.96 KB
/
CipherDriver.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
"""
CPSC 452
Assignment 2
Cipher Driver
Python 2.7
CFB is slow for large files
run long file:
python CipherDriver.py DES 1234567890abcdef ENC long.txt des.enc;
python CipherDriver.py DES 1234567890abcdef DEC des.enc des.dec;
python CipherDriver.py RSA pubkey.pem ENC long.txt rsa.enc;
python CipherDriver.py RSA privkey.pem DEC rsa.enc rsa.dec;
python CipherDriver.py DESCBC 1234567890abcdef ENC long.txt descbc.enc 01234567;
python CipherDriver.py DESCBC 1234567890abcdef DEC descbc.enc descbc.dec 01234567;
python CipherDriver.py RSACBC pubkey.pem ENC long.txt rsacbc.enc 01234567;
python CipherDriver.py RSACBC privkey.pem DEC rsacbc.enc rsacbc.dec 01234567;
python CipherDriver.py DESCFB 1234567890abcdef ENC long.txt descfb.enc 01234567 57;
python CipherDriver.py DESCFB 1234567890abcdef DEC descfb.enc descfb.dec 01234567 57;
python CipherDriver.py RSACFB pubkey.pem ENC long.txt rsacfb.enc iviviviv 33;
python CipherDriver.py RSACFB privkey.pem DEC rsacfb.enc rsacfb.dec iviviviv 33;
run short file:
python CipherDriver.py DES 1234567890abcdef ENC in.txt des.enc;
python CipherDriver.py DES 1234567890abcdef DEC des.enc des.dec;
python CipherDriver.py RSA pubkey.pem ENC in.txt rsa.enc;
python CipherDriver.py RSA privkey.pem DEC rsa.enc rsa.dec;
python CipherDriver.py DESCBC 1234567890abcdef ENC in.txt descbc.enc 01234567;
python CipherDriver.py DESCBC 1234567890abcdef DEC descbc.enc descbc.dec 01234567;
python CipherDriver.py RSACBC pubkey.pem ENC in.txt rsacbc.enc 01234567;
python CipherDriver.py RSACBC privkey.pem DEC rsacbc.enc rsacbc.dec 01234567;
python CipherDriver.py DESCFB 1234567890abcdef ENC in.txt descfb.enc 01234567 57;
python CipherDriver.py DESCFB 1234567890abcdef DEC descfb.enc descfb.dec 01234567 57;
python CipherDriver.py RSACFB pubkey.pem ENC in.txt rsacfb.enc iviviviv 45;
python CipherDriver.py RSACFB privkey.pem DEC rsacfb.enc rsacfb.dec iviviviv 45;
broken: DESCBC RSACBC (rsa decrypt line 53 int argument results in ''
"""
import sys
from CipherInterface import CipherInterface
from DESCipher import DESCipher
from RSACipher import RSACipher
from CFBMode import *
from CBCMode import *
from binaryStrings import *
# Attempt to read the content of the specified filename
def readFile(fileName):
try:
with open(fileName, 'r') as ifh: # Open the file with read permissions
text = ifh.read()
return text
except IOError:
badUser("ERROR: could not read to " + fileName)
# Attempt to write the content to the specified filename
def writeFile(fileName, content):
try:
with open(fileName, 'w') as ofh: # Open the file with write permissions
ofh.write(content) # Write the contents to the file
except IOError:
badUser("ERROR: could not write to " + fileName)
# program used incorrectly by user
def badUser(msg):
print(msg)
sys.exit(-1)
def main ( ):
# check number of parameters
if len(sys.argv) != 6 and len(sys.argv) != 7 and len(sys.argv) != 8:
badUser("ERROR: incorrect number of parameters provided\ncheck README")
cipherName = sys.argv[1] # <CIPHER NAME>
key = sys.argv[2] # <KEY>
mode = sys.argv[3] # <ENC/DEC>
inputFile = sys.argv[4] # <INPUTFILE>
outputFile = sys.argv[5] # <OUTPUTFILE>
# deeper check of number of parameters
if cipherName in {"DESCBC", "RSACBC"}: # set of CBC mode ciphers
if len(sys.argv) != 7:
badUser("ERROR: incorrect number of parameters for CBC\ncheck README")
elif len(sys.argv[6]) != 8:
badUser("ERROR: IV isn't 8 characters\ncheck README")
else:
IV = sys.argv[6] # TODO: 8 character string
elif cipherName in {"DESCFB", "RSACFB"}: # set of CFB mode ciphers
if len(sys.argv) != 8:
badUser("ERROR: incorrect number of parameters for CFB\ncheck README")
elif len(sys.argv[6]) != 8:
badUser("ERROR: IV isn't 8 characters\ncheck README")
else:
IV = sys.argv[6]
try: # ensure s is an int
s = int(sys.argv[7]) #TODO check that it's between 1 and 64
except ValueError:
badUser("ERROR: s argument provided couldn't translate to a 10-base int\ncheck README")
elif len(sys.argv) != 6:
badUser("ERROR: incorrect number of parameters for ECB\ncheck README")
toConvert = readFile(inputFile) # attempt to read file
if cipherName in {"DES", "DESCBC", "DESCFB"}:
cipher = DESCipher() # Create an instance of the class
if cipher.setKey(key) == False: # Attempt to set the key
badUser("Invalid key for DES Cipher. Key must be 16 hexidecimal characters (0-9, a-f)!")
elif cipherName in {"RSA", "RSACBC", "RSACFB"}:
cipher = RSACipher()
if cipher.setKey(key) == False: # Attempt to set the key
badUser("Setting RSA key failed\ncheck README")
else: # Invalid Cipher
badUser("Invalid <CIPHER NAME>\ncheck README")
if mode == "ENC":
print("ENC: Encryption mode selected. Encrypting...")
if cipherName in {"DESCBC", "RSACBC"}:
if cipherName == "DESCBC":
n = 64
else:
n = cipher._key.size() # maximum size of text that the key can handle in bits
IV = strToBinStr(IV)
IV += IV * (n - len(IV)) # duplicate IV to fit n
IV = IV[:n] # cut off the extra characters
IV = strToBinStr(IV)
converted = encryptCBC(cipher, toConvert, IV, n)
elif cipherName in {"DESCFB", "RSACFB"}:
converted = encryptCFB(cipher, toConvert, IV, s)
else:
converted = cipher.encrypt(toConvert) # Encrypt and receive the ciphered text
# print("The ciphered text is: {}".format(converted))
elif mode == "DEC":
print("DEC: Decryption mode selected. Decrypting...")
if cipherName in {"DESCBC", "RSACBC"}:
if cipherName == "DESCBC":
n = 64
else:
n = cipher._key.size() # maximum size of text that the key can handle in bits
IV = strToBinStr(IV)
IV += IV * (n - len(IV)) # duplicate IV to fit n
IV = IV[:n] # cut off the extra characters
IV = strToBinStr(IV)
converted = decryptCBC(cipher, toConvert, IV, n)
elif cipherName in {"DESCFB", "RSACFB"}:
converted = decryptCFB(cipher, toConvert, IV, s)
else:
converted = cipher.decrypt(toConvert) # Decrypt and receive the deciphered text
# print("The deciphered text is: {}".format(converted))
else:
badUser("ERROR: please specify <ENC/DEC>")
writeFile(outputFile, converted) # print converted text
if __name__ == '__main__':
main( )