I want to encrypt a messages which I got from user input using Cryptography:
I have the following simple code:
import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
backend = default_backend()
messages = input("Please, insert messages to encrypt: ")
key = os.urandom(24)
print(key)
cipher = Cipher(algorithms.TripleDES(key), modes.ECB(), backend=backend)
encryptor = cipher.encryptor()
cryptogram = encryptor.update(b"a secret message") + encryptor.finalize()
print(cryptogram)
When I hard code the messages "a secret message" with b prefix in the code it works fine. What I wanted to do is to use messagesvariable to get the text from user input.
messages = input("Please, insert messages to encrypt: ")
I've tried several ways to covert the string type from the input to byte type and pass it to encryptor.update method but nothing works for me.
messages = input(b"Please, insert messages to encrypt: ")
cryptogram = encryptor.update(byte, message) + encryptor.finalize()
...
Versions:
Python 3.7.0
Cryptography 2.4
Mac OS