I'm trying to extract a public key from my certificate but i keep getting this error: ValueError: RSA key format is not supported.. Below is my code:
from Crypto.PublicKey import RSA
bob_crt = 'bob.crt'
public_key = RSA.import_key(bob_crt)
if isinstance(public_key, RSA.RsaKey):
print("RSA public key")
else:
print("Other key")
-----BEGIN CERTIFICATE-----
MIIDzDCCArSgAwIBAgIRAL2spN8ES0R0tFtdixdCL2IwDQYJKoZIhvcNAQELBQAw
GDEWMBQGA1UEAwwNU2VjdXJlTGFicyBDQTAeFw0yMTEyMDUxNjI1MTBaFw0yNDEx
MTkxNjI1MTBaMIGYMQswCQYDVQQGEwJHQjEXMBUGA1UECAwOV2VzdCBZb3Jrc2hp
cmUxFTATBgNVBAcMDEh1ZGRlcnNmaWVsZDETMBEGA1UECgwKU2VjcmV0TGFiczER
MA8GA1UECwwIUmVzZWFyY2gxDDAKBgNVBAMMA0JvYjEjMCEGCSqGSIb3DQEJARYU
Ym9iQHNlY3JldGxhYnMuY28udWswggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK
AoIBAQC0igQyRTkj8QXxS yZEZqucjsLO9nXbQMTETqUGRclv1DvXOz7YEcqoclB
AAQHzPsJH3Wp3nteropURUTqrAjUk5uE4AI5MGEK4KWNijFq8r8wsmqf75eB1G/H
FvQ7yKvzCekOUBAVSNOzYf9piaUgZ0XBvQOQAkpIeJTfxb7z3QnQs8m7x7fhwRil
YZRw68qJMoV1 0mWgAc YQAvNomA PMrYGJoO2KdX4rfOkVVpF6oWUSdY7FUTq7C
hjkS2msc1N7eLhf/LRSVeq8oPbXif4Y129bj vwEes0M/Xx5NyhHvTiy9 QV5/Xd
pbkWUo6eu6zXGtwEyzPDT0D4bAFNAgMBAAGjgY8wgYwwCQYDVR0TBAIwADAdBgNV
HQ4EFgQUqRMA3HZe4XOuXEK10EwqxgJ JycwUwYDVR0jBEwwSoAUAKLtLx6iY6th
wGzhKcoLdzHukz2hHKQaMBgxFjAUBgNVBAMMDVNlY3VyZUxhYnMgQ0GCFAsYq/vt
pDNOrsA sXG2JO42eB0pMAsGA1UdDwQEAwIF4DANBgkqhkiG9w0BAQsFAAOCAQEA
BTUEPp4KyBvQFQUjgWyFO0QGDBZNzcSyhRwGUEhKc/PWXaqxv3RkYKEakqGz 62z
mqun5Bl b6hh2/OspJS6XwBbRmdgaR29ci2DfQl1Gf2q5rRcqbe34SkC/6TJfKLy
r7OmN3kdRZ52O77sMLdkS8x3BzQOAD6y yGI3yF8iQN4VEAZoH2CW8Q5GoEErilD
6QqhsCDGrnIS/FvaQkDkuhWjpf FkeZ/kPIFzlSHiHfKHCtjRHsOp8iCek5nhbVJ
yXW1eo9QO0Z0bfr7hbkCSe/DBp8IvNUYoJ50KzMk454mJCO FSCjZb RLGQqsejK
ND/IBvcNvgK/HzY9g0FRkA==
-----END CERTIFICATE-----
This is the certififcate, its a x.509. Do i convert it to a PEM? I'm really confused. Any help would be great. I'm using Python.
Error is below:
Traceback (most recent call last):
File "C:\Users\Documents\test\encryption\encrypt.py", line 91, in <module>
public_key = RSA.import_key(bob_crt)
File "C:\Users\venv\lib\site-packages\Crypto\PublicKey\RSA.py", line 791, in import_key
raise ValueError("RSA key format is not supported")
ValueError: RSA key format is not supported
Process finished with exit code 1
Thanks
CodePudding user response:
RSA.import_key
expects the content, not the filename. If you read the content first, your script works.
from Crypto.PublicKey import RSA
with open("bob.crt") as f:
bob_crt = f.read()
public_key = RSA.import_key(bob_crt)
if isinstance(public_key, RSA.RsaKey):
print(public_key.export_key())
else:
print("Other key")