我已经编写了一些代码来生成CSR以及私钥。
对<textarea>
的响应使用换行符很好地格式化(如果你知道正确的CSR /私钥是什么样的)。
我将私钥保存到.pfx文件类型时遇到问题,因为它不能很好地格式化。
以下是放入<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
uUURrNmeZRQnCGaj51wn8KYejCS7ReL071MdhwgJesCJzTpZFJ+HpLJSZI8FJQpd
pzX8rZ+4UfXMd1bcSaLMpgRZJrzGLx5uugO2sgTFelF6rzcg+yD+/JOETzSE3Am1
L5TA7MA1AoGBAPsxFTGAwg3Vvwg2xrSBAaUu2Vu8R3mvCRp2fjX1Q9CUM5vJpkqH
8h1xX3miVsGF//+W0yoSzVxcoPEALre5kiEQrmKYtXCaL6b4JY3BHJrUl4yIh0iO
1luFitq/T4R2flmshESd4SolqvdrpTVg3vryj9v9X75sIFlssdV3slZDAoGBALSn
3coa/8Dha5v+qBnWkHZcVGe/q9OOGJWloAbgI/S9TjcKqwe5u+EtvS2S3MurDbE6
uUBC85wCqjDelc36mn84f0YXDDy6vtZp0HoErGL7/wXL5zn52iRmVvF31vU1ahcK
2TT62MYQ8aCr7nNczvG8iF43hbfCd5c5yWDekwe3AoGAQ6REAMCbgP4+IwgdGh5j
QwgKoBljZfEskmO2OPGDAXfnLdxvW0KggC03eJmuow5ikYEb/Ah0JJsM+9kAu5jN
MPo8+3AD6/6ZNm08L6ABA/CbnsxlIbTVJHAhpCZAU3tVKvC57YBWUfMyxs3F/0nG
wezvsF3amnyjeXE7sjA5ZeECgYBzsendTCssaHEVByK64WnFE15OzzaIlsDx3Y89
t//u9emIYIGlwfIrxLIZ5KsYmCR9syD+oaIH8MDz6SjBMFQPU6xWw93naqVbBYsp
CaMpBT+Og8ZCn9tvYkcd/2SfxyR5O05dmqSHPESyZEmigfZaZCZlSGOPirAyHiT0
r7YzwwKBgAQSh1wgnlOaadkKHzItrXf9i9dUaAzuUYj/B/EpZR+y5ckXFuohpczE
rQ4yJIgvynxKJW7r2AVS0ad/fbgh5YNCaykB8HDhP1ZQY8ng1ae6VoHRQiLZAdKx
cAqWIvwWEjAFFbHixt4d3OChDlubpeB3p24MlWycc1H63L4RKAto
-----END PRIVATE KEY-----
文件中的样子如下:
"-----BEGIN PRIVATE KEY-----\nMIIEowIBAAKCAQEAsUM5i1BX1NWS1CGsou86LN3oRcfkA63FdqneDLi36602dxQO\npSPabp8lqR+LgIWq/nIxShbZgc5YwlmhylrqYm1jdHXUQlNhBjsJE6Y0SwybXD9k\nERyWgFfaFLeUjUTXax01/M9oh4JQ+o4pWz+cw3oUjklH5iviLx34bZMq0k5azLus\n312FRgA1x2AKG4QWUwTijTISYZ4mtLDTli2iqWVvPpLi87hLKTAMmLdJZ8hJkXOJ\nQYRALpiERwX4lhckh1xnr/NfzE+QY0zxGwmvE4Uk0MqT7liUfqPnXiMLIIGuC/pt\nxf2SXQBv/A9eus2jI0gM627iyvbDdkw0E1B+5QIDAQABAo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1wgnlOaadkKHzItrXf9i9dUaAzuUYj/B/EpZR+y5ckXFuohpczE\nrQ4yJIgvynxKJW7r2AVS0ad/fbgh5YNCaykB8HDhP1ZQY8ng1ae6VoHRQiLZAdKx\ncAqWIvwWEjAFFbHixt4d3OChDlubpeB3p24MlWycc1H63L4RKAto\n-----END PRIVATE KEY-----"
我的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)
我保存为文件代码:
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();
}
我需要帮助格式化文件内容。
API的原始回复是:
{
"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+6SH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WxOg\\ncVhmamk/w1L67RtzwJ69S6sTECwfnHDlOBLJwCTtVzGTfHZQ1QBctNnyZ1sHUetd\\nwX5QuEO1vsI/T7jpozg0PGr+5BE5r1kr6VjvPBe+WOhO/4X1wUFt\\n-----END PRIVATE KEY-----\",\"status\":\"Success\"}"
}
}
}
答案 0 :(得分:2)
为什么要将您的PFX文件存储为JSON格式?只需删除JSON.stringify
并将其输出为纯文本,无论如何,看到您传入的内容只是一个基本字符串。 stringify所做的就是将它包装在引号中并对新行进行编码。
var dataStr = "data:text/plain;charset=utf-8," + exportObj;
这是一个正常工作的JSbin:http://jsbin.com/lipaxuhidi/edit?html,js,output