Volker Wild

Volker Wild

  • NA
  • 7
  • 68

RSA create PublicKey and PrivateKey in xml or pub

Jan 24 2025 8:19 AM

Hello, I have a question. For example, I have: P: 5 Q: 17 phiN: 64 N: 85 e:5 d: 13 How can I generate the following from these values: PublicKey.xml and PrivateKey.xml ? In the source code below, the parameters are created automatically. How can I insert my own parameters here, e.g. from above?

 

 

     public ICommand GenerateNewKey
        {
            get
            {
                return generateNewKey ?? (generateNewKey = new DelegateCommand(a =>
                {
                    var rsaCryptoServiceProvider = new RSACryptoServiceProvider();

                    XmlRsaPublic = rsaCryptoServiceProvider.ToXmlString(false);
                    XmlRsaPrivate = rsaCryptoServiceProvider.ToXmlString(true);

                    var rsaPrivateKey = new OpenSSL.Crypto.RSA
                    {
                        SecretPrimeFactorP = BigNumber.FromArray(rsaCryptoServiceProvider.ExportParameters(true).P),
                        SecretPrimeFactorQ = BigNumber.FromArray(rsaCryptoServiceProvider.ExportParameters(true).Q),
                        DmodP1 = BigNumber.FromArray(rsaCryptoServiceProvider.ExportParameters(true).DP),
                        DmodQ1 = BigNumber.FromArray(rsaCryptoServiceProvider.ExportParameters(true).DQ),
                        IQmodP = BigNumber.FromArray(rsaCryptoServiceProvider.ExportParameters(true).InverseQ),
                        PrivateExponent = BigNumber.FromArray(rsaCryptoServiceProvider.ExportParameters(true).D),
                        PublicExponent = BigNumber.FromArray(rsaCryptoServiceProvider.ExportParameters(true).Exponent),
                        PublicModulus = BigNumber.FromArray(rsaCryptoServiceProvider.ExportParameters(true).Modulus)
                    };

                    PemRsaPrivate = rsaPrivateKey.PrivateKeyAsPEM;
                    PemRsaPublic = rsaPrivateKey.PublicKeyAsPEM;

                    DerRsaPrivate = BitConverter.ToString(Convert.FromBase64String(PemRsaPrivate.Trim().Substring(31, PemRsaPrivate.Length - 61).Trim()));
                    DerRsaPublic = BitConverter.ToString(Convert.FromBase64String(PemRsaPublic.Trim().Substring(26, PemRsaPublic.Length - 51).Trim()));
                }));
            }
        }