Skip to content

Commit 00af2de

Browse files
committed
Week 2 - Programming Assignment
1 parent 9868466 commit 00af2de

File tree

4 files changed

+184
-1
lines changed

4 files changed

+184
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
Course: Cryptography - www.coursera.org
22
Professor: Dan Boneh
33

4-
Week 1: Many Time Pad
4+
Programmin Assignments exercises implemented on the course.
5+
6+
- Week 1: Many Time Pad
7+
- Week 2: CBC mode and CTR mode
154 KB
Binary file not shown.

Week_02/cbc.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import sys
2+
from Crypto.Cipher import AES
3+
import os
4+
import ctypes
5+
6+
BLOCK_SIZE = 32
7+
8+
# Xor two strings of different lengths
9+
10+
def strxor(a, b):
11+
if len(a) > len(b):
12+
xor_string = "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)])
13+
else:
14+
xor_string = "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])])
15+
16+
return xor_string
17+
18+
# Decrypts a cipher text
19+
20+
def decrypt(key, cipher_text):
21+
22+
num_blocks = len(cipher_text) / len(key)
23+
24+
iv = cipher_text[0:BLOCK_SIZE].decode('hex')
25+
26+
aes = AES.new(key.decode('hex'))
27+
28+
index_min = BLOCK_SIZE
29+
index_max = index_min + BLOCK_SIZE
30+
31+
current_xor = iv
32+
33+
pt = ''
34+
35+
for i in range(1, num_blocks):
36+
37+
block_ciphered = cipher_text[index_min:index_max].decode('hex')
38+
39+
block_decrypted = aes.decrypt(block_ciphered)
40+
41+
message_block = strxor(block_decrypted, current_xor)
42+
43+
pt = pt + message_block
44+
45+
current_xor = block_ciphered
46+
47+
index_min = index_max
48+
index_max = index_max + BLOCK_SIZE
49+
50+
return pt
51+
52+
# Main function
53+
54+
def main(argv):
55+
56+
key = argv[1]
57+
cipher_text = argv[2]
58+
59+
pt = decrypt(key, cipher_text)
60+
61+
print '\nplain text'
62+
print pt
63+
print '\n'
64+
65+
return 0
66+
67+
if __name__ == '__main__':
68+
sys.exit(main(sys.argv))

Week_02/ctr.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import sys
2+
from Crypto.Cipher import AES
3+
import os
4+
import ctypes
5+
6+
BLOCK_SIZE = 32
7+
IV_LENGTH = 32
8+
9+
# Convert String to List
10+
11+
def convertStringToList(string):
12+
list = []
13+
14+
for i in range(0, len(string)):
15+
list.append(string[i])
16+
17+
return list
18+
19+
# Convert List to String
20+
21+
def convertListToString(list):
22+
string = ''
23+
24+
for i in range(0, len(list)):
25+
string += list[i]
26+
27+
return string
28+
29+
# Increment IV List by 1
30+
31+
def incrIV(s, i):
32+
if i < 0:
33+
return ""
34+
35+
if s[i] == 'f':
36+
s[i] = '0'
37+
s = incrIV(s, i-1)
38+
else:
39+
hex_value = int(s[i], 16)
40+
hex_value += 1
41+
s[i] = hex(hex_value)[2:];
42+
43+
return s;
44+
45+
def strxor(a, b):
46+
if len(a) > len(b):
47+
xor_string = "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)])
48+
else:
49+
xor_string = "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])])
50+
51+
return xor_string
52+
53+
# Decrypt a cipher text
54+
55+
def decrypt(key, cipher_text):
56+
57+
num_blocks = len(cipher_text) / len(key)
58+
59+
iv = cipher_text[0:IV_LENGTH].decode('hex')
60+
61+
aes = AES.new(key.decode('hex'))
62+
63+
index_min = IV_LENGTH
64+
index_max = index_min + BLOCK_SIZE
65+
66+
current_xor = iv
67+
68+
pt = ''
69+
70+
for i in range(1, num_blocks+1):
71+
72+
block_ciphered = cipher_text[index_min:index_max].decode('hex')
73+
74+
block_decrypted = aes.encrypt(current_xor)
75+
76+
message_block = strxor(block_decrypted, block_ciphered)
77+
78+
print 'message_block'
79+
print message_block
80+
81+
pt = pt + message_block
82+
83+
list = incrIV(convertStringToList(current_xor.encode('hex')), IV_LENGTH-1)
84+
85+
current_xor = convertListToString(list).decode('hex')
86+
87+
print 'current_xor'
88+
print current_xor.encode('hex')
89+
print ''
90+
91+
index_min = index_max
92+
index_max = index_max + BLOCK_SIZE
93+
94+
return pt
95+
96+
# Main function
97+
98+
def main(argv):
99+
100+
key = argv[1]
101+
cipher_text = argv[2]
102+
103+
pt = decrypt(key, cipher_text)
104+
105+
print '\nplain text'
106+
print pt
107+
print '\n'
108+
109+
return 0
110+
111+
if __name__ == '__main__':
112+
sys.exit(main(sys.argv))

0 commit comments

Comments
 (0)