Beispiel #1
0
        /// <summary>
        /// Decodes the PKCS8 private key.
        /// </summary>
        /// <param name="pkcs8blob">The pkcs8blob.</param>
        /// <returns></returns>
        public static RSAParameters DecodePkcs8PrivateKey(byte[] pkcs8blob)
        {
            var privKeyInfo = new PKCS8.PrivateKeyInfo(pkcs8blob);

            if (privKeyInfo.Algorithm != Oid.rsaEncryption)
            {
                throw new NotSupportedException("Only RSA keys are currently supported.");
            }

            return(DecodeAsn1RsaPrivateKey(privKeyInfo.PrivateKey));
        }
Beispiel #2
0
        /// <summary>
        /// Encodes as PKCS8 private key.
        /// </summary>
        /// <param name="keyParameters">The key parameters.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">Argument should be an RSA private key.</exception>
        public static byte[] EncodeAsPkcs8PrivateKey(RSAParameters keyParameters)
        {
            if (!IsValidPrivateKey(keyParameters))
            {
                throw new ArgumentException("Argument should be an RSA private key.");
            }

            var privKeyInfo = new PKCS8.PrivateKeyInfo()
            {
                Algorithm  = Oid.rsaEncryption,
                PrivateKey = EncodeAsAsn1(keyParameters)
            };

            return(privKeyInfo.GetBytes());
        }