Openssl

如何將字元串(不是文件)傳遞給openssl?

  • October 3, 2016

我想使用 openssl 加密一堆字元串。如何在控制台中將明文傳遞給 openssl(而不是指定具有明文的輸入文件)。

openssl 手冊頁只有這兩個與輸入/輸出相關的選項:

-in <file>     input file
-out <file>    output file

這是我到目前為止所嘗試的:

這工作正常,

openssl aes-256-cbc -a -K 00000000000000000000000000000000 -iv 00000000000000000000000000000000 -in plain.txt -out encrypted.txt

如果我省略 -out 參數,我會在控制台中獲得加密字元串,

openssl aes-256-cbc -a -K 00000000000000000000000000000000 -iv 00000000000000000000000000000000 -in plain.txt

但是如果我同時省略 -in 和 -out,我會得到一個錯誤 - unknown option ‘Encrypt ME’,

openssl aes-256-cbc -a -K 00000000000000000000000000000000 -iv 00000000000000000000000000000000 "Encrypt ME"

用這個:

user@host:~$ echo "my string to encrypt" | openssl aes-256-cbc -e -a -K 00000000000000000000000000000000 -iv 00000000000000000000000000000000
a7svR6j/uAz4kY9jvWbJaUR/d5QdH5ua/vztLN7u/FE=
user@host:~$ echo "a7svR6j/uAz4kY9jvWbJaUR/d5QdH5ua/vztLN7u/FE=" | openssl aes-256-cbc -d -a -K 00000000000000000000000000000000 -iv 00000000000000000000000000000000
my string to encrypt

或者您可以使用命令替換:

user@host:~$ openssl aes-256-cbc -a -K 00000000000000000000000000000000 -iv \
00000000000000000000000000000000 -in <(echo "my string to encrypt") -out encrypted.txt

引用自:https://unix.stackexchange.com/questions/149169