Beispiel #1
0
        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));
        }
Beispiel #2
0
        /// <summary>
        /// Deserialize a private key from a stream
        /// </summary>
        /// <param name="stream">stream to read from</param>
        /// <returns>private key</returns>
        public static PrivateKey Unmarshal(Stream stream)
        {
            var pb = Serializer.Deserialize <PrivateKeyContract>(stream);

            switch (pb.Type)
            {
            case KeyType.RSA:
                return(RsaPrivateKey.Unmarshal(pb.Data));

            case KeyType.Ed25519:
                return(Ed25519PrivateKey.Unmarshal(pb.Data));

            default:
                throw new Exception("Bad key type");
            }
        }