• openssl命令使用


    openssl

    openssl是个密码工具集,提供多端接口调用方式

    组成: 
      1. 代码库 libcryto ,libssl(ssl/tls)
      2. 工具集 openssl
     
    

    对称加密

    对称加密主要是用aes,des算法

    需要注意的是解密不要在源文件操作,否则解密失败源文件也没有了

    usage: enc -ciphername [-AadePp] [-base64] [-bufsize number] [-debug]
        [-in file] [-iv IV] [-K key] [-k password]
        [-kfile file] [-md digest] [-none] [-nopad] [-nosalt]
        [-out file] [-pass arg] [-S salt] [-salt]
    
    -e 指定加密算法
    -d 解密
    -a 使用base64编码
    -base64 使用base64解码
    -in 要加密文件存放位置
    -out 加密后的文件存放位置
    -k 输入密码
    -iv 输入一个向量
    
    加密
     $ openssl enc -e aes-128-cbc -in secret.txt -out myAes128.txt
     
    解密
     $ openssl enc -d aes-128-cbc -in myAes128.txt -out
    
    

    单向散列函数加密

    使用最多的是sha256,sha512,hmac

    md5不再推荐使用,推荐使用sha-2

    单向散列函数特点

    1.输出值的数据长度不变
    2.相同的输入输出也必定相同
    3.输入相似的数据,输出也大不相同
    4.输入完全不同的数据,输出相同的哈希值会以极低的概率出现
    

    单向散列函数的使用

    openssl dgst 
    options are
    -c              to output the digest with separating colons
    -r              to output the digest in coreutils format
    -d              to output debug info
    -hex            output as hex dump
    -binary         output in binary form
    -hmac arg       set the HMAC key to arg
    -verify file    verify a signature using public key in file
    -prverify file  verify a signature using private key in file
    -keyform arg    key file format (PEM or ENGINE)
    -out filename   output to filename rather than stdout
    -signature file signature to verify
    -sigopt nm:v    signature parameter
    -hmac key       create hashed MAC with key
    -mac algorithm  create MAC (not neccessarily HMAC)
    -md4            to use the md4 message digest algorithm
    -md5            to use the md5 message digest algorithm
    -ripemd160      to use the ripemd160 message digest algorithm
    -sha            to use the sha message digest algorithm
    -sha1           to use the sha1 message digest algorithm
    -sha224         to use the sha224 message digest algorithm
    -sha256         to use the sha256 message digest algorithm
    -sha384         to use the sha384 message digest algorithm
    -sha512         to use the sha512 message 
    
    $ cat test.txt | openssl dgst -sha256 -hex -out hash.txt
    
    

    生成随机数

    随机数的作用

    1. 生成密钥: 用于对称密码和消息认证码
    2. 生成密钥对:用于公钥密码和数字签名
    3. 生成初始化向量(IV):用于分组密码的CBC,CFB和OFB模式
    4. 生成nonce, 用于防御重放攻击以及分组密码的CTR模式等
    5. 生成盐,用于基于口令的密码(PBE)等
    

    生成随机数

    openssl rand [option] 字节数
    where options are
    -out file             - write to file
    -engine e             - use engine e, possibly a hardware device.
    -rand file:file:... - seed PRNG from files
    -base64               - base64 encode output
    -hex                  - hex encode output
    
    $ openssl rand -hex 1
    $ openssl rand -base64 10 -out a.txt
    

    生成口令密钥

    一般基于口令的密钥为了防止字典攻击会使用一串随机数加入单向散列函数

    openssl passwd
    Usage: passwd [options] [passwords]
    where options are
    -crypt             standard Unix password algorithm (default)
    -1                 MD5-based password algorithm
    -apr1              MD5-based password algorithm, Apache variant
    -salt string       use provided salt
    -in file           read passwords from file
    -stdin             read passwords from stdin
    -noverify          never verify when reading password from terminal
    -quiet             no warnings
    -table             format output as table
    -reverse           switch table columns
    
    $ openssl passwd -salt 123
    

    生成密钥对

    首先使用genrsa生成私钥,然后在使用rsa从私钥中提取公钥

    openssl genrsa
    usage: genrsa [args] [numbits]
     -des            encrypt the generated key with DES in cbc mode
     -des3           encrypt the generated key with DES in ede cbc mode (168 bit key)
     -idea           encrypt the generated key with IDEA in cbc mode
     -seed
                     encrypt PEM output with cbc seed
     -aes128, -aes192, -aes256
                     encrypt PEM output with cbc aes
     -camellia128, -camellia192, -camellia256
                     encrypt PEM output with cbc camellia
     -out file       output the key to 'file
     -passout arg    output file pass phrase source
     -f4             use F4 (0x10001) for the E value
     -3              use 3 for the E value
     -engine e       use engine e, possibly a hardware device.
     
     $ openssl genrsa -out rsa.key 2048
     //生成公钥
     $ openssl rsa -in rsa.key -pubout -out rsa-public.key
    

    证书

    对证书所发布的公钥进行权威的认证,证书可以有效的避免中间人攻击的问题

    1. PKC:Public-Key Certificate,公钥证书,简称证书。
    2. CA:Certification Authority,认证机构。对证书进行管理,负责 1.生成密钥对、2. 注册公钥时对身份进行认证、3. 颁发证书、4. 作废证书。其中负责注册公钥和身份认证的,称为 RA(Registration Authority 注册机构)
    3. PKI:Public-Key Infrastructure,公钥基础设施,是为了更高效地运用公钥而制定的一系列规范和规格的总称。比较著名的有PKCS(Public-Key Cryptography Standards,公钥密码标准,由 RSA 公司制定)、X.509 等。PKI 是由使用者、认证机构 CA、仓库(保存证书的数据库)组成。
    CRL:Certificate Revocation List 证书作废清单,是 CA 宣布作废的证书一览表,会带有 CA 的数字签名。一般由处理证书的软件更新 CRL 表,并查询证书是否有效。
    4.证书的编码格式:pem,der
    
    

    证书的申请签署步骤

    image

    1. 生成申请请求
    2. RA验证
    3. CA签署
    4. 获取证书
    
    (1)创建所需要的文件
    touch /etc/pki/CA/index.txt 生成证书索引数据库文件
    echo 01 > /etc/pki/CA/serial 指定第一个颁发证书的序列号
    
    (2)CA自签证书
      (1)生成私钥
        cd /etc/pki/CA/
        (umask 066; openssl genrsa -out
       /etc/pki/CA/private/cakey.pem 2048)
      (2)生成自签名证书
        openssl req -new -x509 –key
        /etc/pki/CA/private/cakey.pem -days 7300 -out
        /etc/pki/CA/cacert.pem
        -new: 生成新证书签署请求
        -x509: 专用于CA生成自签证书
        -key: 生成请求时用到的私钥文件
        -days n:证书的有效期限
        -out /PATH/TO/SOMECERTFILE: 证书的保存路径
    
    (3)颁发证书
    A 在需要使用证书的主机生成证书请求
    给web服务器生成私钥
    (umask 066; openssl genrsa -out
    /etc/pki/tls/private/test.key 2048)
    生成证书申请文件
    openssl req -new -key /etc/pki/tls/private/test.key
    -days 365 -out etc/pki/tls/test.csr
    B 将证书请求文件传输给CA
    C CA签署证书,并将证书颁发给请求者
    openssl ca -in /tmp/test.csr –out
    /etc/pki/CA/certs/test.crt -days 365
    注意:默认国家,省,公司名称三项必须和CA一致
    D 查看证书中的信息:
    openssl x509 -in /PATH/FROM/CERT_FILE -noout
    -text|issuer|subject|serial|dates
    openssl ca -status SERIAL 查看指定编号的证书状态
    
    (4) 吊销证书
    
    A 在客户端获取要吊销的证书的serial
    openssl x509 -in /PATH/FROM/CERT_FILE -noout
    -serial -subject
    B 在CA上,根据客户提交的serial与subject信息,对比检验是
    否与index.txt文件中的信息一致,吊销证书:
    openssl ca -revoke /etc/pki/CA/newcerts/SERIAL.pem
    C 指定第一个吊销证书的编号
    注意:第一次更新证书吊销列表前,才需要执行
    echo 01 > /etc/pki/CA/crlnumber
    D 更新证书吊销列表
    openssl ca -gencrl -out /etc/pki/CA/crl/crl.pem
    查看crl文件:
    openssl crl -in /etc/pki/CA/crl/crl.pem -noout -text
    
    
    
  • 相关阅读:
    109 01 Android 零基础入门 02 Java面向对象 03 综合案例(学生信息管理) 03 新增功能及实现 05 问题解析--通过一个方法完成学生和专业的双向关联
    108 01 Android 零基础入门 02 Java面向对象 03 综合案例(学生信息管理) 03 新增功能及实现 04 问题解析--数组未实例化造成的空指针异常
    107 01 Android 零基础入门 02 Java面向对象 03 综合案例(学生信息管理) 03 新增功能及实现 03 编写方法完成学生个数统计功能
    106 01 Android 零基础入门 02 Java面向对象 03 综合案例(学生信息管理) 03 新增功能及实现 02 新增属性完成学生信息存储
    105 01 Android 零基础入门 02 Java面向对象 03 综合案例(学生信息管理) 03 新增功能及实现 01 新增需求及分析
    session与cookie的区别和联系
    session和cookie的区别
    Web服务器主动推送技术
    webSocket的场景应用
    TCP、Http和Socket 优劣比较
  • 原文地址:https://www.cnblogs.com/pluslius/p/9936327.html
Copyright © 2020-2023  润新知