-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDES_ECB.py
36 lines (26 loc) · 936 Bytes
/
DES_ECB.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
from Crypto.Cipher import DES
BS = DES.block_size
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[0:-ord(s[-1])]
class DESCipher:
def __init__(self, key):
self.key = key.decode("hex")
def encrypt(self, pt):
pt = pad(pt)
cipher = DES.new(self.key, DES.MODE_ECB)
return (cipher.encrypt(pt)).encode('hex')
def decrypt(self, ct):
ct = ct.decode("hex")
cipher = DES.new(self.key, DES.MODE_ECB)
return unpad(cipher.decrypt(ct))
if __name__== "__main__":
key = '012345678'
plaintext = 'abcdefghijkmnoasdsdap';
key = key.encode('hex')
# Truncate hex encoded key to block size, so 2x BS
key=key[:2*BS]
crypto = DESCipher(key)
ciphertext = crypto.encrypt(plaintext)
print "Cipher text - %s" % ciphertext
plaintext = crypto.decrypt(ciphertext)
print "Plain text - %s" % plaintext