/// <summary> /// Create an RSA parameter based on the XML Format private key /// </summary> /// <param name="privateKey"></param> /// <returns>RSAParameters</returns> protected sealed override RSAParameters CreateRsapFromPrivateKey(string privateKey) { privateKey = RsaPemFormatHelper.Pkcs1PrivateKeyFormat(privateKey); PemReader pr = new PemReader(new StringReader(privateKey)); if (!(pr.ReadObject() is AsymmetricCipherKeyPair asymmetricCipherKeyPair)) { throw new Exception("Private key format is incorrect"); } RsaPrivateCrtKeyParameters rsaPrivateCrtKeyParameters = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo(asymmetricCipherKeyPair.Private)); RSAParameters rsap = new RSAParameters(); rsap.Modulus = rsaPrivateCrtKeyParameters.Modulus.ToByteArrayUnsigned(); rsap.Exponent = rsaPrivateCrtKeyParameters.PublicExponent.ToByteArrayUnsigned(); rsap.P = rsaPrivateCrtKeyParameters.P.ToByteArrayUnsigned(); rsap.Q = rsaPrivateCrtKeyParameters.Q.ToByteArrayUnsigned(); rsap.DP = rsaPrivateCrtKeyParameters.DP.ToByteArrayUnsigned(); rsap.DQ = rsaPrivateCrtKeyParameters.DQ.ToByteArrayUnsigned(); rsap.InverseQ = rsaPrivateCrtKeyParameters.QInv.ToByteArrayUnsigned(); rsap.D = rsaPrivateCrtKeyParameters.Exponent.ToByteArrayUnsigned(); return(rsap); }
/// <summary> /// Convert RSA Private Key from PKCS1 to XML format /// </summary> /// <param name="privateKey">Private Key</param> /// <returns></returns> public static string PrivateKeyPkcs1ToXml(string privateKey) { privateKey = RsaPemFormatHelper.Pkcs1PrivateKeyFormat(privateKey); PemReader pr = new PemReader(new StringReader(privateKey)); if (!(pr.ReadObject() is AsymmetricCipherKeyPair asymmetricCipherKeyPair)) { throw new Exception("Private key format is incorrect"); } RsaPrivateCrtKeyParameters rsaPrivateCrtKeyParameters = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo(asymmetricCipherKeyPair.Private)); XElement privatElement = new XElement("RSAKeyValue"); XElement primodulus = new XElement("Modulus", Convert.ToBase64String(rsaPrivateCrtKeyParameters.Modulus.ToByteArrayUnsigned())); XElement priexponent = new XElement("Exponent", Convert.ToBase64String(rsaPrivateCrtKeyParameters.PublicExponent.ToByteArrayUnsigned())); XElement prip = new XElement("P", Convert.ToBase64String(rsaPrivateCrtKeyParameters.P.ToByteArrayUnsigned())); XElement priq = new XElement("Q", Convert.ToBase64String(rsaPrivateCrtKeyParameters.Q.ToByteArrayUnsigned())); XElement pridp = new XElement("DP", Convert.ToBase64String(rsaPrivateCrtKeyParameters.DP.ToByteArrayUnsigned())); XElement pridq = new XElement("DQ", Convert.ToBase64String(rsaPrivateCrtKeyParameters.DQ.ToByteArrayUnsigned())); XElement priinverseQ = new XElement("InverseQ", Convert.ToBase64String(rsaPrivateCrtKeyParameters.QInv.ToByteArrayUnsigned())); XElement prid = new XElement("D", Convert.ToBase64String(rsaPrivateCrtKeyParameters.Exponent.ToByteArrayUnsigned())); privatElement.Add(primodulus); privatElement.Add(priexponent); privatElement.Add(prip); privatElement.Add(priq); privatElement.Add(pridp); privatElement.Add(pridq); privatElement.Add(priinverseQ); privatElement.Add(prid); return(privatElement.ToString()); }
/// <summary> /// Convert RSA Private Key from PKCS1 to PKCS8 format /// </summary> /// <param name="privateKey">Private Key</param> /// <returns></returns> public static string PrivateKeyPkcs1ToPkcs8(string privateKey) { privateKey = RsaPemFormatHelper.Pkcs1PrivateKeyFormat(privateKey); PemReader pr = new PemReader(new StringReader(privateKey)); AsymmetricCipherKeyPair kp = pr.ReadObject() as AsymmetricCipherKeyPair; StringWriter sw = new StringWriter(); PemWriter pWrt = new PemWriter(sw); Pkcs8Generator pkcs8 = new Pkcs8Generator(kp.Private); pWrt.WriteObject(pkcs8); pWrt.Writer.Close(); string result = sw.ToString(); return(result); }