AES: Difference between revisions
From charlesreid1
(Created page with "<pre> from Crypto.Cipher import AES from Crypto.Util import RFC1751 from Crypto import Random AES_MODE = AES.MODE_CBC def encrypt(text, key): text += '\0' * (16 - (len...") |
Bleep bloop (talk | contribs) m (Bot: Orphan page, add template) |
||
| Line 1: | Line 1: | ||
{{Orphan|date=April 2017}} | |||
<pre> | <pre> | ||
from Crypto.Cipher import AES | from Crypto.Cipher import AES | ||
Revision as of 03:27, 16 April 2017
| O NOES!!!
|
from Crypto.Cipher import AES
from Crypto.Util import RFC1751
from Crypto import Random
AES_MODE = AES.MODE_CBC
def encrypt(text, key):
text += '\0' * (16 - (len(text) % 16))
IV = Random.new().read(AES.block_size)
crypto = AES.new(key, AES_MODE, IV=IV)
return IV + crypto.encrypt(text)
def decrypt(ciphertext, key):
crypto = AES.new(key, AES_MODE, IV=ciphertext[:AES.block_size])
plain = crypto.decrypt(ciphertext[AES.block_size:])
return plain.rstrip('\0')
if __name__ == '__main__':
key = Random.new().read(32)
text = "This is a test"
ciphertext = encrypt(text, key)
print("Plaintext was '%s'" % decrypt(ciphertext, key))
print("Key was '%s'" % RFC1751.key_to_english(key))
https://www.reddit.com/r/cryptography/comments/4yu7mh/code_review_of_aes_implementation_in_python/