Python: Cryptography
Jump to navigation
Jump to search
Fernet
AES
Fernet
<strong>from</strong> <strong>cryptography.fernet</strong> <strong>import</strong> Fernet
key = Fernet.generate_key()
<strong>print</strong>("key:", key)
msg = "mensagem!"
<strong>print</strong>("msg:", msg)
encrypted = Fernet(key).encrypt(msg.encode())
<strong>print</strong>("encrypted:", encrypted)
decrypted = Fernet(key).decrypt(encrypted).decode()
<strong>print</strong>("decrypted:", decrypted)
<br>AES
<strong>import</strong> <strong>base64</strong><strong>from</strong> <strong>Crypto.Cipher</strong> <strong>import</strong> AES
msg_text = b'test some plain text here'.rjust(32) secret_key = b'1234567890123456'
cipher = AES.new(secret_key,AES.MODE_ECB) <em># never use ECB in strong systems obviously</em> encoded = base64.b64encode(cipher.encrypt(msg_text)) <strong>print</strong>(encoded)
decoded = cipher.decrypt(base64.b64decode(encoded))
<strong>print</strong>(decoded)