{"id":721,"date":"2023-10-24T09:01:01","date_gmt":"2023-10-24T01:01:01","guid":{"rendered":""},"modified":"2023-10-24T09:01:01","modified_gmt":"2023-10-24T01:01:01","slug":"Node.js \u4e2d\u7684 crypto \u6a21\u5757\u751f\u6210\u516c\u94a5\u548c\u79c1\u94a5","status":"publish","type":"post","link":"https:\/\/mushiming.com\/721.html","title":{"rendered":"Node.js \u4e2d\u7684 crypto \u6a21\u5757\u751f\u6210\u516c\u94a5\u548c\u79c1\u94a5"},"content":{"rendered":"
const<\/span> crypto = require<\/span>('crypto'<\/span>);\n\n\/\/ \u751f\u6210\u5bc6\u94a5\u5bf9<\/span>\nconst<\/span> { publicKey, privateKey } = crypto.generateKeyPairSync<\/span>('rsa'<\/span>, {\n  modulusLength<\/span>: 2048<\/span>, \/\/ \u6a21\u6570\u957f\u5ea6\uff0c\u4e00\u822c\u9009\u62e9 2048 \u6216\u4ee5\u4e0a<\/span>\n  publicKeyEncoding<\/span>: {\n    type<\/span>: 'spki'<\/span>, \/\/ \u516c\u94a5\u7f16\u7801\u683c\u5f0f<\/span>\n    format<\/span>: 'pem'<\/span> \/\/ \u516c\u94a5\u8f93\u51fa\u683c\u5f0f<\/span>\n  },\n  privateKeyEncoding<\/span>: {\n    type<\/span>: 'pkcs8'<\/span>, \/\/ \u79c1\u94a5\u7f16\u7801\u683c\u5f0f<\/span>\n    format<\/span>: 'pem'<\/span>, \/\/ \u79c1\u94a5\u8f93\u51fa\u683c\u5f0f<\/span>\n    cipher<\/span>: 'aes-256-cbc'<\/span>, \/\/ \u52a0\u5bc6\u7b97\u6cd5\uff0c\u53ef\u9009<\/span>\n    passphrase<\/span>: 'myPassphrase'<\/span> \/\/ \u52a0\u5bc6\u5bc6\u7801\uff0c\u53ef\u9009<\/span>\n  }\n});\n\nconsole<\/span>.log<\/span>('\u751f\u6210\u7684\u516c\u94a5\uff1a'<\/span>);\nconsole<\/span>.log<\/span>(publicKey);\nconsole<\/span>.log<\/span>('\u751f\u6210\u7684\u79c1\u94a5\uff1a'<\/span>);\nconsole<\/span>.log<\/span>(privateKey);\n<\/code><\/pre>\n

\u4ee5\u4e0a\u4ee3\u7801\u4f7f\u7528\u4e86 Node.js \u5185\u7f6e\u7684 crypto \u6a21\u5757\uff0c\u901a\u8fc7 generateKeyPairSync<\/code> \u65b9\u6cd5\u751f\u6210\u4e86\u4e00\u5bf9 RSA \u5bc6\u94a5\u5bf9\uff0c\u5176\u4e2d\u5305\u62ec\u4e86\u516c\u94a5\u548c\u79c1\u94a5\u3002\u53ef\u4ee5\u901a\u8fc7 publicKey<\/code> \u548c privateKey<\/code> \u53d8\u91cf\u83b7\u53d6\u751f\u6210\u7684\u516c\u94a5\u548c\u79c1\u94a5\u3002<\/p>\n

\u5728\u751f\u6210\u79c1\u94a5\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u52a0\u5bc6\u7b97\u6cd5\u548c\u52a0\u5bc6\u5bc6\u7801\uff0c\u4ee5\u589e\u52a0\u79c1\u94a5\u7684\u5b89\u5168\u6027\u3002\u4f8b\u5982\uff0c\u4e0a\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u4e2d\u6307\u5b9a\u4e86\u52a0\u5bc6\u7b97\u6cd5\u4e3a AES-256-CBC\uff0c\u5e76\u8bbe\u7f6e\u4e86\u52a0\u5bc6\u5bc6\u7801\u4e3a \"myPassphrase\"\u3002\u751f\u6210\u7684\u79c1\u94a5\u4f1a\u4f7f\u7528\u6307\u5b9a\u7684\u52a0\u5bc6\u7b97\u6cd5\u548c\u5bc6\u7801\u8fdb\u884c\u52a0\u5bc6\uff0c\u9700\u8981\u5728\u4f7f\u7528\u79c1\u94a5\u65f6\u8f93\u5165\u6b63\u786e\u7684\u5bc6\u7801\u8fdb\u884c\u89e3\u5bc6\u3002\u8fd9\u6837\u53ef\u4ee5\u589e\u52a0\u79c1\u94a5\u7684\u4fdd\u5bc6\u6027\uff0c\u9632\u6b62\u672a\u6388\u6743\u7684\u8bbf\u95ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Node.js \u4e2d\u7684 crypto \u6a21\u5757\u751f\u6210\u516c\u94a5\u548c\u79c1\u94a5\u4ee5\u4e0a\u4ee3\u7801\u4f7f\u7528\u4e86 Node.js \u5185\u7f6e\u7684 crypto \u6a21\u5757\uff0c\u901a\u8fc7 generateKeyPairSync \u65b9\u6cd5\u751f\u6210\u4e86...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/721"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=721"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/721\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}