Задать вопрос
@Pyhon3x

Как расшифровать RSA с помощью Python?

Как расшифровать текст Зашифрованный так:
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
jThIJCyD8Fda+tY5HfVlAef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QTvC4JERIypOnkti3xpsWaedP5fHHqveSDeR6ObRRTFcM990EogN9IzE
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;
?>


Как такой текст расшифровать уже на Python 3?
Спасибо!
  • Вопрос задан
  • 1120 просмотров
Подписаться 1 Простой 1 комментарий
Решения вопроса 1
fox_12
@fox_12 Куратор тега Python
Расставляю биты, управляю заряженными частицами
Как такой текст расшифровать уже на Python 3


К примеру вот так:

import rsa
import base64

PRIVKEY = '''<<<SOMEDATA777
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEA3bv1e8E+bR53cvDwwwAF3Yhc+g47OvFpmmb0oi/H5xV/u1us
UCBqgC0Apz1CcAAQJM/0TrKF7alfDeAvUD2E0E8SU7UxBHc3Bsn75Gs0wt3kVzqL
TBfBRBW/qVdcNINkAnQUrOJ784OX8bMbH1DtMbvZAoBHFOaEv5G/fUWICBARsEsl
hSvFxCXyY4tBwDgPTP1QaF/pzlHhl2L8EVUNNjworEoG897uaDwyJbbShfMlrgZG
zEnNFr6WYlCsHAlKoWKYq2Duag4ojPV7bh9sVyXCketlZHkYgLb0suw5jZZYuZ42
UhPtQYNKp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
mGCVAoGAK+IfA2Nkqi09xsU1KPPJTL4P54OsEbE5fNm+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.
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
tumbler
@tumbler Куратор тега Python
бекенд-разработчик на python
С помощью библиотеки cryptography.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы