forked from tml/Matasano-Crypto-Challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.py
80 lines (67 loc) · 1.97 KB
/
profile.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
# Class for the cryptopals challenge 13 - ECB cut and paste
import crypto
import string
class Profile:
def encode(self):
'''
Return the k=v encoding of the profile object.
Deliminator is the & character
'''
return 'email=' + self.email + '&uid=' + str(self.uid) + '&role=' + self.role
def __repr__(self):
'''
Print a dictionary of the attributes
'''
return str(vars(self))
def parse(string):
'''
Parse a string of k=v pairs (e.g. foo=bar&baz=qux&zap=zazzle)
and produce the profile object
{
foo: 'bar',
baz: 'qux',
zap: 'zazzle'
}
'''
items = string.split('&')
p = Profile()
for pair in items:
if '=' in pair:
k, v = pair.split('=')
setattr(p, k, v)
return p
def profile_for(email):
'''
Return a profile for a given email address.
Eat/quote out the metacharacters (& and =) in the email field.
'''
p = Profile()
p.email = email.translate(str.maketrans({'&':None,'=':None}))
p.uid = 10
p.role = 'user'
return p
PROFILE_SECRET_KEY = None
def encrypt(encoded):
'''
Encrypt a k=v encoded string using AES ECB mode.
Returns the ciphertext.
'''
global PROFILE_SECRET_KEY
if not PROFILE_SECRET_KEY:
PROFILE_SECRET_KEY = crypto.random_AES_key()
plain = crypto.str_to_bytes(encoded)
plain = crypto.plaintext_pad_PKCS7(plain)
return crypto.encrypt_AES_ECB(plain, PROFILE_SECRET_KEY)
def decrypt(cipher):
'''
Decrypt a ciphertext and parse it to produce a profile..
'''
global PROFILE_SECRET_KEY
if not PROFILE_SECRET_KEY:
PROFILE_SECRET_KEY = crypto.random_AES_key()
plain = crypto.decrypt_AES_ECB(cipher, PROFILE_SECRET_KEY)
# Crudely remove any padding
plain=plain.decode()
plain = ''.join(filter(string.printable.__contains__, str(plain)))
print('About to parse {0}'.format(plain))
return parse(plain)