Ledger Nano X - The secure hardware wallet
SHARE
TWEET

PGP key serialization and deserialization

a guest Feb 21st, 2014 225 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.                 public static string PrivateKeyToString(PgpPrivateKey key)
  2.                 {
  3.                         PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(key.Key);
  4.                         byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetDerEncoded();
  5.                         return Convert.ToBase64String(serializedPrivateBytes);
  6.                 }
  7.                 public static string PublicKeyToString(PgpPublicKey key)
  8.                 {
  9.                         SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(key.GetKey());
  10.                         byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
  11.                         return Convert.ToBase64String(serializedPublicBytes);
  12.                 }
  13.                 public static PgpPrivateKey PrivateKeyFromString(string serialized, long keyId)
  14.                 {
  15.                         AsymmetricKeyParameter akp = PrivateKeyFactory.CreateKey(Convert.FromBase64String(serialized));
  16.                         return new PgpPrivateKey(akp, keyId);
  17.                 }
  18.                 public static PgpPublicKey PublicKeyFromString(string serialized, DateTime creationTime)
  19.                 {
  20.                         AsymmetricKeyParameter akp = PublicKeyFactory.CreateKey(Convert.FromBase64String(serialized));
  21.                         return new PgpPublicKey(PublicKeyAlgorithmTag.RsaGeneral, akp, creationTime);
  22.                 }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top