Mercurial > hg > easyhg
comparison easyhg.py @ 446:f72cad3baa34
Ensure encryption happens in CBC mode
author | Chris Cannam |
---|---|
date | Tue, 28 Jun 2011 16:32:13 +0100 |
parents | ff6252986354 |
children | 89b6ba707096 |
comparison
equal
deleted
inserted
replaced
445:ff6252986354 | 446:f72cad3baa34 |
---|---|
64 | 64 |
65 def encrypt_salted(text, key): | 65 def encrypt_salted(text, key): |
66 salt = os.urandom(8) | 66 salt = os.urandom(8) |
67 text = '%d.%s.%s' % (len(text), base64.b64encode(salt), text) | 67 text = '%d.%s.%s' % (len(text), base64.b64encode(salt), text) |
68 text += (16 - len(text) % 16) * ' ' | 68 text += (16 - len(text) % 16) * ' ' |
69 cipher = AES.new(key) | 69 cipher = AES.new(key, AES.MODE_CBC) |
70 return base64.b64encode(cipher.encrypt(text)) | 70 return base64.b64encode(cipher.encrypt(text)) |
71 | 71 |
72 def decrypt_salted(ctext, key): | 72 def decrypt_salted(ctext, key): |
73 cipher = AES.new(key) | 73 cipher = AES.new(key, AES.MODE_CBC) |
74 text = cipher.decrypt(base64.b64decode(ctext)) | 74 text = cipher.decrypt(base64.b64decode(ctext)) |
75 (tlen, d, text) = text.partition('.') | 75 (tlen, d, text) = text.partition('.') |
76 (salt, d, text) = text.partition('.') | 76 (salt, d, text) = text.partition('.') |
77 return text[0:int(tlen)] | 77 return text[0:int(tlen)] |
78 | 78 |