This repository has been archived by the owner on Jun 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cbc.py
82 lines (60 loc) · 1.69 KB
/
cbc.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
import gen_bin
import keystream
ambilKey = keystream.keystream()
def __plainHex(teks) :
temp = ""
for i in teks :
temp += gen_bin.hek_conv(ord(i))
return temp
def plainASCII(teks) :
i = 1
temp = ""
while i < len(teks) :
tmp = teks[i-1] + teks[i]
tmp = gen_bin.hek_conv(tmp)
temp += chr(tmp)
i += 2
return temp
def __wrap_left(karakter) :
tmp = gen_bin.bin_conv(gen_bin.hek_conv(karakter))
if len(tmp) < 4 :
tp = ""
for i in range(4-len(tmp)) :
tp += "0"
tmp = tp+tmp
tmp = tmp[1:]+tmp[0]
return gen_bin.hek_conv(gen_bin.bin_conv(tmp))
def __wrap_right(karakter) :
tmp = gen_bin.bin_conv(gen_bin.hek_conv(karakter))
if len(tmp) < 4 :
tp = ""
for i in range(4-len(tmp)) :
tp += "0"
tmp = tp+tmp
tmp = tmp[len(tmp)-1]+tmp[:len(tmp)-1]
return gen_bin.hek_conv(gen_bin.bin_conv(tmp))
def enkripsi(plain, K, IV) :
plain_hex = __plainHex(plain)
temp = ""
i = 0
for j in plain_hex :
tmp = gen_bin.hek_conv(gen_bin.hek_conv(j) ^ gen_bin.hek_conv(IV))
tmp = __wrap_left(gen_bin.hek_conv(gen_bin.hek_conv(tmp) ^ gen_bin.hek_conv(K[i])))
temp += tmp
IV = tmp
i += 1
if i == len(K) :
i %= len(K)
return temp
def dekripsi(chiper, K, IV) :
temp = ""
i = 0
for j in chiper :
tmp = gen_bin.hek_conv(gen_bin.hek_conv(__wrap_right(j)) ^ gen_bin.hek_conv(K[i]))
tmp = gen_bin.hek_conv(gen_bin.hek_conv(tmp) ^ gen_bin.hek_conv(IV))
temp += tmp
IV = j
i += 1
if i == len(K) :
i %= len(K)
return temp