# 生成私钥
openssl genrsa -out private.pem 1024
Generating RSA private key, 1024 bit long modulus (2 primes)
.................+++++
...................+++++
e is 65537 (0x010001)
# 使用私钥生成公钥
openssl rsa -in private.pem -pubout -out public.pem
writing RSA key
# 查看私钥
cat private.pem
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDb9+S57Ro4r7s0DtjuLBDSQzA/oEE3zCkkaO+t1maZDh73EJwC
8wNP/7WTMD0xsloPTpB30tyIUxDyZq2+pwX4itr4XSfLYrL+hV/6Gb5icCSsw8wP
XGuITTgnhF1iElBbiS6hWo50BgAel42SopTAbgfj/cT1RZ0rTm3DzVgEZwIDAQAB
AoGBAKJcydGK943b+HwmcWzrzcbFnTl5mmoLIlsjsbZIyRY0XBrVgbKTmWBVSLtK
IgEqulsODIjFdc6SMbIapChTtI0vBhYhJr5/yxjycQMXIt6rtM/CKDeiy+fgbF0s
FgNmFxEOhQilxCwPf5sbeYq0Ght4/panv8ZLyrGx8IoUeV/RAkEA/w8vO7K/f/7p
08woePwXLyQ/QefJE+UZKDtzh0zrT9Mwg4ziZs0egaDJIYO0vRwO6RjcX2jTL4S5
i9g7VnvHMwJBANzHk+ACr57Jaiclm6s/hEHiBa6OoNjPtHrVk1APL5oDM0TveFc7
VBRRutzReSWz4ezxvb015x0LR8TsBus/Pf0CQDAoV8y3xTyPjuq0g3fEcUQvXdo9
v1OIUCWug/Op7EwYHfLYqO6sD/R9vTwcuAV0xMPyM7m3+xffwYDMBPghSs0CQQC8
+Lqyz7nAYMfSkBcpQemTdsHEQmKckVP6m5pGuWjWG40joRFRIP/7kGUwIr/eLJTh
eZ0dOekC7CfW/uuBKhWFAkB817v9s7TIPrrTpLcQV3p3d+efijNvp+kvyam7xaEa
lGrgGuCiKCssbpoLvLyptUgs0049gO38NE0QZLXxhDpC
-----END RSA PRIVATE KEY-----
# 查看公钥
cat public.pem
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDb9+S57Ro4r7s0DtjuLBDSQzA/
oEE3zCkkaO+t1maZDh73EJwC8wNP/7WTMD0xsloPTpB30tyIUxDyZq2+pwX4itr4
XSfLYrL+hV/6Gb5icCSsw8wPXGuITTgnhF1iElBbiS6hWo50BgAel42SopTAbgfj
/cT1RZ0rTm3DzVgEZwIDAQAB
-----END PUBLIC KEY-----
const crypto = require('crypto')
const privateKey = `-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDb9+S57Ro4r7s0DtjuLBDSQzA/oEE3zCkkaO+t1maZDh73EJwC
8wNP/7WTMD0xsloPTpB30tyIUxDyZq2+pwX4itr4XSfLYrL+hV/6Gb5icCSsw8wP
XGuITTgnhF1iElBbiS6hWo50BgAel42SopTAbgfj/cT1RZ0rTm3DzVgEZwIDAQAB
AoGBAKJcydGK943b+HwmcWzrzcbFnTl5mmoLIlsjsbZIyRY0XBrVgbKTmWBVSLtK
IgEqulsODIjFdc6SMbIapChTtI0vBhYhJr5/yxjycQMXIt6rtM/CKDeiy+fgbF0s
FgNmFxEOhQilxCwPf5sbeYq0Ght4/panv8ZLyrGx8IoUeV/RAkEA/w8vO7K/f/7p
08woePwXLyQ/QefJE+UZKDtzh0zrT9Mwg4ziZs0egaDJIYO0vRwO6RjcX2jTL4S5
i9g7VnvHMwJBANzHk+ACr57Jaiclm6s/hEHiBa6OoNjPtHrVk1APL5oDM0TveFc7
VBRRutzReSWz4ezxvb015x0LR8TsBus/Pf0CQDAoV8y3xTyPjuq0g3fEcUQvXdo9
v1OIUCWug/Op7EwYHfLYqO6sD/R9vTwcuAV0xMPyM7m3+xffwYDMBPghSs0CQQC8
+Lqyz7nAYMfSkBcpQemTdsHEQmKckVP6m5pGuWjWG40joRFRIP/7kGUwIr/eLJTh
eZ0dOekC7CfW/uuBKhWFAkB817v9s7TIPrrTpLcQV3p3d+efijNvp+kvyam7xaEa
lGrgGuCiKCssbpoLvLyptUgs0049gO38NE0QZLXxhDpC
-----END RSA PRIVATE KEY-----`
let publicKey = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDb9+S57Ro4r7s0DtjuLBDSQzA/
oEE3zCkkaO+t1maZDh73EJwC8wNP/7WTMD0xsloPTpB30tyIUxDyZq2+pwX4itr4
XSfLYrL+hV/6Gb5icCSsw8wPXGuITTgnhF1iElBbiS6hWo50BgAel42SopTAbgfj
/cT1RZ0rTm3DzVgEZwIDAQAB
-----END PUBLIC KEY-----`
let 原始文本 = '中国万岁'
// 公钥加密
let 加密文本 = crypto.publicEncrypt(publicKey, 原始文本).toString('hex')
// 私钥解密
let 解密文本 = crypto.privateDecrypt(privateKey, Buffer.from(加密文本, 'hex')).toString()
console.log({加密文本, 解密文本})
/**
{
'加密文本': '46f5f8796b48a7eae791006e5f8882e657b3988332de28c00f3a1dc988f04c9b7fc9e0b09b1f95c872fe55aad1215dcd29a7c531c88998928d3afa29
9f0f4adda646876f9a9548b2aee30f76ca47bd2cfc8c7ec517a2bd7bd0dbb2a84549aeea122991056afe74b12db94df014dd8bcf74c4988e7f788a5cd8465cb3a1577c6
3',
'解密文本': '中国万岁'
}
**/
© 2020 飞鱼的博客