I am using Python RSA to do RSA encryption and decryption
I have public/private key in string format and the above mentioned library expects it in format class of type rsa.PublicKey
I am not able to convert my string public/private key to required format(class)
Public Key:
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt14jQ0+D8+gpsCPIrCoWVgw8qmH6izDXQTSqHngcGkjuuK58TOOgUo/lari7uTAg5s0ng42WYwQw3uXqa4aKOUMfcLvmn9pALNY3q9oXZa9plxemGR9itlTrY6ZKOX2FrRTB42K6F6YUnMTtjotw/6E3lNQJpFYwyT1EhLV/TP2ds7NVbNEMX+kRcelxD9Cwwigfv+2eljUJP/lQUoNTEJr6oQRibPMSBCRBbljUq5fDSxGrm0WKFLcxDwcf57/qekeWeFkysdzOTSlOQfGs8WLGho3pMNal0uCzEi2SIVPnkg3cNs6nCJ/Y3LCwUcOk1kRJqyZqk46s4iFzEElGqQIDAQAB
The posted key is a Base64 encoded, DER encoded RSA public key in X.509/SPKI format (this can be verified in an ASN.1 parser, e.g. https://lapo.it/asn1js/).
Python-RSA supports for public RSA keys both formats (i.e. X.509/SPKI and PKCS#1) and encodings (i.e. PEM and DER) with the respective methods of the rsa.PublicKey
class, e.g. the posted Base64 encoded, DER encoded X.509/SPKI key can be imported with load_pkcs1_openssl_der()
as follows:
import rsa
import base64
publicKeySpkiDer = base64.b64decode('MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt14jQ0+D8+gpsCPIrCoWVgw8qmH6izDXQTSqHngcGkjuuK58TOOgUo/lari7uTAg5s0ng42WYwQw3uXqa4aKOUMfcLvmn9pALNY3q9oXZa9plxemGR9itlTrY6ZKOX2FrRTB42K6F6YUnMTtjotw/6E3lNQJpFYwyT1EhLV/TP2ds7NVbNEMX+kRcelxD9Cwwigfv+2eljUJP/lQUoNTEJr6oQRibPMSBCRBbljUq5fDSxGrm0WKFLcxDwcf57/qekeWeFkysdzOTSlOQfGs8WLGho3pMNal0uCzEi2SIVPnkg3cNs6nCJ/Y3LCwUcOk1kRJqyZqk46s4iFzEElGqQIDAQAB')
publicKey = rsa.PublicKey.load_pkcs1_openssl_der(publicKeySpkiDer)
A PEM encoded X.509/SPKI key can be imported with load_pkcs1_openssl_pem()
.
A PKCS#1 public key can be imported with load_pkcs1()
. In the second parameter the encoding is specified (with 'PEM'
or 'DER'
).