Exemplo n.º 1
0
 internal static DsaPublicKeyParameters BouncyCastleFromKey(DsaPublicKey key)
 {
     return(new DsaPublicKeyParameters(key.Y.ToBouncyBigInteger(),
                                       new DsaParameters(
                                           key.P.ToBouncyBigInteger(),
                                           key.Q.ToBouncyBigInteger(),
                                           key.G.ToBouncyBigInteger())));
 }
Exemplo n.º 2
0
            /// <summary>
            /// Imports the X509 certificate.
            /// </summary>
            /// <param name="purpose">The purpose.</param>
            /// <param name="input">The input.</param>
            /// <returns></returns>
            public virtual ImportedKeySet X509Certificate(KeyPurpose purpose, Stream input)
            {
                var parser    = new X509CertificateParser();
                var cert      = parser.ReadCertificate(input);
                var bouncyKey = cert.GetPublicKey();

                Key key;

                if (bouncyKey is RsaKeyParameters)
                {
                    var keyParam = bouncyKey as RsaKeyParameters;
                    key = new RsaPublicKey
                    {
                        Modulus        = keyParam.Modulus.ToSystemBigInteger(),
                        PublicExponent = keyParam.Exponent.ToSystemBigInteger(),
                        Size           = keyParam.Modulus.BitLength,
                    };
                }
                else if (bouncyKey is DsaPublicKeyParameters)
                {
                    var keyParam = bouncyKey as DsaPublicKeyParameters;
                    if (KeyPurpose.Encrypt == purpose)
                    {
                        throw new InvalidKeySetException("DSA key cannot be used for encryption!");
                    }
                    key = new DsaPublicKey
                    {
                        Y    = keyParam.Y.ToSystemBigInteger(),
                        G    = keyParam.Parameters.G.ToSystemBigInteger(),
                        P    = keyParam.Parameters.P.ToSystemBigInteger(),
                        Q    = keyParam.Parameters.Q.ToSystemBigInteger(),
                        Size = keyParam.Parameters.P.BitLength
                    };
                }
                else
                {
                    throw new InvalidKeySetException("Unsupported key type!");
                }
                return(new ImportedKeySet(key, purpose, "imported from certificate"));
            }