OpenSSL Command Cheatsheets

生成临时密码

1
openssl rand -base64 48

会生成类似如下的字符串 6/4tsW7PR4bYjdY+zzZWEGIsuUz8RIwrNc8FTeQLoeouGO/C3RK/JeqNi8E6nR1l

1
tr -dc 'A-Za-z0-9!?%=' < /dev/urandom | head -c 16; echo

这样生成出来的字符串更适合需要特殊字符的场景, 例如 SYqx!6J3=M8jrUeh

流式加解密

1
2
3
4
5
6
7
8
9
10
# 加密

cat input.raw | openssl enc -aes-256-cbc -e -pbkdf2 -iter 100000 -salt > output.raw
enter AES-256-CBC encryption password:
Verifying - enter AES-256-CBC encryption password:

# 解密

cat output.raw | openssl enc -aes-256-cbc -d -pbkdf2 -iter 100000 -salt > decrypted.raw
enter AES-256-CBC decryption password:

分块传输(对网盘上传比较友好)

1
2
3
4
5
6
7
8
9
10
# 加密后分块

cat input.raw | openssl enc -aes-256-cbc -e -pbkdf2 -iter 100000 -salt | split -b 1G -d -a 3 - chunk_
enter AES-256-CBC encryption password:
Verifying - enter AES-256-CBC encryption password:

# 合并后解密

cat chunk_* | openssl enc -aes-256-cbc -d -pbkdf2 -iter 100000 -salt > decrypted.raw
enter AES-256-CBC decryption password:

CBC加密后分块可以配合tar命令使用, tar可以保证缺块的情况下仍然能够最大限度的提取出可用的文件.

split 命令, -d 为使用数字结尾(而不是aaa,aab这样), -a 2 表示补全到两位, 需要预估总输入大小确保不会出现 01, 02, ..., 99, 100 这样的情况. 否则 cat 的时候可能会错乱.

参考资料

split(1) — Linux manual page