private static byte[] GetBytesFromPEM(string pemString, PemStringType type)
        {
            string header;
            string footer;

            switch (type)
            {
            case PemStringType.Certificate:
                header = "-----BEGIN CERTIFICATE-----";
                footer = "-----END CERTIFICATE-----";
                break;

            case PemStringType.RsaPrivateKey:
                header = "-----BEGIN RSA PRIVATE KEY-----";
                footer = "-----END RSA PRIVATE KEY-----";
                break;

            default:
                return(null);
            }

            int start = pemString.IndexOf(header, StringComparison.Ordinal) + header.Length;
            int end   = pemString.IndexOf(footer, start, StringComparison.Ordinal) - start;

            return(Convert.FromBase64String(pemString.Substring(start, end)));
        }
Beispiel #2
0
        public static byte[] GetBytesFromPem(string pemString, PemStringType type)
        {
            string header; string footer;

            switch (type)
            {
            case PemStringType.Certificate:
                header = "-----BEGIN CERTIFICATE-----";
                footer = "-----END CERTIFICATE-----";
                break;

            case PemStringType.PrivateKey:
                header = "-----BEGIN PRIVATE KEY-----";
                footer = "-----END PRIVATE KEY-----";
                break;

            case PemStringType.RsaPrivateKey:
                header = "-----BEGIN RSA PRIVATE KEY-----";
                footer = "-----END RSA PRIVATE KEY-----";
                break;

            default:
                return(null);
            }

            var start = pemString.IndexOf(header) + header.Length;
            var end   = pemString.IndexOf(footer, start) - start;

            return(Convert.FromBase64String(pemString.Substring(start, end)));
        }
        public static byte[] GetBytesFromPem(string pemString, PemStringType type)
        {
            string header; string footer;

            switch (type)
            {
            case PemStringType.Certificate:
                header = "-----BEGIN CERTIFICATE-----";
                footer = "-----END CERTIFICATE-----";
                break;

            case PemStringType.RsaPrivateKey:
                header = "-----BEGIN RSA PRIVATE KEY-----";
                footer = "-----END RSA PRIVATE KEY-----";
                break;

            default:
                return(null);
            }

            var start = pemString.IndexOf(header, StringComparison.Ordinal) + header.Length;
            var end   = pemString.IndexOf(footer, start, StringComparison.Ordinal) - start;

            if (start < 0 || end < 0)
            {
                throw new ArgumentException("Invalid PEM string content: perhaps a certificate is passed instead of the private key or vice versa?");
            }

            return(Convert.FromBase64String(pemString.Substring(start, end)));
        }
		/// <summary>
		/// 
		/// </summary>
		/// <param name="PemString"></param>
		/// <param name="Type"></param>
		/// <returns></returns>
		private static byte[] GetBytesFromPEM(string PemString, PemStringType Type)
		{
			string Header, Footer;

			switch (Type)
			{
				case PemStringType.Certificate:
					Header = "-----BEGIN CERTIFICATE-----";
					Footer = "-----END CERTIFICATE-----";
					break;
				case PemStringType.RsaPrivateKey:
					Header = "-----BEGIN RSA PRIVATE KEY-----";
					Footer = "-----END RSA PRIVATE KEY-----";
					break;
				default:
					return null;
			}

			int Start = PemString.IndexOf(Header) + Header.Length;
			int End = PemString.IndexOf(Footer, Start) - Start;

			if (Start < 0) throw(new Exception("Can't find '" + Header + "'"));
			if (End < 0) throw (new Exception("Can't find '" + Footer + "'"));

			return Convert.FromBase64String(PemString.Substring(Start, End));
		}
Beispiel #5
0
        private static String GetPemStringFromBytes(Byte[] key, PemStringType type)
        {
            String header, footer;

            switch (type)
            {
            case PemStringType.Certificate:
                header = "-----BEGIN CERTIFICATE-----";
                footer = "-----END CERTIFICATE-----";
                break;

            case PemStringType.RsaPrivateKey:
                header = "-----BEGIN RSA PRIVATE KEY-----";
                footer = "-----END RSA PRIVATE KEY-----";
                break;

            case PemStringType.PublicKey:
                header = "-----BEGIN PUBLIC KEY-----";
                footer = "-----END PUBLIC KEY-----";
                break;

            default: return(null);
            }

            var textBase64 = Convert.ToBase64String(key);
            var text       = new StringBuilder(textBase64.Length);

            for (Int32 i = 0; i < textBase64.Length; i++)
            {
                text.Append(textBase64[i]);
                if (i != 0 && (i + 1) % 64 == 0)
                {
                    text.Append(Environment.NewLine);
                }
            }
            var result = String.Format("{0}{1}{2}", header, text, footer);

            return(result);
        }
Beispiel #6
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pemString"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static byte[] GetBytesFromPEM(string pemString, PemStringType type)
        {
            string header; string footer;

            switch (type)
            {
                case PemStringType.Certificate:
                    header = "-----BEGIN CERTIFICATE-----";
                    footer = "-----END CERTIFICATE-----";
                    break;
                case PemStringType.RsaPrivateKey:
                    header = "-----BEGIN RSA PRIVATE KEY-----";
                    footer = "-----END RSA PRIVATE KEY-----";
                    break;
                default:
                    return null;
            }

            int start = pemString.IndexOf(header) + header.Length;
            int end = pemString.IndexOf(footer, start) - start;
            return Convert.FromBase64String(pemString.Substring(start, end));
        }