-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryptMe.py
66 lines (47 loc) · 1.35 KB
/
encryptMe.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
from Crypto.Util import number
from Crypto.Cipher import AES
import random
def getPrimeP():
return number.getStrongPrime(512)
def getS():
return number.getRandomNBitInteger(500)
def bitLen(int_type):
length = 0
while (int_type):
int_type >>= 1
length += 1
return(length)
def testBit(int_type, offset):
mask = 1 << offset
return(int_type & mask)
def modExpo(b, exp, mod):
multiplier = b
total = 1
expBitLength = bitLen(exp)
bitCheck = 1
for i in range(0,expBitLength):
if exp & bitCheck:
total *= multiplier
total %= mod
bitCheck *= 2
multiplier *= multiplier
multiplier %= mod
return total
if __name__ == '__main__':
message = "The answer is noThe answer is noThe answer is no"
k = 'aoqirj.xmbnajsiq'
iv = '29581948572-1948'
print "\nECB Mode:"
obj = AES.new(k, AES.MODE_ECB)
ciphertext = obj.encrypt(message)
print ":".join("{00:x}".format(ord(c)) for c in ciphertext)
obj2 = AES.new(k,AES.MODE_ECB)
ans = obj2.decrypt(ciphertext)
print ans
print "\nCBC Mode:"
obj3 = AES.new(k, AES.MODE_CBC,iv)
ciphertext = obj3.encrypt(message)
print ":".join("{00:x}".format(ord(c)) for c in ciphertext)
obj4 = AES.new(k,AES.MODE_CBC,iv)
ans = obj4.decrypt(ciphertext)
print ans