使用openssl 的命令行进行文件的加密与解密过程,主要有两种方式:

  1. openssl 指定加密/解密算法加密
  2. openssl 指定公钥/私钥文件加密

openssl 指定加密/解密算法加密

To Encrypt:

openssl enc -e -aes-256-cbc -in un_encrypted.data -out encrypted.data
  • 1

To Decrypt:

openssl enc -d -aes-256-cbc -in encrypted.data -out un_encrypted.data
  • 1

Note: 1. You will be prompted for a password when encrypting or decrypt.

openssl 指定公钥/私钥文件加密

The following commands are relevant when you work with RSA keys:

  • openssl genrsa: Generates an RSA private keys.
  • openssl rsa: Manage RSA private keys (includes generating a public key from it).
  • openssl rsautl: Encrypt and decrypt files with RSA keys.

Get the public key
Let the other party send you a certificate or their public key. If they send to a certificate you can extract the public key using this command:

openssl rsa -in certificate.pem -out publickey.pem -outform PEM -pubout
  • 1

Generate the random password file
Use the following command to generate the random key:

openssl rand -base64 128 -out key.bin
  • 1

Do this every time you encrypt a file. Use a new key every time!

Encrypt the file with the random key
Use the following command to encrypt the large file with the random key:

openssl enc -aes-256-cbc -salt -in largefile.pdf -out largefile.pdf.enc -pass file:./bin.key
  • 1

The file size doesn’t grows that much:

 $ ls -larth
-rw-r--r-- 1 user group 40M Nov 9 21:14 Linux-Voice-Issue-020.pdf
-rw-r--r-- 1 user group 40M Nov 9 22:03 Linux-Voice-Issue-020.pdf.enc
  • 1
  • 2
  • 3

It’s encrypted however:

$ file Linux-Voice-Issue-020.pdf
Linux-Voice-Issue-020.pdf: PDF document, version 1.4
  • 1
  • 2
$ file Linux-Voice-Issue-020.pdf.enc
Linux-Voice-Issue-020.pdf.enc: data
  • 1
  • 2

Encrypt the random key with the public keyfile
Use the following command to encrypt the random keyfile with the other persons public key:

openssl rsautl -encrypt -inkey publickey.pem -pubin -in key.bin -out key.bin.enc
  • 1

You can safely send the key.bin.enc and the largefile.pdf.enc to the other party.

You might want to sign the two files with your public key as well.

Decrypt the random key with our private key file
If you want to decrypt a file encrypted with this setup, use the following command with your privte key (beloning to the pubkey the random key was crypted to) to decrypt the random key:

openssl rsautl -decrypt -inkey privatekey.pem -in key.bin.enc -out key.bin
  • 1

This will result in the decrypted random key we encrypted the file in.

Decrypt the large file with the random key
Once you have the random key, you can decrypt the encrypted file with the decrypted key:

openssl enc -d -aes-256-cbc -in largefile.pdf.enc -out largefile.pdf -pass file:./bin.key
  • 1

This will result in the decrypted large file.

openssl 程序实现公钥/私钥加解密

生成私钥:
openssl genrsa -out pri_test.key 2048

生成公钥:
openssl rsa -in pri_test.key -pubout > pub_test.key

Run the following command to retrieve your SSH RSA fingerprint (-l means "list" instead of create a new key, -f means "filename"):

$ ssh-keygen -lf /path/to/ssh/key

So for example, on my machine the command I ran was:

$ ssh-keygen -lf ~/.ssh/id_rsa.pub

Concrete example (if you use an RSA public key):

$ ssh-keygen -lf ~/.ssh/id_rsa.pub
2048 00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff /Users/username/.ssh/id_rsa.pub (RSA)

With newer versions of ssh-keygen, run ssh-keygen -E md5 -lf <fileName> if you want the same format as old (thanks Lloyd Dewolf)

Openssl 加解密文件的更多相关文章

  1. (8) openssl rsautl(签名/验证签名/加解密文件)和openssl pkeyutl(文件的非对称加密)

    rsautl是rsa的工具,相当于rsa.dgst的部分功能集合,可用于生成数字签名.验证数字签名.加密和解密文件. pkeyutl是非对称加密的通用工具,大体上和rsautl的用法差不多,所以此处只 ...

  2. curses-键盘编码-openssl加解密【转】

    本文转载自;https://zhuanlan.zhihu.com/p/26164115 1.1 键盘编码 按键过程:当用户按下某个键时, 1.键盘会检测到这个动作,并通过键盘控制器把扫描码(scan ...

  3. Java DES 加解密文件

    import com.mchange.v2.io.DirectoryDescentUtils; import javax.crypto.Cipher;import javax.crypto.Ciphe ...

  4. OpenSSL加解密

    http://www.caole.net/diary/des.html Table of Contents OpenSSL - DES Summary DES使用的例子 另一个带注释的例子 另一段Co ...

  5. vim 命令加解密文件

    加密文件 vim file :X  (大写X,是加密 ,小写x是保存) 输入密码: 确认密码: 解除密码: vim file :set key= :wq 命令模式下,输入 /word 后回车,即查找w ...

  6. php OpenSSL 加解密

    2018-1-6 17:10:19 星期六 $data = '123456'; $openssl_method = 'AES-256-CBC'; $openssl_iv_length = openss ...

  7. openssl在多平台和多语言之间进行RSA加解密注意事项

    首先说一下平台和语言: 系统平台为CentOS6.3,RSA加解密时使用NOPADDING进行填充 1)使用C/C++调用系统自带的openssl 2)Android4.2模拟器,第三方openssl ...

  8. 用mp3stego来加密与解密文件的几次尝试

    用法来自实验吧的"Canon"隐写题目的灵感. 先来简单的聊一下这道题目,打开题目后发现了一个mp3文件,除此之外还有一枚压缩包.然而压缩包是加密的,看来我们需要通过解出来mp3里 ...

  9. PHP加密解密方法,使用openssl加密解密

    /** * des 加密算法 */ function do_mencrypt($input, $key) { if (!function_exists("mcrypt_module_open ...

随机推荐

  1. [Luogu] 外星密码

    https://www.luogu.org/problemnew/show/P1928 沙比提 读清题目 #include <bits/stdc++.h> using namespace ...

  2. Bzoj 3333 高级打字机(主席树)

    3333 高级打字机 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题目描述 Description 早苗入手了最新的高级打字机.最新款自然有着与以往不同的功能 ...

  3. P2543 [AHOI2004]奇怪的字符串

    题目描述 输入输出格式 输入格式: 输入文件中包含两个字符串X和Y.当中两字符串非0即1.序列长度均小于9999. 输出格式: X和Y的最长公共子序列长度. 输入输出样例 输入样例#1: 复制 010 ...

  4. SLAM第一篇:基础知识

    无论在室内.野外.空中还是水下,SLAM是机器人进入未知环境遇到的第一个问题.本期给大家介绍SLAM的基础知识:传感器与视觉SLAM框架 近来年,智能机器人技术在世界范围内得到了大力发展.人们致力于把 ...

  5. org.springframework.expression.spel.SpelEvaluationException: EL1030E

    问题与分析 在本地开发项目时发现报错如下: org.springframework.expression.spel.SpelEvaluationException: EL1030E: The oper ...

  6. 在开发iOS程序时对日期处理的总结

    小贴士(Tips)-iOS 由于iOS的设备对应多国语言,用户也可以选择不同的日历模式.比如日本的和历,泰国日历等等. 用户也可以自行设定24小时制或者12小时制来显示时间.这些设置会直接影响应用程序 ...

  7. 在CentOS7中安装zookeeper

    参考:https://www.linuxidc.com/Linux/2016-09/135052.htm 1.zookeeper运行需要jdk环境,先确保有配置jdk,可以参考此处 2.下载解压zoo ...

  8. 常见的可以写入VIM配置文件中的设置参数

    常见的可以写入.vimrc文件中的设置参数 设置参数 含义 :set nu :set nonu 设置与取消行号 :syn on :syn off 是否依据语法显示相关的颜色帮助,在VIM修改相关的配置 ...

  9. 记一次被DDoS敲诈的历程 糖果LUA FreeBuf 今天 0x01 背景

    记一次被DDoS敲诈的历程 糖果LUA FreeBuf 今天 0x01 背景

  10. LC 450. Delete Node in a BST

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...