使用Openssl保管密码

这是一个关于如何使用OpenSSL命令行工具进行文件加密和解密的批处理脚本说明。通过调用openssl,利用base64和des3算法进行操作。用户可以选择加密或解密文件,并指定密码和输出文件名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


信息时代,密码会很多, 有些很少用,过些时间就会忘掉。 是件比较痛苦的事情
于是想到一个方法,用于集中管理密码,只要记住一个就可以了,
 
加密方法如下:
1、建立一个passworddb.txt,将需要加密的密码保存到里面 (实际使用的时候不用起这么明显的名字-_-)
2、使用base64进行编码
3、对第二步的输出进行triple-des加密
4、对第三步的输出进行base64编码,输出passworddb.txt.bdb
 
解密就是逆过程,方法如下:
1、对加密文件passworddb.txt.bdb进行base64解码
2、对第一步的输出进行triple-des解密
3、对第二步的输出进行base64解码,输出passworddb.txt
 
 
加密的东东,当然使用openssl了,很好很强大。 但是要是一个个的敲入命令, 不是很方便,于是我写了一个批处理,用于加解密。 哈哈, 这下可以将加密之后的密码文件放到邮箱了, 只要记住加密这个文件的秘钥即可, 以前的密码记不得的时解开看一下, 很方便!
 
批处理文件(ed.bat)代码如下:
  1. @rem /* decrypt file with openssl. */
  2. @echo off
  3. @rem /* check parameters */
  4. @setlocal EnableDelayedExpansion
  5. if {%1}== {}  goto help
  6. if {%2}== {}  goto help
  7. if {%3}== {}  goto help
  8. set ENCKEY=%3
  9. set FILEINPUT=%2
  10. @rem /* calculate output file name. */
  11. @rem /* if output file name is specified by the command parameter then it will be used. */
  12. @rem /* otherwise to append/remove .bdb extension to/from the input file name */
  13. set FILEOUTPUT=%2
  14. if not {%4} == {} (set FILEOUTPUT=%4) else (
  15.     set EXT=%FILEOUTPUT:~-4%
  16.     if "!EXT!"==".bdb" (
  17.         set FILEOUTPUT=%FILEOUTPUT:.bdb=%
  18.     ) else (
  19.         set FILEOUTPUT=%FILEOUTPUT%.bdb
  20.     )
  21. )
  22. set FILE1=%FILEOUTPUT%.b
  23. set FILE2=%FILEOUTPUT%.bd
  24. @rem /* Find weather specified file exsits! */
  25. if exist %FILEOUTPUT% (
  26.     echo "%FILEOUTPUT% Exists. overwrite it? (y/n)"
  27.     set /P Query=
  28.     if "!Query!" == "n" goto end
  29. )
  30. if exist %FILE1% (
  31.     echo "%FILE1% Exists. overwrite it? (y/n)"
  32.     set /P Query=
  33.     if "!Query!" == "n" goto end
  34. )
  35. if exist %FILE2% (
  36.     echo "%FILE2% Exists. overwrite it? (y/n)"
  37.     set /P Query=
  38.     if "!Query!" == "n" goto end
  39. )
  40. if %1 == enc goto encrypt-base64-des3-base64
  41. if %1 == dec goto decrypt-base64-des3-base64
  42. goto help
  43. @rem /* encrypt file with base64 des3 base64 algorithm: %FILEINPUT%->%FILE1%->%FILE2%->%FILEOUTPUT% */
  44. :encrypt-base64-des3-base64
  45. call openssl base64 -in %FILEINPUT% -out %FILE1%
  46. if errorlevel 1 goto error
  47. call openssl des3 -salt -k %ENCKEY% -in %FILE1% -out %FILE2%
  48. if errorlevel 1 goto error
  49. call openssl base64 -in %FILE2% -out %FILEOUTPUT%
  50. if errorlevel 1 goto error
  51. echo File encrypted to %FILEOUTPUT% successfully!
  52. goto end
  53. @rem /* decrypt file with base64 des3 base64 algorithm: %FILEINPUT%->%FILE1%->%FILE2%->%FILEOUTPUT% */
  54. :decrypt-base64-des3-base64
  55. call openssl base64 -d -in %FILEINPUT% -out %FILE1%
  56. if errorlevel 1 goto error
  57. call openssl des3 -d -salt -k %ENCKEY% -in %FILE1% -out %FILE2%
  58. if errorlevel 1 goto error
  59. call openssl base64 -d -in %FILE2% -out %FILEOUTPUT%
  60. if errorlevel 1 goto error
  61. echo File decrypted to %FILEOUTPUT% successfully!
  62. goto end
  63. :error
  64. echo error!
  65. goto :end
  66. :help
  67. echo decrypt file: dec file password [outfile]
  68. echo encrypt file: enc file password [outfile]
  69. :end
  70. if exist %FILE1% del %FILE1%
  71. if exist %FILE2% del %FILE2%


使用方法:
加密 passworddb.txt,密码是123456 (方法1默认将加密文件保存为passworddb.txt.bdb,方法二明确指定加密后文件保存为password.enc)
1、ed enc passworddb.txt 123456
2、ed enc passworddb.txt 123456 password.enc

解密(方法1解密后,保存为passworddb.txt,方法二明确指定解密后保存为password.txt)
1、 ed dec passworddb.txt.bdb 123456
2、 ed dec passworddb.txt.bdb 123456 password.txt
 

 

注: 本想将openssl相关程序和批处理文件上传上来,但是csdn不支持-_-!

openssl可以到www.openssl.org去下载源码编译,如有需要也可以给我留言, 留下邮箱, 我会发一份给你。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值