-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathECB.py
66 lines (52 loc) · 1.59 KB
/
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
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
import random
import string
from serpent import convertToBitstring, stringtohex, makeLongKey, \
keyLengthInBitsOf, encrypt,decrypt
def makptright(plainText):
result = plainText
if len(result) % 16 != 0:
result += "1"
while len(result) % 16 != 0:
result += "0"
return result
def convert(s):
new = ""
for x in s:
new += str(x)
return new
def ECBEnc(plainText, key):
pos = 0
cipherTextChunks = []
strigkey = str(key)
strigkey = strigkey[2:]
strl = strigkey
strl = strl.lower()
bitsInKey = keyLengthInBitsOf(strl)
rawKey = convertToBitstring(strl, bitsInKey)
userKey = makeLongKey(rawKey) # for the increption 256
plainText = str(plainText)
while pos + 16 <= len(plainText):
nextPos = pos + 16
textt = plainText[pos:nextPos]
toEnc = stringtohex(textt)
toEnc = convertToBitstring(toEnc, len(toEnc) * 4)
enc = encrypt(toEnc, userKey)
cipherTextChunks.append(enc)
pos += 16
return cipherTextChunks
def ECBDec(cipherTextChunks, key):
plainText = []
strigkey = str(key)
strigkey = strigkey[2:]
strl = strigkey
strl = strl.lower()
bitsInKey = keyLengthInBitsOf(strl)
rawKey = convertToBitstring(strl, bitsInKey)
userKey = makeLongKey(rawKey) # for the increption
temp = []
for chunk in cipherTextChunks:
dec = decrypt(chunk, userKey)
temp.append(dec)
for l in reversed(temp):
plainText += l
return plainText