Exemplo n.º 1
0
        /// <summary>
        /// Private Key Convert Pkcs1->xml
        /// </summary>
        /// <param name="privateKey"></param>
        /// <returns></returns>
        public static string PrivateKeyPkcs1ToXml(string privateKey)
        {
            privateKey = RSAPemFormatHelper.Pkcs1PrivateKeyFormat(privateKey);

            var pr = new PemReader(new StringReader(privateKey));

            if (!(pr.ReadObject() is AsymmetricCipherKeyPair asymmetricCipherKeyPair))
            {
                throw new Exception("Private key format is incorrect");
            }

            var rsaPrivateCrtKeyParameters = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo(asymmetricCipherKeyPair.Private));

            var element = new XElement("RSAKeyValue");

            var privateModulus  = new XElement("Modulus", Convert.ToBase64String(rsaPrivateCrtKeyParameters.Modulus.ToByteArrayUnsigned()));
            var privateExponent = new XElement("Exponent", Convert.ToBase64String(rsaPrivateCrtKeyParameters.PublicExponent.ToByteArrayUnsigned()));
            var privateP        = new XElement("P", Convert.ToBase64String(rsaPrivateCrtKeyParameters.P.ToByteArrayUnsigned()));
            var privateQ        = new XElement("Q", Convert.ToBase64String(rsaPrivateCrtKeyParameters.Q.ToByteArrayUnsigned()));
            var privateDp       = new XElement("DP", Convert.ToBase64String(rsaPrivateCrtKeyParameters.DP.ToByteArrayUnsigned()));
            var privateDq       = new XElement("DQ", Convert.ToBase64String(rsaPrivateCrtKeyParameters.DQ.ToByteArrayUnsigned()));
            var privateInverseQ = new XElement("InverseQ", Convert.ToBase64String(rsaPrivateCrtKeyParameters.QInv.ToByteArrayUnsigned()));
            var privateD        = new XElement("D", Convert.ToBase64String(rsaPrivateCrtKeyParameters.Exponent.ToByteArrayUnsigned()));

            element.Add(privateModulus);
            element.Add(privateExponent);
            element.Add(privateP);
            element.Add(privateQ);
            element.Add(privateDp);
            element.Add(privateDq);
            element.Add(privateInverseQ);
            element.Add(privateD);

            return(element.ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Private Key Convert Pkcs1->Pkcs8
        /// </summary>
        /// <param name="privateKey"></param>
        /// <returns></returns>
        public static string PrivateKeyPkcs1ToPkcs8(string privateKey)
        {
            privateKey = RSAPemFormatHelper.Pkcs1PrivateKeyFormat(privateKey);

            var pemReader = new PemReader(new StringReader(privateKey));

            if (pemReader.ReadObject() is AsymmetricCipherKeyPair asymmetricCipherKeyPair)
            {
                using var writer = new StringWriter();
                var pemWriter = new PemWriter(writer);
                var pkcs8Gen  = new Pkcs8Generator(asymmetricCipherKeyPair.Private);
                pemWriter.WriteObject(pkcs8Gen);
                pemWriter.Writer.Close();
                return(writer.ToString());
            }

            throw new ArgumentException($"Unknown format for Private Key: cannot convert to {nameof(AsymmetricCipherKeyPair)}.");
        }