-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCTR_mode.py
More file actions
64 lines (49 loc) · 1.64 KB
/
Copy pathCTR_mode.py
File metadata and controls
64 lines (49 loc) · 1.64 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
'''
Created on Mar 21, 2017
This is a module for the CTR mode of encryption where the Counter is used as an IV and is first fed into the encryption algorithm and the output is then XOR'd with the Plain-text to produce the
Cipher-text and the counter is incremented at every block of encryption round and the same process is followed.
@author: SaathvikPrasad
'''
import SdesEncrypt
Enc_Message_ctr = []
Dec_Message_ctr = []
'''aux function to encode the counter to binary 12 chars'''
def encode_counter(ctr):
ct = format(ctr, "012b")
C = list(ct)
return C
'''function for encryption using CTR mode of operation'''
def ctr_enc(message,para_key,ctr):
i = 0
CT = []
C = []
P = []
while(i<len(message)):
P = message[i:i+12]
C = encode_counter(ctr)
ek, ctrT = SdesEncrypt.desenc(para_key, C)
CpT = ''.join(ctrT)
CT = SdesEncrypt.exorbits(CpT, P)
Enc_Message_ctr.append(''.join(map(str,CT)))
i = i + 12
ctr = ctr + 1
emctr = ''.join(map(str,Enc_Message_ctr))
return ek, emctr
'''function for decryption using CTR mode of operation'''
def ctr_dec(message,pk,ctr):
C = []
P = []
CT = []
Msg = list(message)
i = 0
while(i<len(Msg)):
C = Msg[i:i+12]
P = encode_counter(ctr)
ek,ctrT = SdesEncrypt.desenc(pk,P)
Cpt = ''.join(ctrT)
CT = SdesEncrypt.exorbits(Cpt,C)
Dec_Message_ctr.append(''.join(map(str,CT)))
i = i+12
ctr = ctr+1
dmctr = ''.join(map(str,Dec_Message_ctr))
return dmctr