I've created a script to generate both the CSR and private key.
The response displayed in the <textarea>
is well-formatted with newline characters (assuming familiarity with the correct CSR/Private key format).
However, I'm encountering an issue when saving the private key as a .pfx file, resulting in poor formatting.
Below is how it appears in the browser within a <textarea>
:
-----BEGIN PRIVATE KEY-----
MIIEowIBAAKCAQEAsUM5i1BX1NWS1CGsou86LN3oRcfkA63FdqneDLi36602dxQO
pSPabp8lqR+LgIWq/nIxShbZgc5YwlmhylrqYm1jdHXUQlNhBjsJE6Y0SwybXD9k
ERyWgFfaFLeUjUTXax01/M9oh4JQ+o4pWz+cw3oUjklH5iviLx34bZMq0k5azLus
312FRgA1x2AKG4QWUwTijTISYZ4mtLDTli2iqWVvPpLi87hLKTAMmLdJZ8hJkXOJ
QYRALpiERwX4lhckh1xnr/NfzE+QY0zxGwmvE4Uk0MqT7liUfqPnXiMLIIGuC/pt
xf2SXQBv/A9eus2jI0gM627iyvbDdkw0E1B+5QIDAQABAoIBAQCjpZM/aSnc5FsM
GhZ9yWsktqzTlymKt+dfmIzVo8av/hYVMuAeVw42KBilnOi1+zEUfKnCY3vkGXLZ
4dO6s9pEigZSIuGVZdJh5SiJClymmHnpXOBt572NuQ0tKRosnUxep/YKchRnXciS
t6G4iu6XjGHjxgVpmkPTCdEqn73drkf4jeQQrXWJQeOFH3b0e7XwvkoBKrjn6Tu3
uUUR...
This is how it looks after being saved as a file:
"-----BEGIN PRIVATE KEY-----\nMIIEowIBAAKCAQEAsUM5i1BX1NWS1CGsou86LN3oRcfkA63FdqneDLi36602dxQO\npSPabp8lqR+LgIWq/nIxShbZgc5YwlmhylrqYm1jdHXUQlNhBjsJE6Y0SwybXD9k\nERyWgFfaFLeUjUTXax01/M9oh4JQ+o4pWz+cw3oUjklH5iviLx34bZMq0k5azLus\n312FRgA1x2AKG4QWUwTijTISYZ4mtLDTli2iqWVvPpLi87hLKTAMmLdJZ8hJkXOJ\nQYRALpiERwX4lhckh1xnr/NfzE+QY0zxGwmvE4Uk0MqT7liUfqPnXiMLIIGuC/pt\nxf2SXQBv/A9eus2jI0gM62...
Here's the code used for generating the CSR:
var csrResult = (JSON.parse(csr))
var csrResultResdata = JSON.parse(csrResult["resdata"]["csrgen"]["context"])
var csrResultResdataCSR = csrResultResdata["csr"]
var csrSpliter = csrResultResdataCSR.split("\n\n")
var csrOnly = csrSpliter[0]
var privateKeyOnly = csrSpliter[1]
var exportName = "private_key"
downloadObjectAsJson(privateKeyOnly, exportName)
And the code to save it as a file:
function downloadObjectAsJson(exportObj, exportName){
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj));
var downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute("download", exportName + ".pfx");
downloadAnchorNode.click();
downloadAnchorNode.remove();
}
I would appreciate assistance in improving the file content layout.
Lastly, here is the original API response:
{
"result": {
"code": "0",
"msg": "Command completed successfully"
},
"resdata": {
"csrgen": {
"context": "{\"csr\":\"-----BEGIN NEW CERTIFICATE REQUEST-----\\nMIICtjCCAZ4CAQAwcTELMAkGA1UEBhMCTVkxETAPBgNVBAgTCFNlbGFuZ29yMRUw\\nEwYDVQQHEwxQdWNob25nIEpheWExDzANBgNVBAoMBuerpeWNqzEPMA0GA1UECwwG\\n56ul5Y2rMRYwFAYDVQQDEw1hYmMudGt0YW4uY29tMIIBIjANBgkqhkiG9w0BAQEF\\nAAOCAQ8AMIIBCgKCAQEAj3SON7mz0TZwFHAfy/m3vKICH2BjaZdWJWi7ZPLo2uYC\\n6070cU7hd5iM8+q7VEYSUJb8XisuGpKSakx0xWXuXkJfrciR9P5dypEWAUle3e+z\\nHaQMhu9eJu7W0do96f8WLJoy/T1jYIF6p2hhwRy368FrUtWGJQ+6SH5MTTRytOl5\\nayPdvJiW2AmTwv26OG47eDXZqQOFO+MBKd12DYZxBDTpswubhs1rYT5tA5yQA6HP\\ndG+40LxoNqcZVJ/aYvftOVS3JS...