예제 #1
0
        /// <summary>
        /// Convert PEM string to Private Key byte array
        /// </summary>
        /// <param name="privatePEMKey">PEM string</param>
        /// <returns>Private Key</returns>
        public static byte[] RSA_ImportPEMPrivateKey(string privatePEMKey)
        {
            const string header  = "-----BEGIN RSA PRIVATE KEY-----"; //pkcs#1 format
            const string footer  = "-----END RSA PRIVATE KEY-----";
            const string header2 = "-----BEGIN PRIVATE KEY-----";     //pkcs#8 format
            const string footer2 = "-----END PRIVATE KEY-----";

            privatePEMKey = privatePEMKey.Replace("\r", "").Replace("\n", "");

            bool use_2 = false;
            int  start = privatePEMKey.IndexOf(header);

            if (start == -1)
            {
                start = privatePEMKey.IndexOf(header2); use_2 = true;
            }
            int end = privatePEMKey.IndexOf(use_2 ? footer2 : footer);

            if (end == -1)
            {
                throw new ArgumentException("Expected " + (use_2 ? footer2 : footer));
            }
            start += use_2 ? header2.Length : header.Length;

            var base64 = privatePEMKey.Substring(start, (end - start));
            var data   = Convert.FromBase64String(base64);

            return(use_2 ? DerParser.ParseDERPrivateKeyPKCS8(data): DerParser.ParseDERPrivateKeyPKCS1(data));
        }
예제 #2
0
        /// <summary>
        /// Convert PEM string to Public Key byte array
        /// </summary>
        /// <param name="publicPEMKey">PEM string</param>
        /// <returns>Public Key</returns>
        public static byte[] RSA_ImportPEMPublicKey(string publicPEMKey)
        {
            const string header             = "-----BEGIN PUBLIC KEY-----";
            const string header2            = "-----BEGIN RSA PUBLIC KEY-----";
            const string footer             = "-----END PUBLIC KEY-----";
            const string footer2            = "-----END RSA PUBLIC KEY-----";
            const string private_key_header = "-----BEGIN RSA PRIVATE KEY-----";
            const string private_key_footer = "-----END RSA PRIVATE KEY-----";

            publicPEMKey = publicPEMKey.Replace("\r", "").Replace("\n", "");
            bool use_2       = false;
            bool use_private = false;
            int  start       = publicPEMKey.IndexOf(header);

            if (start == -1)
            {
                start = publicPEMKey.IndexOf(header2); use_2 = true;
            }
            //falling to using private key to export
            if (start == -1)
            {
                use_2 = false; use_private = true; start = publicPEMKey.IndexOf(private_key_header);
            }
            if (start == -1)
            {
                throw new ArgumentException("Expected " + header);
            }
            start += (use_2) ? header2.Length : (use_private ? private_key_header.Length : header.Length);

            int end = publicPEMKey.IndexOf(use_2 ? footer2 : (use_private ? private_key_footer : footer));

            if (end == -1)
            {
                throw new ArgumentException("Expected " + (use_2 ? footer2 : (use_private ? private_key_footer : footer)));
            }

            var base64 = publicPEMKey.Substring(start, (end - start));
            var data   = Convert.FromBase64String(base64);

            try
            {
                return(DerParser.ParseDERPublicKeyPKCS8(data));
            }
            catch
            {
                try
                {
                    return(DerParser.ParseDERPublicKeyPKCS1(data));
                }
                catch { }
            }
            return(null);
        }