'str' object has no attribute 'encrypt'

0

I made a program that generates a public key and another private key, serializes them and saves them to a .key file. Dai I tried to create another program to encrypt and decrypt a message with these two keys generated in the other program, how can I do this?

from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding

#o que ira ser criptografado.
message = b"UMA CHAVE MUITO SECRETA"
#chave privada gerada em outro programa e salva em um arquivo .key
private_key ="""-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIFLTBXBgkqhkiG9w0BBQ0wSjApBgkqhkiG9w0BBQwwHAQIxLaJgjeSQbcCAggA
MAwGCCqGSIb3DQIJBQAwHQYJYIZIAWUDBAEqBBDskixa53SXACIzrP/txssvBIIE
0HkKNOthGTkec0AtC28AX0H/8kCfuboCTnIejxJ7b1KUJc3g4tn51OLPwavEQw2q
2Lk8DgPHPVoWzYp7wU8a0wdWKkeZW/il6c+v7CjwxdO2SFVxsPj4UuCwcfx0+WcE
8EA9ueaPQTwF+DsSbDSsee24ajeOOTYJQbuliImedcrG8mt3pjawJ+KiN0xY9p15
6nnGZj8A5GFQJrSYae4hl46qycZ+B5CYSvnmprHDmqUopEESXCnl6TT8e8NoFASM
xNJOQsd7B0Lgj1HliQz22MXq9T/H8bWEB1lw2vtWmWS8EBDlUuanC779CTYjbVUW
s+DyJ4hl/6t5/rzQBKiLcbQqtzDTeTEp9GfH845Ui4fAoQ2vuPq5CoxlD6raSBTv
9X2GsRaBYtfiXSvWcXeo3bl4cvLIAr/ogIuKo+wVoWF579Jgxb4NhBCjRUwBfJY3
GjGIY4lLVoQ4O570uVEYotlzKnP0JmHiBh+ltPfV9GH5u5ZEIaiWXUQMlR/PRbaw
y57O+O7pUfBt7SmBvijL0+BaIG3NKt+a8lThRn8pIl4Q4bLm8sm6RAPFOCGD8XDa
Q3+7LbqGwDjHQ1+lYG9ZHvK3KuCKTq0uTAWSmJwh9cnNyO34QmoZI/PX4BiXXlj7
gbsCapn0BEOpW+WIekda50yxUSagfLTdUzvMS4ZcxUW6vEdNuFygRSXkPStmKpsC
QR8QppDfLT/0nlUPifxt5clKUekImy5nTTFloP/udEfxoAdscSWHOrXYmLnFvdk2
Cyw/PwEKjo/MMpDi1dkR0v6aNeVIU87QRgCVp8xq93fqGNzLod7hSqQ2mu3sSKms
DjPObtQ6WPgKN4iyNBRjRjkQsrB6tF1rThhgvXZuY0ubSVtMd688SK7I1vbe51Es
UsVS/Ivf8th7jHSFsZI40h58D37HC6Djj+s4D0Ip9DwsX4MjUFcp4XKKqlQO/106
Jmgyui72LEopWpP8ysErqlnrn67ByIqrQZr92wZEyczK+uZDCrBJHPU5tAB1zN8O
jTpGRIh/SZxkH3r/i34p+jGZWbPEzUbz57d8Y9f2/sIqGqOS0hXqEInljSM0Hpe3
Y71l4dDHPrHVL6KeBHW7Z3kUHGv2AfCYUQrvP0jp7VzgGHmLLcNx4Mtra4QQe798
Oqn3MJMiOGWyKx7md46NAfHwguRrQbQWBCEh5rL+GU/bVXthKEAgvU6hNCd9f0z0
sGsCR5/jeFyNHQ0L1JhsjpqBiYKrh9PZhO4eeKBh9Jnm+pstotG/7SyxBo83sDQb
sT8SniRqRkdnHi6Rv4RNNpfh5Obd6hGV7Mi4TdPmvCTc4WF32gUU0QGoXw0CvWtR
DnljEYWj199jwSdUrHeMjnjKtCJDt3+FoNVxM+TCC9zmMWq1Oj0FSu9CAZM5dlYZ
knO+sLo3K36ptfFbFofG9WmaElPIlRPdx0YMf32U8gRgKpZBZoztlEtjeqrf9rvP
0PrpUweJzVBfvt9q+TvG35TMa4IPUeVi0Od1dtk6pdPmMHlG7z1+LBaRMlm1wzj5
Qxc30xZPFOWCmsLUJah+oxovEFcyWeQ6rH7qufqzEYHLm876uxKGSKJSTwYk5Boi
4XsrVl9UUGjvh9XsnMnIGc/xSrTuNAW4qhLvrdcuPXf8
-----END ENCRYPTED PRIVATE KEY-----
"""
#chave publica gerada em outro programa e salva em um arquivo key.
public_key = """-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxu6aLyNkusNci2aiIVoK
5Bc8JcwsBx5Rfq/S7u0OWRnQz+KXnJ+QEOTozP/ywzaubsuVSEr73dtkbrSvx/7K
mGg9qiqgR6f3pUOKt4d7FGV/HBwYcHaEI8albex9vTInjsT5YiLG0fppjckLvYrZ
xtjUcOsbOCYr7hUc1YgbyM5whbXbuCmC48KzAvEwIZnq1K0qqM5rea7y88a0Izr6
ADt6aGe/O8p13TSWbBeH+tufISSDBo2GN+KCWgYJ6heLcu7sS+9SvdxDwp+KaO0z
lWOocA+Tcpc3Zu5KDS/lIq1LZDAmjVIl6I4+8UvGDmhnIvAKHFroW1Ulg7TptBKz
pwIDAQAB
-----END PUBLIC KEY-----
"""
#criptografando
ciphertext = public_key.encrypt(
    message,
        padding.OAEP(
        mgf=padding.MGF1(algorithm=hashes.SHA1()),
        algorithm=hashes.SHA1(),
        label=None
    )
)
print(ciphertext)
#descriptografando
normaltext = private_key.decrypt(
    ciphertext,
    padding.OAEP(
        mgf=padding.MGF1(algorithm=hashes.SHA1()),
        algorithm=hashes.SHA1(),
        label=None
    )
)

print(normaltext)
    
asked by anonymous 19.10.2018 / 21:05

1 answer

2

You're missing code here - You try to call public_key.decrypt() but public_key is str - a default type that's already defined in python - and no str has methods decrypt nor encrypt then one piece is missing in your puzzle. I believe that these methods that you want to call must be defined in another object of the encryption library you are using, not objects of type str .

See this example from the library documentation :

# ...
>>> from cryptography.fernet import Fernet
>>> key = Fernet.generate_key()
>>> f = Fernet(key)
>>> token = f.encrypt(b"my deep dark secret")
# ...

As you can see, it calls the .encrypt() method in f which is an instance of the Fernet() class and not a str . It passes the cryptographic key to this Fernet() class which returns a f object, which, yes, has the encrypt() method.

    
19.10.2018 / 21:28