bcgit / pc-dart

Pointy Castle - Dart Derived Bouncy Castle APIs
MIT License
230 stars 120 forks source link

Converting PublicKey and PrivateKey objects to a string that can be stored in secure storage #213

Closed bahman2000 closed 8 months ago

bahman2000 commented 8 months ago

This is a method to generate an RSA key pair:

AsymmetricKeyPair<PublicKey, PrivateKey> getRsaKeyPair( SecureRandom secureRandom) { var rsaparams = RSAKeyGeneratorParameters(BigInt.from(65537), 4096, 5); var params = ParametersWithRandom(rsaparams , secureRandom); var keyGenerator = RSAKeyGenerator(); keyGenerator.init(params); return keyGenerator.generateKeyPair(); }

How can I convert PublicKey and PrivateKey objects to string?

Ephenodrom commented 8 months ago

@bahman2000 Take a look at the following package : https://github.com/Ephenodrom/Dart-Basic-Utils

This is basically a wrapper package around pointycastle, offering some methods to convert keys to PEM strings. Take a look at the CryptoUtils class. There are methods like "encodeRSAPrivateKeyToPem" that take a RSAPrivateKey from pointycastle.