diff options
| author | Guillaume Horel <guillaume.horel@gmail.com> | 2010-11-11 21:56:26 -0500 |
|---|---|---|
| committer | Guillaume Horel <guillaume.horel@gmail.com> | 2010-11-11 21:56:26 -0500 |
| commit | f40f2278c9d38825bbffd8c52fbd1753d90b23e7 (patch) | |
| tree | 4a91ef2e5f4b6f1e74e6310a0065bd2839275301 | |
| parent | ed5342ebcd608a77e3102ad04d5f733bdc4d909c (diff) | |
| download | alias-f40f2278c9d38825bbffd8c52fbd1753d90b23e7.tar.gz | |
Fix previous commit (pushed empty file).
| -rw-r--r-- | crypto.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/crypto.py b/crypto.py new file mode 100644 index 0000000..c5d28d2 --- /dev/null +++ b/crypto.py @@ -0,0 +1,29 @@ +from Crypto.Cipher import AES +import os + +def encrypt(msg,key,block_size): + pad = block_size - len(msg) % block_size + data = msg + pad * chr(pad) + iv = os.urandom(block_size) + cipheredtext = iv + AES.new(key, AES.MODE_CBC, iv).encrypt(data) + return cipheredtext + +def decrypt(cipheredtext,key,block_size): + iv = cipheredtext[:block_size] + msg = AES.new(key, AES.MODE_CBC, iv).decrypt(cipheredtext[block_size:]) + #remove the padding + pad = ord(msg[-1]) + msg = msg[:-pad] + return msg + +if __name__ == '__main__' : + block_size = 16 + key_size = 32 + key = os.urandom(key_size) + + msg1 = 'Guillaume is a genius!' + msg2 = encrypt(msg1,key,block_size) + msg3 = decrypt(msg2,key,block_size) + print "original message: " + msg1 + print "encrypted message: " + msg2.encode('hex') + print 'verification: ' + msg3 |
