-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek2.py
114 lines (84 loc) · 3.55 KB
/
week2.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import sys
from operator import methodcaller
import re
import numpy
from Crypto.Cipher import AES
from Crypto.Util import Counter
from Crypto import Random
def main():
blockSize = 16
q1 = cbcDecrypt("140b41b22a29beb4061bda66b6747e14", "4ca00ff4c898d61e1edbf1800618fb2828a226d160dad07883d04e008a7897ee2e4b7465d5290d0c0e6c6822236e1daafb94ffe0c5da05d9476be028ad7c1d81", blockSize)
q2 = cbcDecrypt("140b41b22a29beb4061bda66b6747e14", "5b68629feb8606f9a6667670b75b38a5b4832d0f26e1ab7da33249de7d4afc48e713ac646ace36e872ad5fb8a512428a6e21364b0c374df45503473c5242a253", blockSize)
q3 = ctrDecrypt("36f18357be4dbd77f050515c73fcf9f2", "69dda8455c7dd4254bf353b773304eec0ec7702330098ce7f7520d1cbbb20fc388d1b0adb5054dbd7370849dbf0b88d393f252e764f1f5f7ad97ef79d59ce29f5f51eeca32eabedd9afa9329", blockSize)
q4 = ctrDecrypt("36f18357be4dbd77f050515c73fcf9f2", "770b80259ec33beb2561358a9f2dc617e46218c0a53cbeca695ae45faa8952aa0e311bde9d4e01726d3184c34451", blockSize)
print "\n\nAnswers:"
print "Q1. ",q1
print "Q2. ",q2
print "Q3. ",q3
print "Q4. ",q4
def ctrDecrypt(key, cypherText, blockSize):
print "\nCTR decryption of key/cypher",key," / ",cypherText
res1 = ctrDecrypt1(key, cypherText, blockSize)
res2 = ctrDecrypt2(key, cypherText, blockSize)
print "1: ", res1
print "2: ", res2
return res1
def ctrDecrypt1(key, cypherText, blockSize):
k = key.decode('hex')
ct = cypherText.decode('hex')
iv = ct[:blockSize]
ct1 = ct[blockSize:]
ctr = Counter.new(blockSize*8,initial_value=long(iv.encode('hex'),16))
obj = AES.new(k,AES.MODE_CTR,counter=ctr)
paddedStr = obj.decrypt(ct1)
return paddedStr
def ctrDecrypt2(key, cypherText, blockSize):
cypherTextBlocks = [cypherText[i:i+(blockSize*2)] for i in range(0, len(cypherText), (blockSize*2))]
iv = long(cypherTextBlocks.pop(0), 16)
cypherTextBlocksDecoded = map(methodcaller("decode", "hex"), cypherTextBlocks)
k = key.decode('hex')
pt = ""
i = 0
for c in cypherTextBlocksDecoded:
ctr = hex(iv+i << 64)[2:(2*blockSize)+2]
encIV = AES.new(k, AES.MODE_ECB).encrypt(ctr)
plaintext = strxor(encIV, c)
i = i + 1
pt = plaintext + pt
return "?"#pt
def cbcDecrypt(key, cypherText, blockSize):
print "\nCBC decryption of key/cypher",key," / ",cypherText
res1 = cbcDecrypt1(key, cypherText, blockSize)
res2 = cbcDecrypt2(key, cypherText, blockSize)
print "1: ", res1
print "2: ", res2
return res2
def cbcDecrypt1(key, cypherText, blockSize):
k = key.decode('hex')
ct = cypherText.decode('hex')
iv = ct[:blockSize]
ct1 = ct[blockSize:]
obj = AES.new(k,AES.MODE_CBC,iv)
paddedStr = obj.decrypt(ct1)
paddingAmount = ord(paddedStr[len(paddedStr)-1:])
return paddedStr[:-paddingAmount]
def cbcDecrypt2(key, cypherText, blockSize):
cypherTextBlocks = [cypherText[i:i+(blockSize*2)] for i in range(0, len(cypherText), (blockSize*2))]
cypherTextBlocksDecoded = map(methodcaller("decode", "hex"), cypherTextBlocks)
k = key.decode('hex')
pt = ""
iter = len(cypherTextBlocksDecoded)
for c in reversed(cypherTextBlocksDecoded):
iter = iter - 1
if(iter > 0):
cipher = AES.new(k, AES.MODE_ECB).decrypt(c)
plaintext = strxor(cipher, cypherTextBlocksDecoded[iter - 1])
pt = plaintext + pt
paddingAmount = ord(pt[len(pt)-1:])
return pt[:-paddingAmount]
def strxor(a, b):
if len(a) > len(b):
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)])
else:
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])])
main()