private static KeyPair GenerateRsaKeyPair(int?bits) { var generator = new RsaKeyPairGenerator(); generator.Init(new KeyGenerationParameters(new SecureRandom(), bits ?? 512)); var pair = generator.GenerateKeyPair(); var priv = (RsaPrivateCrtKeyParameters)pair.Private; var pub = (RsaKeyParameters)pair.Public; var pk = new RsaPublicKey(pub); var sk = new RsaPrivateKey(priv, pub); return(new KeyPair(sk, pk)); }
/// <summary> /// Deserialize a public key from stream /// </summary> /// <param name="stream">input stream</param> /// <returns>public key</returns> public static PublicKey Unmarshal(Stream stream) { var pb = Serializer.Deserialize <PublicKeyContract>(stream); switch (pb.Type) { case KeyType.RSA: return(RsaPublicKey.Unmarshal(pb.Data)); case KeyType.Ed25519: return(new Ed25519PublicKey(pb.Data.Take(32).ToArray())); default: throw new Exception("Bad key type"); } }