openssl命令加密/解密操作
2020-03-05 本文已影响0人
牙齿不帅
//查看openssl版本,没有就安装一个:yum install openssl openssl-devel
$ openssl version -a
LibreSSL 2.6.5
built on: date not available
platform: information not available
options: bn(64,64) rc4(16x,int) des(idx,cisc,16,int) blowfish(idx)
compiler: information not available
OPENSSLDIR: "/private/etc/ssl"
//随机生成一个私钥文件:rsa_private_key.pem
$ openssl genrsa -out rsa_private_key.pem 1024
Generating RSA private key, 1024 bit long modulus
...............++++++
....++++++
e is 65537 (0x10001)
//查看私钥文件
$ cat rsa_private_key.pem
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCa59sHPrbUfnwaM08jqtp2UH+dnGG1/ds8EKjk3upptnaIUByN
nAk3Tf4IdD+hQwer4zZW/q9Z4JANIURE6UCpXPwqdsASn0AswlGdy4n9Qou9RFIX
mo5iyi4HlWAaLmsC2q9vCSjT64z/xFu7z48unZeRkVkaxY/hbYlYcoilpwIDAQAB
AoGAU3GH+/wdcpP7+AUhoV38tnUgeV9vByELQuzafgdSkMV4Tm+RIfsmv8Lkj9tH
F3BLJp2LfQtOnASqtFfyNYjMilh/OCCJQTKbM3yiMyiNcmifewaCOXBlxHSZqxNg
oqVZ4+QViDQIka/daBhj8GSb2+d2yXH7+/UsOW5kH6saRnECQQDIbt6POLRAcwcI
J+HF0SJTa4eO6d8mLrTxxcTXsnnoipX2FQXUX6WSG4qhAsDRgVw9cuT3lOZ0gex/
3mzocgN5AkEAxdnRMFwzzDdq5K5O3CUR20PGFDgBcKyJ9Hg+0UnUpLj1/Xy+MnAi
ck7U994GgtWj//RJGreFmtoPggloI/OKHwJAWOx3aWtq7T0bXY70J+OFKyWF2u3/
xtzqAmoPzvKsC3OQ3MCu+W4F8zCs7IpgKZMO7WRQGVfCduY9mu1/vBD5YQJAAn5e
Tpe652YDaOJxiADHmtXd1z9ldUAXdQdW81hU38adAxbhZs1XAc3QjVc1Cc0bhgxU
J4wMmQkKjP8FWv6kzQJBAKbwglA6Pw6q9vfHT3G4X0JaAjk+7Dj7VtkbbnmFJQcG
Kp4grXco/bRAgtlGVMUVjc5rymWNQtrlwjqg/i2hUJ8=
-----END RSA PRIVATE KEY-----
//根据私钥生成公钥文件:rsa_public_key.pem
$ openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
writing RSA key
//查看公钥文件
$ cat rsa_public_key.pem
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCa59sHPrbUfnwaM08jqtp2UH+d
nGG1/ds8EKjk3upptnaIUByNnAk3Tf4IdD+hQwer4zZW/q9Z4JANIURE6UCpXPwq
dsASn0AswlGdy4n9Qou9RFIXmo5iyi4HlWAaLmsC2q9vCSjT64z/xFu7z48unZeR
kVkaxY/hbYlYcoilpwIDAQAB
-----END PUBLIC KEY-----
//随便写点内容到一个文件中
$ echo "hello" > rasdata.txt
//根据公钥加密成文件:hello.en
$ openssl rsautl -encrypt -in rasdata.txt -inkey rsa_public_key.pem -pubin -out hello.en
//根据私钥解密成文件:hello.de
$ openssl rsautl -decrypt -in hello.en -inkey rsa_private_key.pem -out hello.de
//查看解密后的文件
$ cat hello.de
hello