-
Notifications
You must be signed in to change notification settings - Fork 2
/
3des.py
48 lines (37 loc) · 1.35 KB
/
3des.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
from Crypto.Cipher import DES3
import base64
import sys
import argparse
import optparse
key = 'abcdefghijklmnop'
IV = 'ABCDEFGH'
def unpad(string):
pad_size = ord(string[-1])
print "padding size : " +str(pad_size)
print "decrypted string : " +string[:-pad_size]
def encrypt(string):
pad_len = 8 -len(string) %8
pad = string+chr(pad_len) * pad_len
cipher = DES3.new(key, DES3.MODE_CBC, IV)
ctext = cipher.encrypt(pad)
return base64.b64encode(ctext)
def decrypt(string):
encrypt = DES3.new(key, DES3.MODE_CBC, IV)
decode_string = base64.b64decode(string)
plaintext = encrypt.decrypt(decode_string)
return unpad(plaintext)
if len(sys.argv) == 1:
print "Usage : python 3des.py -d/e -c cipher -t text"
sys.exit()
parser = optparse.OptionParser(description= "Triple DES Encrypt Decrypt Script ", prog="3des.py")
parser.add_option('-d','--decrypt',dest='decrypt',action="store_true",help='Triple DES Decrypt Method')
parser.add_option('-e','--encrypt',dest='encrypt',action="store_true",help='Triple DES Encrypt Method')
parser.add_option('-c','--cipher',dest='cipher',help='Cipher Text')
parser.add_option('-t','--text',dest='text', type='string',help='String to encrpyt')
options, args = parser.parse_args()
if (options.encrypt):
ctext = options.text
print encrypt(ctext)
if (options.decrypt):
ctext = options.cipher
decrypt(ctext)