Skip to content

Commit 5cf791a

Browse files
author
rusrom
committed
Get RSAPublicKey Object from public key string
1 parent c53c746 commit 5cf791a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

load_public_key_from_string.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from base64 import b64decode
2+
3+
from cryptography.hazmat.backends import default_backend
4+
from cryptography.hazmat.primitives import serialization
5+
6+
7+
public_key_string = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3CPXhKOGh6UXbcMD4tJceGVAeNZkdCvwn8ZhVol9+S5zkaEFXxu1nMQQqoHisKhdGowFMqwBbTU7a1yibSEeaiRpSAtLrf9ggedAZHq7UbohaRXaLaqF2xub4WogJMDjts+y+NbgyE31JbNlF0AXjuBc2cQzjeEI8PvbpfE3SGH68jZcKNl9xr0LgIEp0XExtTqBA/NUL1IFfTH7RWr/SZJfMwaB+YL4rDHG0RMQBg6mDwY0Z6NSkdyfxwwEmINJv6oeesAFeomhgsk0iUzbIrqtYwT3zskU+S6hCX65jp/JoxaNXfgn3r7C7wOsExqxq/Bm2nrldDfQ/E9U+mm6OQIDAQAB'
8+
9+
10+
# The data between -----BEGIN RSA PUBLIC KEY----- and -----END RSA PUBLIC KEY----- is actually just base-64 encoded DER data.
11+
der_data = b64decode(public_key_string)
12+
13+
# Return RSAPublicKey object from public key string
14+
# Deserialize a public key from DER encoded data to one of the supported asymmetric public key types.
15+
# https://cryptography.io/en/latest/hazmat/primitives/asymmetric/serialization/#cryptography.hazmat.primitives.serialization.load_der_public_key
16+
public_key_obj = serialization.load_der_public_key(der_data, backend=default_backend())
17+
18+
print(public_key_obj) # <cryptography.hazmat.backends.openssl.rsa._RSAPublicKey object at 0x000001D107C3A160>
19+
print(type(public_key_obj)) # <class 'cryptography.hazmat.backends.openssl.rsa._RSAPublicKey'>

0 commit comments

Comments
 (0)