愿你出走半生,归来仍是少年

Kratos
专注于用户阅读体验的响应式博客主题
  1. 首页
  2. linux
  3. 正文

openssl加解密实战

2022年1月6日 3153点热度 0人点赞 0条评论

Table of Contents

Toggle
  • 一、概念
    • 1、加密有两种类型
    • 2、公钥加密使用两组密钥,称为密钥对
  • 二、公钥加密/非对称加密
    • 1、生成私钥
    • 2、查看私钥
    • 3、提取公钥
    • 4、查看公钥
    • 5、使用公钥加密文件
    • 6、使用私钥解密文件
  • 三、密钥加密/对称加密
    • 1、加密
    • 2、解密
    • 3、批量文件压缩、加解密操作
    • 4、批量文件压缩、加解密操作:【其他参考】
  • 四、总结

一、概念

1、加密有两种类型

密钥加密(secret-key encryption)使用相同的密钥进行加密和解密,也叫对称加密
公钥加密(public-key encryption)使用不同的密钥进行加密和解密,也叫非对称加密
密钥加密速度更快,公钥加密更加安全,因为它解决了安全共享密钥的问题

2、公钥加密使用两组密钥,称为密钥对

一个是公钥,用于加密,可以与你想要秘密通信的任何人自由共享、交换;
一个是私钥,用于解密,应该是一个秘密,永远不会共享。
如你想与别人交流敏感文件,可将你的公钥发送给他,他使用公钥加密文件,然后将加密后的文件其发送给你。你解密发件人的加密文件的唯一方法,是使用私钥进行解密。

二、公钥加密/非对称加密

1、生成私钥

#定义私钥文件的名字
export PRIVATE_KEY=liking_private
#定义公钥文件的名字
export PUBLIC_KEY=liking_public
#生成私钥
openssl genrsa -aes128 -out ${PRIVATE_KEY}.pem 1024

2、查看私钥

openssl rsa -in ${PRIVATE_KEY}.pem -noout -text
hexdump -C ./${PRIVATE_KEY}.pem

3、提取公钥

openssl rsa -in ${PRIVATE_KEY}.pem -pubout > ${PUBLIC_KEY}.pem

4、查看公钥

openssl rsa -in ${PUBLIC_KEY}.pem -pubin -text -noout
hexdump -C ./${PUBLIC_KEY}.pem

5、使用公钥加密文件

以下脚本可以对多个文件进行加密:

vim liking_enc.sh
#!/bin/bash
export PRIVATE_KEY=liking_private
export PUBLIC_KEY=liking_public
export FILES="oratopjk oratopts oratopwy oratopzz sqlplusjk sqlplusts sqlpluswy sqlpluszz"
for file in ${FILES}
do
  openssl rsautl -encrypt -inkey ${PUBLIC_KEY}.pem -pubin -in ${file} -out ${file}.enc
  rm -rf ${file}
done

注1:-inkey指定密钥,默认是私钥,再加-pubin表示指定公钥
注2:rsautl只能对短小的文件进行操作,否则将报"data too large for key size"
hexdump -C ./${file}.enc

6、使用私钥解密文件

以下脚本可以对多个文件进行解密:

vim liking_dec.sh
#!/bin/bash
export PRIVATE_KEY=liking_private
export PUBLIC_KEY=liking_public
export FILES="oratopjk oratopts oratopwy oratopzz sqlplusjk sqlplusts sqlpluswy sqlpluszz"
for file in ${FILES}
do
  openssl rsautl -decrypt -inkey ${PRIVATE_KEY}.pem -in ${file}.enc -out ${file}
  rm ${file}.enc
done
chmod +x ${FILES}

三、密钥加密/对称加密

1、加密

openssl enc -e -aes-128-ecb -in ${FILE_NAME} -out ${FILE_NAME}.enc -pass pass:passw0rd -p

2、解密

openssl enc -d -aes-128-ecb -in ${FILE_NAME}.enc -out ${FILE_NAME} -pass pass:passw0rd -p

3、批量文件压缩、加解密操作

#压缩并加密一组文件如x、y,生成文件xy
tar cvfz - x y | openssl enc -e -aes-128-ecb -out xy -pass pass:passw0rd
#解压缩并解密文件xy,恢复为源文件x、y
openssl enc -d -aes-128-ecb -in xy -pass pass:passw0rd | tar xvfz -

4、批量文件压缩、加解密操作:【其他参考】

tar cvfz - x y | openssl des3 -salt -k passw0rd -out xy
openssl des3 -d -k passw0rd -salt -in xy | tar xvfz -

四、总结

以上可以看出,在工作中选择加密方式的策略如下:
1、如果追求安全、无痕,则采用非对称加密,自始至终没有明文密码痕迹,但稍显复杂,且以上方法只能加密小文件。
2、如果追求简单、实用,则采用对称加密,一个命令即可实现,但是命令行历史会暴露密码明文。

标签: 暂无
最后更新:2022年1月6日

liking

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

归档

  • 2026 年 6 月
  • 2026 年 3 月
  • 2026 年 2 月
  • 2025 年 12 月
  • 2025 年 11 月
  • 2025 年 4 月
  • 2025 年 2 月
  • 2025 年 1 月
  • 2024 年 12 月
  • 2024 年 10 月
  • 2024 年 7 月
  • 2024 年 5 月
  • 2024 年 4 月
  • 2023 年 11 月
  • 2023 年 10 月
  • 2023 年 8 月
  • 2023 年 7 月
  • 2023 年 5 月
  • 2023 年 3 月
  • 2023 年 2 月
  • 2022 年 12 月
  • 2022 年 11 月
  • 2022 年 10 月
  • 2022 年 9 月
  • 2022 年 8 月
  • 2022 年 6 月
  • 2022 年 5 月
  • 2022 年 3 月
  • 2022 年 2 月
  • 2022 年 1 月
  • 2021 年 12 月
  • 2021 年 11 月
  • 2021 年 10 月
  • 2021 年 8 月
  • 2021 年 7 月
  • 2021 年 6 月
  • 2021 年 5 月
  • 2021 年 4 月
  • 2021 年 3 月
  • 2021 年 2 月
  • 2021 年 1 月
  • 2020 年 12 月
  • 2020 年 11 月
  • 2020 年 10 月
  • 2020 年 9 月
  • 2020 年 8 月
  • 2020 年 7 月
  • 2020 年 6 月
  • 2020 年 5 月
  • 2020 年 3 月
  • 2020 年 2 月
  • 2019 年 12 月
  • 2019 年 11 月
  • 2019 年 10 月
  • 2019 年 9 月
  • 2019 年 8 月
  • 2019 年 7 月
  • 2019 年 6 月
  • 2019 年 5 月
  • 2019 年 4 月
  • 2019 年 2 月
  • 2018 年 12 月
  • 2018 年 11 月
  • 2018 年 8 月
  • 2018 年 7 月
  • 2018 年 6 月
  • 2018 年 5 月
  • 2018 年 4 月
  • 2018 年 3 月
  • 2018 年 2 月
  • 2018 年 1 月
  • 2017 年 12 月
  • 2017 年 11 月
  • 2017 年 8 月
  • 2017 年 6 月
  • 2017 年 5 月
  • 2017 年 3 月
  • 2016 年 9 月
  • 2016 年 8 月
  • 2016 年 7 月
  • 2016 年 6 月
  • 2016 年 5 月
  • 2015 年 10 月
  • 2014 年 10 月
  • 2014 年 8 月
  • 2014 年 3 月
  • 2013 年 12 月
  • 2013 年 8 月
  • 2013 年 7 月
  • 2013 年 6 月
  • 2013 年 4 月
  • 2012 年 11 月
  • 2012 年 7 月
  • 2012 年 6 月
  • 2012 年 5 月
  • 2012 年 2 月
  • 2011 年 10 月
  • 2011 年 8 月
  • 2011 年 3 月
  • 2010 年 11 月
  • 2010 年 10 月
  • 2010 年 9 月
  • 2010 年 8 月
  • 2010 年 7 月
  • 2010 年 5 月
  • 2010 年 4 月
  • 2010 年 1 月
  • 2009 年 11 月
  • 2009 年 8 月
  • 2009 年 5 月
  • 2009 年 3 月
  • 2009 年 2 月
  • 2008 年 11 月
  • 2008 年 10 月
  • 2008 年 8 月
  • 2008 年 4 月
  • 2007 年 4 月

分类

  • BigData
  • kubernetes
  • linux
  • MongoDB
  • MySQL
  • Oracle
  • PostgreSQL
  • 备份恢复
  • 安装配置
  • 性能优化
  • 技术杂谈
  • 未分类
  • 梦回红楼
  • 测试
  • 生活点滴
  • 网站建设
  • 自制工具
  • 轻松一刻
  • 运维知识
  • 问题处理

COPYRIGHT © 2026 愿你出走半生,归来仍是少年. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang