设为首页 收藏本站
查看: 1282|回复: 0

[经验分享] Linux openssl 实现

[复制链接]

尚未签到

发表于 2018-5-20 15:25:31 | 显示全部楼层 |阅读模式
  openssl是ssl的开源实现,至于ssl或tls就不在这讲述了。总之openssl就是一软件,实现加密数据、解密数据、创建私有CA。官网:www.openssl.org。
  openssl主要构成:
  libcrypro:加密库
  libssl:tls/ssl(库)的实现(基于会话,实现身份认证、数据完成性和机密性)
  openssl:命令工具,可实现私有证书颁发
  例子解释
  #查看本系统openssl的安装
  [root@localhost ~]# rpm -qa openssl
  openssl-0.9.8e-22.el5
  openssl-0.9.8e-22.el5
  #openssl安装的文件路径,太多就不显示了
  [root@localhost ~]# rpm -ql openssl | more
  

  [root@localhost ~]# vim testssl.txt
  12345678910
  ~
  ~
  ~
  #执行下条命令时,输入加密密码(本次采用对称加密des3算法,也就是三重des)
  [root@localhost ~]# openssl enc -des3 -salt -a -in testssl.txt -out testssl_des3
  enter des-ede3-cbc encryption password:
  Verifying - enter des-ede3-cbc encryption password:
  [root@localhost ~]# vim testssl_des3
  U2FsdGVkX18Ac3RL0TNIX/2lCQEGy959/u2/A4SCNt8=
  ~
  ~
  #执行下条命令时,输入解密密码
  [root@localhost ~]# openssl enc -des3 -d -salt -a -in testssl_des3 -out testssl.txt1
  enter des-ede3-cbc decryption password:
  #比较原文件和解密后的文件
  [root@localhost ~]# diff testssl.txt testssl.txt1
  

  #几种单向加密的输出
  [root@localhost ~]# sha1sum  testssl.txt
  bb9670e73957e64e15d8411e2e424d84b8599e20  testssl.txt
  [root@localhost ~]# openssl dgst -sha1 testssl.txt
  SHA1(testssl.txt)= bb9670e73957e64e15d8411e2e424d84b8599e20
  [root@localhost ~]# openssl dgst -md5 testssl.txt
  MD5(testssl.txt)= 8140de300ce2cf5032f9489bd39afed0
  [root@localhost ~]# openssl passwd -1 testssl.txt
  $1$Ml2abKeF$Kx6uZL.1RTBshxFa.CT3I0
  

  #openssl实现私有CA
  # 1. 先生成一对密钥
  # 2. 生成自签署证书
  [root@localhost ~]# cd /etc/pki/CA
  [root@localhost CA]# openssl genrsa -out private/cakey.pem 2048
  Generating RSA private key, 2048 bit long modulus
  ......................................................................................................................+++
  ..+++
  e is 65537 (0x10001)
  [root@localhost CA]# ls  -l private/
  total 8
  -rw-r--r-- 1 root root 1675 Apr 15 19:55 cakey.pem
  [root@localhost CA]# chmod 600 private/cakey.pem
  [root@localhost CA]# ls  -l private/
  total 8
  -rw------- 1 root root 1675 Apr 15 19:55 cakey.pem
  [root@localhost CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem
  You are about to be asked to enter information that will be incorporated
  into your certificate request.
  What you are about to enter is what is called a Distinguished Name or a DN.
  There are quite a few fields but you can leave some blank
  For some fields there will be a default value,
  If you enter '.', the field will be left blank.
  -----
  Country Name (2 letter code) [GB]:CN
  State or Province Name (full name) [Berkshire]:JS
  Locality Name (eg, city) [Newbury]:NJ
  Organization Name (eg, company) [My Company Ltd]:bocs
  Organizational Unit Name (eg, section) []:Tech
  Common Name (eg, your name or your server's hostname) []:ca.bocs.com
  Email Address []:ca@bocs.com
  [root@localhost CA]# openssl x509 -text -in cacert.pem
  [root@localhost CA]# mkdir certs newcerts crl
  [root@localhost CA]# touch index.txt
  [root@localhost CA]# touch serial
  [root@localhost CA]# echo 01 > serial
  

  #假设现有一台web要签证
  [root@localhost ~]# mkdir /tmp/httpd
  [root@localhost ~]# cd /tmp/httpd/
  [root@localhost httpd]# mkdir ssl
  [root@localhost httpd]# cd ssl
  #生成私钥
  [root@localhost ssl]# (umask 077; openssl genrsa -out httpd.key 1024)
  Generating RSA private key, 1024 bit long modulus
  .................++++++
  ..........................................................++++++
  e is 65537 (0x10001)
  #生成证书申请请求
  [root@localhost ssl]# openssl req -new -key httpd.key -out httpd.csr
  You are about to be asked to enter information that will be incorporated
  into your certificate request.
  What you are about to enter is what is called a Distinguished Name or a DN.
  There are quite a few fields but you can leave some blank
  For some fields there will be a default value,
  If you enter '.', the field will be left blank.
  -----
  Country Name (2 letter code) [GB]:CN
  State or Province Name (full name) [Berkshire]:JS
  Locality Name (eg, city) [Newbury]:NJ
  Organization Name (eg, company) [My Company Ltd]:bocs
  Organizational Unit Name (eg, section) []:Tech
  Common Name (eg, your name or your server's hostname) []:www.bocs.com
  Email Address []:www@bocs.com
  

  Please enter the following 'extra' attributes
  to be sent with your certificate request
  A challenge password []:
  An optional company name []:
  

  #CA服务器签证,执行命令时,先检查下/etc/pki/tls/openss.cnf  确认:dir  = /etc/pki/CA
  [root@localhost ssl]# openssl ca -in httpd.csr -out httpd.crt -days 365
  Using configuration from /etc/pki/tls/openssl.cnf
  Check that the request matches the signature
  Signature ok
  Certificate Details:
  Serial Number: 1 (0x1)
  Validity
  Not Before: Apr 15 12:23:06 2016 GMT
  Not After : Apr 15 12:23:06 2017 GMT
  Subject:
  countryName               = CN
  stateOrProvinceName       = JS
  organizationName          = bocs
  organizationalUnitName    = Tech
  commonName                = www.bocs.com
  emailAddress              = www@bocs.com
  X509v3 extensions:
  X509v3 Basic Constraints:
  CA:FALSE
  Netscape Comment:
  OpenSSL Generated Certificate
  X509v3 Subject Key Identifier:
  E9:FE:1D:40:A2:42:A5:C1:80:A2:CD:94:3F:B0:D4:AB:92:45:7D:30
  X509v3 Authority Key Identifier:
  keyid:B2:0B:0D:C7:11:34:EF:B7:E3:AF:F7:75:D6:82:73:12:FA:A0:55:62
  

  Certificate is to be certified until Apr 15 12:23:06 2017 GMT (365 days)
  Sign the certificate? [y/n]:y
  

  

  1 out of 1 certificate requests certified, commit? [y/n]y
  Write out database with 1 new entries
  Data Base Updated
  [root@localhost ssl]#
  

  #查看是否签证成功
  [root@localhost CA]# cd /etc/pki/CA
  [root@localhost CA]# cat index.txt
  V170415122306Z01unknown/C=CN/ST=JS/O=bocs/OU=Tech/CN=www.bocs.com/emailAddress=www@bocs.com
  [root@localhost CA]# cat serial
  02
  [root@localhost CA]#
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-467561-1-1.html 上篇帖子: Linux权限设置 下篇帖子: Linux inotify
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表