本文介绍 openssl 用法,生成 rsa 的公私钥。
代码如下:
# 1. 生成私钥
openssl genrsa -out private.pem
# 2. 基于私钥生成公钥
openssl rsa -in private.pem -pubout -out public.pem
# 3. 查看 ASN.1 格式的私钥信息
openssl asn1parse -i -in private.pem
# 4. 查看 ASN.1 格式的公钥信息
openssl asn1parse -i -in public.pem
# 标注:第 4 步会看到 BIT STRING,其中前面的数字表示偏移量,假设是 19
# 4-1. 进一步解析
openssl asn1parse -i -in public.pem -strparse 19
# 5. 对文件 hello.txt 加密
openssl rsautl -encrypt -in hello.txt -inkey public.pem -pubin -out hello.en
# 6. 对加密文件 hello.en 使用私钥解密
openssl rsautl -decrypt -in hello.en -inkey private.pem -out hello.de
至此,结束。