pythonencryptionattributeerrorfernet

Python 'bytes' object has no attribute 'decrypt'


I'm trying some encryption examples.

and i keep get

dec_msg = dec_key.decrypt(enc_msg)

AttributeError: 'bytes' object has no attribute 'decrypt'

this error.

from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa,padding
from cryptography.hazmat.primitives import serialization,hashes

key = Fernet.generate_key()
f_object = Fernet(key)

# encrypt plain_data with AES
plain_data = bytes(input(), encoding='utf8')
enc_msg = f_object.encrypt(plain_data)

# Get public / private key
with open("public_key.pem", "rb") as key_file:
     public_key = serialization.load_pem_public_key(key_file.read(),backend=default_backend())
with open("private_key.pem", "rb") as key_file:
     private_key = serialization.load_pem_private_key(key_file.read(),password=None,backend=default_backend())

# encrypt symmetric key and decrypt encrypted symmetric key
enc_key = public_key.encrypt(plain_data, padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None))
print("enc_key and enc_message send through insecure channel.")
dec_key = private_key.decrypt(enc_key,padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None))

# decrypt AES encrypted message using decrypted symmetric key
dec_key.decode('utf-8')
dec_msg = dec_key.decrypt(enc_msg)

# print result
print(dec_msg)

I tried to decode my dec_key but it doesnt work.

Is there any more thing to decode? I don't see them.


Solution

  • It looks like there are two problems:

    1. When you create enc_key, you're providing plain_data as input instead of key.

    2. dec_key is an instance of bytes and should be used to create another Fernet instance.

    Here's your example modified to address those issues:

    from cryptography.fernet import Fernet
    from cryptography.hazmat.primitives.asymmetric import rsa,padding
    from cryptography.hazmat.primitives import serialization,hashes
    
    key = Fernet.generate_key()
    f_enc = Fernet(key)
    
    # encrypt plain_data with AES
    print("input message to be encrypted: ", end="")
    plain_data = bytes(input(), encoding='utf8')
    enc_msg = f_enc.encrypt(plain_data)
    
    # Get public / private key
    with open("public_key.pem", "rb") as key_file:
        public_key = serialization.load_pem_public_key(key_file.read())
    with open("private_key.pem", "rb") as key_file:
        private_key = serialization.load_pem_private_key(
            key_file.read(), password=None)
    
    # encrypt symmetric key
    enc_key = public_key.encrypt(
        key,
        padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
        algorithm=hashes.SHA256(),
        label=None)
    )
    
    print("send enc_key and enc_msg through an insecure channel...")
    
    # decrypt symmetric key
    dec_key = private_key.decrypt(
        enc_key,
        padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
        algorithm=hashes.SHA256(),
        label=None)
    )
    
    # decrypt encrypted message using decrypted symmetric key
    f_dec = Fernet(dec_key)
    dec_msg = f_dec.decrypt(enc_msg)
    
    # print result
    print("decrypted message: " + dec_msg.decode('utf-8'))
    

    Output:

    $ python3 example.py
    input message to be encrypted: this is a test
    send enc_key and enc_msg through an insecure channel...
    decrypted message: this is a test