openssl加密解密字符串 - Wed, Dec 20, 2023
openssl加密解密字符串
私钥
openssl genrsa -out privkey.pem 2048
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAzBIO7Pd2s6in0cFJAJvWucBZAXM1Cmpx1Fnpot2U8pAXAdW3
R32eGlEz3wqH1AvpikwsHCB8iktjHoI+YMhjTfiHfyBi7ghDoRmbPg3tRv7OXhB1
ax0cDG05VQXVxLo2dKMHE2en5pYxLlw3HJC6r1uDwj4xtFOye6Wdao0F2388vCpK
b5o+uEZWJtLwodQ3azsbEJN2TbjoTy+Zmsvp3N6WK8mv7gppsX91dRTTW51cJJTQ
jwVg0xdMuaUJx/WmW4mivBSTBNF0B/Ovlvb4H3+TRhZc2qXGRQ7fjHWOMYNESw8I
fH4ZCEcYY6+qiViFiQ21b6oHt+4fpDV9cPMLvwIDAQABAoIBAHX4YoR+sjI953YM
pKUjcchD97As55az3kYCDFxIeBiZT5OR9rfvWHyiQLhJoCfnEwUN79n6tcREvHIg
nOraLP4gFERbdpVgVahF40bGSkrmdmpOXXKki1hrs98A1ok5lM1siWSgYaGdZaUF
Th7HVASXhN2/koBKej2cihnUmHRAW5EJKO2N4yDXHH8VU1VWh+vhzaL7Y6M2+P6M
YsvgJtDIP9RBXlE8HKSXDYt6fo6W9ydcCbX1o8TVIacKlxuJifwple8plXcIG8l/
pbhbKUb1vW4g7TiB3N+Qn6EnK0nYbo1JdbHpZMHU69+e74M5WfFaoWysUADd1iiz
wkNLabECgYEA6Fy4XzjhDBWU89FfCZHRtaNX5sAhfZpbhTyQQ4wi1s1hwgmQm221
l7hEpuqFIH/rRXyBioUTMROzKujkNpkVAjuaBADefsC7f1WTAYT+KyKjYY/MJvG+
Jb4PK/XAbTCAJbZzjv+Dq0jBBDaFK6wN57hPShf9txOXpX8S4l3QlQMCgYEA4NSN
66eoBD/xXW1EGZZ2uIYu5polFbmhmvv9gfoLevqFFOrfxjIfIQbr80veWenqdUR3
pmioe1RaNZleyq6Kwuz6DWhixDePmvp+TvUBQ1un0uyWb2qpPG5re6C3V58hamkl
Ptc4CRlzF+OhLvCxKwVS5RVVYw3KV1ef+IFHG5UCgYAJgWmFc7U5IlTimIkTwiRb
EuCQrwFvuvXNgInCSaRPNz2ti2igAx/vofkk/ukNSE5enFZMTV9YnZ6xva9AUIjc
e4W0bjir5MSG1ABcio0/RH58ij5mxXLq5liGitohXV+Qq2MU0X0Yb7R9sKFIBRTY
iuCrfknByIpmmcky+3WB8wKBgQDBVYaE8rMElIfVszQwXLbSjnCeVEc3m11gjPi+
YGqm4e62whKTuF4UZINZwBAAeanoFWxeaPkYtLyuAqkE2XrxjB6qkOz9llmjB2y+
MOlZJuP1+QST4sCM+tbqhJWkf26YtroUg7Xn/T5/eHsOhmOTfwx9eg7nEuDAotUp
OOIQ0QKBgQDNFDrgSX2CFWsHyoz5PqAu4M7x+8nXUfxt4yxo/krBwJNJerP3aiDl
OMliPqn+QoLFkK+oTHypFjvw75JtBdN8Ts2NP2Zd3ornnQpqflFcElZUiGlhhNld
g4h1jOuNOhsg6YKSCtefK2DQ0PN6kKUGN003muHB0yD6xyUjASHovA==
-----END RSA PRIVATE KEY-----
根证书
openssl req -new -x509 -key privkey.pem -out cacert.pem -days 1095
-----BEGIN CERTIFICATE-----
MIIEATCCAumgAwIBAgIUKOW2innz2OBL5SNafQAVOnsGZ2MwDQYJKoZIhvcNAQEL
BQAwgY8xCzAJBgNVBAYTAkNOMREwDwYDVQQIDAhMaWFvTmluZzEPMA0GA1UEBwwG
RGFMaWFuMRMwEQYDVQQKDApDbG91ZFN0b25lMQ4wDAYDVQQLDAVQaXMxMDESMBAG
A1UEAwwJa2Fpc2F3aW5kMSMwIQYJKoZIhvcNAQkBFhR3aW5kLmthaXNhQGdtYWls
LmNvbTAeFw0yMzExMjQwODE5NDdaFw0yNjExMjMwODE5NDdaMIGPMQswCQYDVQQG
EwJDTjERMA8GA1UECAwITGlhb05pbmcxDzANBgNVBAcMBkRhTGlhbjETMBEGA1UE
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MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzBIO7Pd2s6in0cFJAJvW
ucBZAXM1Cmpx1Fnpot2U8pAXAdW3R32eGlEz3wqH1AvpikwsHCB8iktjHoI+YMhj
TfiHfyBi7ghDoRmbPg3tRv7OXhB1ax0cDG05VQXVxLo2dKMHE2en5pYxLlw3HJC6
r1uDwj4xtFOye6Wdao0F2388vCpKb5o+uEZWJtLwodQ3azsbEJN2TbjoTy+Zmsvp
3N6WK8mv7gppsX91dRTTW51cJJTQjwVg0xdMuaUJx/WmW4mivBSTBNF0B/Ovlvb4
H3+TRhZc2qXGRQ7fjHWOMYNESw8IfH4ZCEcYY6+qiViFiQ21b6oHt+4fpDV9cPML
vwIDAQAB
-----END PUBLIC KEY-----
input.txt
012345678
公钥加密
openssl pkeyutl -encrypt -in input.txt -out output.enc -pubin -inkey pubkey.pem
0000000 8075 8343 4796 b1c0 fce7 2591 b993 30bb
0000010 c4b8 ed52 a647 707c 7a0a 0254 7af3 f2f6
0000020 897a fd4d 04ca f212 d9ad e4b2 f11c 433a
0000030 22e3 3210 a4e7 5f47 592e 5ec1 8ebd 295b
0000040 8578 58ee c8d6 4577 787a 70f2 e122 24fa
0000050 11aa 33d9 68bb 2036 72ef 09c6 fa2b 595c
0000060 2391 0444 6ad0 0901 bf62 49a0 f52c 12d9
0000070 779c 0877 c230 187c 7f0a 5f3d 2043 f08b
0000080 d527 564c 6d63 5ae0 29c6 a590 117b 7d53
0000090 be8f bc38 7c0e 682f f76b ee59 a4dd c6c3
00000a0 71bd f180 1d6d 7c82 7602 4529 d7f4 30d7
00000b0 b814 26d0 9283 692e a1be 1a7a f0b0 ea5c
00000c0 363b 6e15 76f4 4ed5 55a2 9732 58dd 0014
00000d0 a793 7a6e 8ae0 1884 bd27 8973 5085 d087
00000e0 c332 79fc 892c 475f 8622 dc23 bdd1 db1b
00000f0 42a9 de27 8dec e684 b04d 6f14 11da 2338
0000100
私钥解密
openssl pkeyutl -decrypt -in output.enc -out input.dec -inkey privkey.pem
012345678