I need the way to extract PEM string of Public Key from the x509 certificate PEM string.
Now i'm using basic_utils package, that can generate x509Certificate object from the giving PEM string.
This x509Certificate object have publicKeyData
field that has bytes
field, that represents the public key bytes as String.
However i cannot use this bytes
to generate valid PEM string of public key.
So question is - how can i extract public key as PEM string from the certificate?
Thanks
For RSA public keys:
import 'package:convert/convert.dart';
import 'package:basic_utils/basic_utils.dart';
final bytes = hex.decode(cert.publicKeyData.bytes!);
final key = CryptoUtils.rsaPublicKeyFromDERBytes(Uint8List.fromList(bytes));
final pem = CryptoUtils.encodeRSAPublicKeyToPemPkcs1(key);
The Pkcs1
version of the encoder will bracket the PEM with BEGIN RSA PUBLIC KEY
whereas the non-pkcs1 version will use BEGIN PUBLIC KEY
.