0

I tried to import this RSA public key :

from hashlib import md5
from base64 import b64decode
from base64 import b64encode 
import json
from Crypto.Cipher import AES
from Crypto.PublicKey import RSA
from Cryptodome.Util import Padding
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad
from Crypto.Util import asn1
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
from base64 import b64decode
import binascii

public_key = """MIGqAgEAAoGBAI688Ywhsy+PP47tnnXp2wqLvVLRZ7uvheTjKfxZFy1RYWl886c5\r\nzFndlBFOcYcr5NCfDDjjV+7CbPXo6Rid3xh+o36uEX/RIOWPq/TAuqlmkBPJpjvg\r\npYbbOcAjX3kZ0fT78GLpf8V6Vk3ctAj2bVeDlpoDOXIfgqE7lcMzbTAnAgMBAAEC\r\nAwEAAQIDAQABAgMBAAECAwEAAQIDAQABAgMBAAE=""".replace('\r\n', '\n')

raw_key = b64decode(public_key)
recipient_key = RSA.import_key(raw_key)


but got Error !

ValueError: RSA key format is not supported

then tried another way :

formated = "-----BEGIN PUBLIC KEY-----\n"+raw_key +"\n-----END PUBLIC KEY-----"
recipient_key = RSA.import_key(formated)

but this didn't work !

verified the key with https://holtstrom.com/michael/tools/asn1decoder.php

and key is correct !

I want to use this key to encrypt data with python ,

Can someone please help me out?

mohammad sf
  • 13
  • 1
  • 3
  • 1
    This is not a valid RSA key. The key has the format of a private key in PKCS#1 format but with invalid values for p, q, dp, dq and inverse q. Check the key in an ASN.1 parser, e.g. https://lapo.it/asn1js/ (after removing the \r\n). – Topaco Jan 10 '23 at 19:25
  • 1
    @Topaco: This key was almost certainly created by copy&pasting from [this](https://stackoverflow.com/a/25591659/238704) now deleted stack overflow answer, or by copy&pasting from a source that traces back to it. When I came across it I saw it was plagiarized which gave me a good reason to ask to have it deleted. It's amazing how much damage some of these bad answers can do. I'll bet even chatGPT has 'learned' from it. – President James K. Polk Jan 10 '23 at 20:33

0 Answers0