-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.py
More file actions
66 lines (55 loc) · 1.93 KB
/
Copy pathcipher.py
File metadata and controls
66 lines (55 loc) · 1.93 KB
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
from Crypto.Cipher import DES, AES
import random
class Cipher:
def __init__(self, cipher="DES",plaintext=1, keylen=56, msglen=64, IV='00000000'):
self.cipher = cipher
self.plaintext = plaintext
self.keylen = keylen
self.msglen = msglen
self.permute = random.sample(range(0, msglen), keylen)
# print self.permute
# self.permute = [0,1,2,3,4,5,6,7]
self.iv = IV
# print "[*] Params passed to Cipher class:"
# print "[*] plaintext: ", plaintext
# print "[*] keylen: ", keylen
# print "[*] msglen: ", msglen
def f(self, key, display=False):
# Convert key and plaintext to string
key = hex(key).replace("0x", "").replace("L", "")
key = '0'*((2*(self.msglen/8)-len(key))) + key
key = key.decode("hex")
plaintext = hex(self.plaintext).replace("0x", "").replace("L", "")
plaintext = '0'*(2*(self.msglen/8)-len(plaintext)) + plaintext
plaintext = plaintext.decode("hex")
# if display:
# print "[*] key: ", key.encode("hex")
# print "[*] plaintext:" , plaintext.encode("hex")
# Encryption code
if self.cipher=="DES":
cphr = self.des_encrypt(key, plaintext)
else:
cphr = self.aes_encrypt(key, plaintext)
cphr = self.R(int(cphr.encode("hex"), 16))
return cphr
def R(self, cipher):
cipher = bin(cipher).replace("0b", "")
cipher = '0'*(self.msglen-len(cipher)) + cipher
cipher = [cipher[i] for i in self.permute]
cipher = ''.join(cipher)
# print "[*] cipher:",cipher
return int(cipher, 2)
def des_encrypt(self, key, plaintext, display=False):
# if display:
# print "[*] plaintext: ", plaintext.encode("hex")
# print "[*] key: ", key.encode("hex")
des = DES.new(key, DES.MODE_CBC, self.iv)
cphr = des.encrypt(plaintext)
return cphr
def aes_encrypt(self, key, plaintext, display=False):
# if display:
# print "[*] plaintext: ", plaintext.encode("hex")
# print "[*] key: ", key.encode("hex")
aes = AES.new(key, AES.MODE_CBC, '0'*16)
cphr = aes.encrypt(plaintext)
return cphr