• openssl rsautl和openssl pkeyutl(文件的非对称加密)


    rsautl是rsa的工具,相当于rsa、dgst的部分功能集合,可用于生成数字签名、验证数字签名、加密和解密文件

    pkeyutl是非对称加密的通用工具,大体上和rsautl的用法差不多,所以此处只解释rsautl。

    openssl rsautl [-in file] [-out file] [-inkey file] [-pubin] [-certin] [-passin arg] [-sign] [-verify] [-encrypt] [-decrypt] [-hexdump]
    
    openssl pkeyutl [-in file] [-out file] [-sigfile file] [-inkey file] [-passin arg] [-pubin] [-certin] [-sign] [-verify] [-encrypt] [-decrypt] [-hexdump]
    
    共同的选项说明:
    -in file:指定输入文件
    -out file:指定输出文件
    -inkey file:指定密钥输入文件,默认是私钥文件,指定了"-pubin"则表示为公钥文件,使用"-certin"则表示为包含公钥的证书文件
    -pubin:指定"-inkey file"的file是公钥文件
    -certin:使用该选项时,表示"-inkey file"的file是包含公钥的证书文件
    -passin arg:传递解密密码。若验证签名时实用的公钥或私钥文件是被加密过的,则需要传递密码来解密。密码的格式见"openssl 密码格式"
    【功能选项:】
    -sign:签名并输出签名结果,注意,该选项需要提供RSA私钥文件
    -verify:使用验证签名文件
    -encrypt:使用公钥加密文件
    -decrypt:使用私钥解密文件
    【输出格式选项:】
    -hexdump:以hex方式输出
    
     
    openssl pkeyutl选项说明:
    sigfile file:待验证的签名文件

    rsautl命令的用法和rsa、dgst不太一样。首先,它的前提是已经有非对称密钥,所有的命令操作都用到公钥或私钥来处理;再者,该命令使用-in选项来指定输入文件,而不像dgst一样可以把输入文件放在命令的结尾;最后,该命令使用的密钥文件、签名文件、证书文件都通过-inkey选项指定,再通过各功能的选项搭配来实现对应的功能。

    注意rsautl和pkeyutl的缺陷是默认只能对短小的文件进行操作,否则将报类似如下的错误信息。

    140341340976968:error:0406C06E:rsa routines:RSA_padding_add_PKCS1_type_1:data too large for key size:rsa_pk1.c:73:

    因为这两个工具签名和验证签名的功能和openssl dgst命令差不多,且自身又有缺陷,所以就不举例说明。此处仅给出对短小文件的非对称加密和解密示例。

    (1).使用公钥加密b.txt文件,注意待加密文件b.txt必须是短小文件,且不建议使用-hexdump输出,否则解密时可能超出文件的长度。

    [root@docker-01 ~]# openssl genrsa -out genrsa.pri# 生成私钥
    Generating RSA private key, 2048 bit long modulus ....+++ .+++ e is 65537 (0x10001)
    [root@docker-01 ~]# openssl rsa -in genrsa.pri -pubout -out rsa.pub   # 从私钥中提取公钥
    writing RSA key [root@docker-01 ~]# openssl rsautl -encrypt -in b.txt -out b_crypt.txt -inkey rsa.pub -pubin

    查看非对称加密后的文件b_crypt.txt。

    [root@docker-01 ~]# cat b_crypt.txt
    HO:d3|H>8(_M
    +|Ϳ#OO;)ot@docker-01 ~]# 7t(Ħ~pS4u_$z'

    (2).使用私钥解密b_crypt.txt文件。

    [root@docker-01 ~]# openssl rsautl -decrypt -in b_crypt.txt -out b_decrypt.txt -inkey genrsa.pri
    [root@docker-01 ~]# cat b_decrypt.txt
    abc

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    洛谷P4016 负载平衡问题 费用流
    Reactor Cooling ZOJ
    Acme Corporation UVA
    洛谷P4014 分配问题 费用流
    洛谷P4013 数字梯形问题 费用流
    洛谷P4012 深海机器人问题 费用流
    力扣题目汇总(旋转数字,移除元素,找不同)
    力扣题目汇总(两数之和Ⅱ-输入有序数组,删除排序数组中的重复项,验证回文串)
    逻辑练练手小程序
    爬虫之Scarpy.Request
  • 原文地址:https://www.cnblogs.com/liujunjun/p/12398719.html
Copyright © 2020-2023  润新知