T
T
The-TS2020-09-29 12:50:06
PHP
The-TS, 2020-09-29 12:50:06

How to decrypt RSA using Python?

How to decrypt text Encrypted like this:
encode.php:

<?php
$pub = <<<SOMEDATA777
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3bv1e8E+bR53cvDwwwAF
3Yhc+g47OvFpmmb0oi/H5xV/u1usUCBqgC0Apz1CcAAQJM/0TrKF7alfDeAvUD2E
0E8SU7UxBHc3Bsn75Gs0wt3kVzqLTBfBRBW/qVdcNINkAnQUrOJ784OX8bMbH1Dt
MbvZAoBHFOaEv5G/fUWICBARsEslhSvFxCXyY4tBwDgPTP1QaF/pzlHhl2L8EVUN
NjworEoG897uaDwyJbbShfMlrgZGzEnNFr6WYlCsHAlKoWKYq2Duag4ojPV7bh9s
VyXCketlZHkYgLb0suw5jZZYuZ42UhPtQYNKpDmWpnrECkUD6T/1UXTRHvubrKnv
ywIDAQAB
-----END PUBLIC KEY-----
SOMEDATA777;
$data = "PHP is my secret love.";
$pk  = openssl_get_publickey($pub);
openssl_public_encrypt($data, $encrypted, $pk);
echo chunk_split(base64_encode($encrypted));
?>

decode.php:
<?php
$key = <<<SOMEDATA777
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEA3bv1e8E+bR53cvDwwwAF3Yhc+g47OvFpmmb0oi/H5xV/u1us
UCBqgC0Apz1CcAAQJM/0TrKF7alfDeAvUD2E0E8SU7UxBHc3Bsn75Gs0wt3kVzqL
TBfBRBW/qVdcNINkAnQUrOJ784OX8bMbH1DtMbvZAoBHFOaEv5G/fUWICBARsEsl
hSvFxCXyY4tBwDgPTP1QaF/pzlHhl2L8EVUNNjworEoG897uaDwyJbbShfMlrgZG
zEnNFr6WYlCsHAlKoWKYq2Duag4ojPV7bh9sVyXCketlZHkYgLb0suw5jZZYuZ42
UhPtQYNKpDmWpnrECkUD6T/1UXTRHvubrKnvywIDAQABAoIBADcHs/gf0KdyPUD4
j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3cz7VV9X+yvV
fseNn1qkQTvC4JERIypOnkti3xpsWaedP5fHHqveSDeR6ObRRTFcM990EogN9IzE
WOUCpOARkqvdlQO5UwP4fVgJmKERicoHjsuWOf4byZad7FjJYE4=
-----END RSA PRIVATE KEY-----
SOMEDATA777;
$data = "qwcxZgHZ1hccWeZG6UHV3s7qLTDBtz+tUiv/qOB9gxtm9JQRXnUb+MWXxU4oU+ek0y5EMR+QTh7w u0WivMjNxmmmvpWkBkcnigsqrosqnksvV5OSmcyu8/dMDdRpT87fsmTjrBlwrXBy+r9fZqQ2eILH X0dEsUNexQy2z24FCVJRpNTwH9bXpgCJkuQKOOA5jze2dUIA+78es1f09B3dQQEEWF5jNv2NlkKa L3teqYe1StJ0V8ButejzDoXzTxjgTLA1J7rJgbiJ7zooPRZafZakZwGDO2c2bB9fGDYd90TD4Y0G mBybEs/nThjGry/F+dtxu1df0O6HgPTDnnol6Q==";
$pk  = openssl_get_privatekey($key);
openssl_private_decrypt(base64_decode($data), $out, $pk);
echo $out;
?>


How to decrypt such text already in Python 3?
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2020-09-29
@Pyhon3x

How to decrypt such text already in Python 3

For example like this:
import rsa
import base64

PRIVKEY = '''<<<SOMEDATA777
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEA3bv1e8E+bR53cvDwwwAF3Yhc+g47OvFpmmb0oi/H5xV/u1us
UCBqgC0Apz1CcAAQJM/0TrKF7alfDeAvUD2E0E8SU7UxBHc3Bsn75Gs0wt3kVzqL
TBfBRBW/qVdcNINkAnQUrOJ784OX8bMbH1DtMbvZAoBHFOaEv5G/fUWICBARsEsl
hSvFxCXyY4tBwDgPTP1QaF/pzlHhl2L8EVUNNjworEoG897uaDwyJbbShfMlrgZG
zEnNFr6WYlCsHAlKoWKYq2Duag4ojPV7bh9sVyXCketlZHkYgLb0suw5jZZYuZ42
UhPtQYNKpDmWpnrECkUD6T/1UXTRHvubrKn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1KPPJTL4P54OsEbE5fNm+hxL1jpfU3cz7VV9X+yvV
fseNn1qkQTvC4JERIypOnkti3xpsWaedP5fHHqveSDeR6ObRRTFcM990EogN9IzE
WOUCpOARkqvdlQO5UwP4fVgJmKERicoHjsuWOf4byZad7FjJYE4=
-----END RSA PRIVATE KEY-----
SOMEDATA777'''

privkey = rsa.PrivateKey.load_pkcs1(
    PRIVKEY,
)

crypted_text = "qwcxZgHZ1hccWeZG6UHV3s7qLTDBtz+tUiv/qOB9gxtm9JQRXnUb+MWXxU4oU+ek0y5EMR+QTh7w u0WivMjNxmmmvpWkBkcnigsqrosqnksvV5OSmcyu8/dMDdRpT87fsmTjrBlwrXBy+r9fZqQ2eILH X0dEsUNexQy2z24FCVJRpNTwH9bXpgCJkuQKOOA5jze2dUIA+78es1f09B3dQQEEWF5jNv2NlkKa L3teqYe1StJ0V8ButejzDoXzTxjgTLA1J7rJgbiJ7zooPRZafZakZwGDO2c2bB9fGDYd90TD4Y0G mBybEs/nThjGry/F+dtxu1df0O6HgPTDnnol6Q=="
crypted_text = base64.b64decode(crypted_text.encode())

message = rsa.decrypt(crypted_text, privkey)
print('DECRYPTED> ', message.decode())

DECRYPTED> PHP is my secret love.

S
Sergey Tikhonov, 2020-09-29
@tumbler

With the help of the cryptography library.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question