先簡單介紹一下RSA妄讯。
- 加密、解密:使用公鑰對數(shù)據(jù)進行加密酷宵,通過私鑰對加密后的數(shù)據(jù)解密亥贸。
- 加簽、解簽:使用私鑰對數(shù)據(jù)進行簽名浇垦,通過加簽名的數(shù)據(jù)和公鑰進行數(shù)據(jù)驗證炕置,以確認合法性。
- 第一步:生成私鑰男韧,這里我們指定私鑰的長度為2048
openssl genrsa -out rsa_private_key.pem 2048
生成的rsa_private_key.pem
內(nèi)容如下:
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAzuT124KbsXqqOvHbpD9uqVBSNbodP9IjErDtvfKvjRlJpI37
FkGZrHzz3w6CoXm2VrPtTOtWDF0FcqEFoeRi4ayEWaShF7kEXI0QCAgqCXZX1Q8w
YPq0PCLJDTfnKvEYP1f83kY2oYLI38v1ft1Usztharkspgum0FaNZrsJnV27tRlH
LNtzsza/B020AAJwRov6xAU09KeMCTjveGI9jNtjt+sN91vXORis9m3jKEYgzKrZ
BFHMRYbaWJQK1oIxFgkwetmst2I3K6QLfOEOoml+FuUJIUQp63JM5UmIkjxo3sK0
fwFpGudlJ2n8FdtDkl2UGgJZgImE9Vc2QPTEbQIDAQABAoIBACSM+opPJqZSH2TO
eZkow6oR0JRBTw2JWDzjGP0ntK/euc3ib2tdJ4L7iDHt5FIWqInku5WNNeR1QpMf
s1kIeheYXMFhfjwsqYKYQS7KEnMdfyLazhcz80d0dcRqDeLeOQRnzPqPuPMLoeU3
qSVCgMYxRWKPWD31bQD9aXmurWIpbcBz3HHqrJ5Q9/dS0GW7tYHZkiqOhks7+sqB
6ld1Io3M1/qsJd5/irXhLolDWDpoYKyx3fsxS46BExxU1Bwuu2FghQf3/dRug4dY
DzkbvINSmUiU0W+8Vnyz+WHyqnZbydS7bpoP5eguRbU1yLR5wbbOozY7XiP6Qi5V
cDC1HAECgYEA57pge5pEpUpDfC//WeDFJ1Uq9PDGYiU0nMiXekFSS3QKZ60bZScN
tCZeebAckJVBXVF5r4aIjM2X3kG8c/mArpc/3byBKzZpWO54pSL97xcaBRvuh2BX
LD+HPcrix5UW1BFFRKuEA7FOUeP7B4jllLx6zgPDTSLnlCOAU5NOKe0CgYEA5JCv
wQ2APchj1/NTT6Ed0NiimnOqtLNcsMGcLTxhZukGoWXzEbGKNSwzlHbgNuWcjZhp
IcX427VjS+w2s10vr91KZcrWf4x2U4Svbv4mZJhgIXvHXbNayei2hYfku5I2g5bC
e1XBljtLM/+9t1eHkQR84uZ5UwvyMyOofDaTtIECgYEA0MqDqJsFgxD44Wq135vA
yIHapLxVY5U2ZJXwtn0o00eJIIU/C9M2a72unmJqOnPhY13IELldS9DrJ51/WdfT
dHxfVUFF+VObdD/agKCYwNbF4RibwWqy4PGUv76Qu1fAK/Arw4Xu+wzFDtAmHVHh
2jRBSmLllBkBI98g/yzY3rkCgYA5pkzOMquP6cVVKKhww0CT9rA8qMP7w+mzI4JS
3pHqtQfZGa/PXKpxcu6nmbw/3HY6OKD7xcVZFU0wTyoU34Ixp20mc5zamwEVW7gs
FKp2YQCOMXw9pAlYN/whff8xWFWrpxDyrtSfIeF8AhtF7SviZfFiAaPEUJUMVQgg
HFvxAQKBgQCY/tdNGZfMUYM0+uxoVKDc6J/QYLgqEU0KC8pDj7HYuKNqPxLvBT38
nUsYEdDFbgQbtLoMtwjjvAQ7RRnwa87jTc3HiNICbDuMFDy14OwXfgDRM6SHCuzp
DMX7hBkzeMzzDqfJZjTR9dIPIj/pP7kBw6CuSGmYByrg+BLwtSOmCw==
-----END RSA PRIVATE KEY-----
- 第二步:根據(jù)私鑰生成對應的公鑰:
openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key_2048.pem
生成的rsa_public_key_2048.pem
文件內(nèi)容為:
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzuT124KbsXqqOvHbpD9u
qVBSNbodP9IjErDtvfKvjRlJpI37FkGZrHzz3w6CoXm2VrPtTOtWDF0FcqEFoeRi
4ayEWaShF7kEXI0QCAgqCXZX1Q8wYPq0PCLJDTfnKvEYP1f83kY2oYLI38v1ft1U
sztharkspgum0FaNZrsJnV27tRlHLNtzsza/B020AAJwRov6xAU09KeMCTjveGI9
jNtjt+sN91vXORis9m3jKEYgzKrZBFHMRYbaWJQK1oIxFgkwetmst2I3K6QLfOEO
oml+FuUJIUQp63JM5UmIkjxo3sK0fwFpGudlJ2n8FdtDkl2UGgJZgImE9Vc2QPTE
bQIDAQAB
-----END PUBLIC KEY-----
- 第三步:私鑰轉(zhuǎn)化成pkcs8格式(該格式一般Java調(diào)用)
openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform pem -nocrypt -out private_pkcs8.pem
生成的private_pkcs8.pem
的文件內(nèi)容為:
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDO5PXbgpuxeqo6
8dukP26pUFI1uh0/0iMSsO298q+NGUmkjfsWQZmsfPPfDoKhebZWs+1M61YMXQVy
oQWh5GLhrIRZpKEXuQRcjRAICCoJdlfVDzBg+rQ8IskNN+cq8Rg/V/zeRjahgsjf
y/V+3VSzO2FquSymC6bQVo1muwmdXbu1GUcs23OzNr8HTbQAAnBGi/rEBTT0p4wJ
OO94Yj2M22O36w33W9c5GKz2beMoRiDMqtkEUcxFhtpYlArWgjEWCTB62ay3Yjcr
pAt84Q6iaX4W5QkhRCnrckzlSYiSPGjewrR/AWka52UnafwV20OSXZQaAlmAiYT1
VzZA9MRtAgMBAAECggEAJIz6ik8mplIfZM55mSjDqhHQlEFPDYlYPOMY/Se0r965
zeJva10ngvuIMe3kUhaoieS7lY015HVCkx+zWQh6F5hcwWF+PCypgphBLsoScx1/
ItrOFzPzR3R1xGoN4t45BGfM+o+48wuh5TepJUKAxjFFYo9YPfVtAP1pea6tYilt
wHPcceqsnlD391LQZbu1gdmSKo6GSzv6yoHqV3UijczX+qwl3n+KteEuiUNYOmhg
rLHd+zFLj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a -in private_pkcs8.pem -out private_pkcs1.pem
實際private_pkcs1.pem
和 rsa_private_key.pem
內(nèi)容一致才對