0
I made a program that generates a public key and another private key, serializes them and saves them in a .key. Dai 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)
But
public_key
is, in fact, astr
and not this methodencrypt
. What are you trying to do?– Wallace Maxters
there is some way I can use the key created in another program that generated the key and use in this?
– Nathalia Dias
or how to put Encrypt method?
– Nathalia Dias
The question is, where do you want to call this
encrypt
. The first information you already have: It is not instr
that you do it. I imagine you wanted to use something from the librarycryptography
, perhaps it was the case to give a read in the documentation.– Wallace Maxters
Nathalia, spy on this one doc. Looks like the lib you’re using
– Wallace Maxters