文章目录
- 生成RSA密钥对
- 验证RSA密钥对
生成RSA密钥对
- 正常RSA密钥对生成操作命令
openssl genrsa -out rsa_private_key.pem 2048
openssl rsa -pubout -in rsa_private_key.pem -out rsa_public_key.pem
- 通过添加环境变量来简化RSA密钥对生成操作
设置环境变量.zshrc或.bash_profile
# 使用zsh终端
vim ~/.zshrc
- 将下面shell脚本复制到.zshrc文件中
function myrsa() {if [ -z $1 ]; thenecho "请输入密钥名称"returnfi# 512位、1024位、2048位、3072位、4096位len=2048if [ -n "$2" ]; thenlen=$2fiprivateKey=$1"_rsa_private_key.pem"publicKey=$1"_rsa_public_key.pem"openssl genrsa -out $privateKey $lenopenssl rsa -pubout -in $privateKey -out $publicKeyecho $(pwd)echo $privateKeyecho $publicKey
}
source ~/.zshrc
- 执行简化后的生成命令
# 终端输入生成RSA密钥对命令
myrsa test
- 生成的RSA密钥
test_rsa_private_key.pem
test_rsa_public_key.pem
验证RSA密钥对
# 利用私钥生成 .pub 公钥
openssl rsa -pubout -in test_rsa_private_key.pem -out test_rsa_public_key.pub
# 校验文件内容是否一致
diff test_rsa_public_key.pub test_rsa_public_key.pem
# 使用zsh终端
vim ~/.zshrc
- 将下面shell脚本复制到.zshrc文件中
function myrsadiff() {if [ -z $1 ]; thenecho "请输入RSA私钥文件名称"returnfiif [ -z $2 ]; thenecho "请输入RSA公钥文件名称"returnfiprivateKey=$1publicKey=$2publicKeyPub="${publicKey//.pem/.pub}"openssl rsa -pubout -in $privateKey -out $publicKeyPubif [ -z $(diff $publicKey $publicKeyPub) ]; thenecho "RSA密钥对匹配"fi
}
myrsadiff test_rsa_private_key.pem test_rsa_public_key.pem
注意 test_rsa_public_key.pem 文件中不要有多余空格,否则 diff 命令可能不准