使用openssl 的命令行进行文件的加密与解密过程,主要有两种方式:
- openssl 指定加密/解密算法加密
- 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)