简体   繁体   中英

Generate base-64 encoded X509 certificate pair (public/private

I'm trying to generate RSA X509 public/private key to dynamically, below is how I do it with openssh command line:

openssl genrsa -out privatekey.pem 1024
openssl req -new -x509 -key privatekey.pem -out publickey.cer -days 1825
openssl pkcs12 -export -out public_privatekey.pfx -inkey privatekey.pem -in publickey.cer

Also how do I add passphrase to encrypt the private key?

and I only got here by far

//Generate a public/private key pair.  
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

//Save the public key information to an RSAParameters structure.  
RSAParameters rsaKeyInfo = rsa.ExportParameters(true);

I have used this class however it does not result into a valid SSL, when I submit it to developer portal it did not get accepted as valid public key: developer.xero.com/myapps

Regards

Note that I have replaced the RSACryptoServiceProvider class with the recommended RSA base class which is cross-platform and also the better RSA implementation .

This SO question put me in the right direction.

using (var rsa = RSA.Create(1024))
{
    var distinguishedName = new X500DistinguishedName($"CN=SelfSignedCertificate");
    var request = new CertificateRequest(distinguishedName, rsa, HashAlgorithmName.SHA256,RSASignaturePadding.Pkcs1);
    var certificate = request.CreateSelfSigned(DateTimeOffset.Now, DateTimeOffset.Now.AddDays(1825));

    // Create PFX (PKCS #12) with private key
    File.WriteAllBytes("privatekey.pfx", certificate.Export(X509ContentType.Pfx, "RGliXtaLkENste"));

    // Create Base 64 encoded CER (public key only)
    File.WriteAllText("publickey.cer",
        "-----BEGIN CERTIFICATE-----\r\n"
        + Convert.ToBase64String(certificate.Export(X509ContentType.Cert), Base64FormattingOptions.InsertLineBreaks)
        + "\r\n-----END CERTIFICATE-----");
}

I have tested the resulting .cer file on xero so it should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM