Jak używać OpenSSL do szyfrowania / deszyfrowania plików?

Chcę zaszyfrować i odszyfrować jeden plik za pomocą jednego hasła.

Jak mogę używać OpenSSL do tego celu?

Author: jww, 2013-04-17

7 answers

To jest najlepsza odpowiedź na twoje pytanie z google: http://tombuntu.com/index.php/2007/12/12/simple-file-encryption-with-openssl/

Szyfruj:

openssl aes-256-cbc -a -salt -in secrets.txt -out secrets.txt.enc

Odszyfrować:

openssl aes-256-cbc -d -a -in secrets.txt.enc -out secrets.txt.new

Ale to w ogóle nie wykorzystuje infrastruktury klucza publicznego, więc trochę jak wbijanie gwoździa śrubokrętem: -)

 192
Author: Szocske,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-08 04:33:25

Krótka Odpowiedź:

prawdopodobnie chcesz użyć gpg zamiast openssl, więc zobacz "Dodatkowe uwagi" na końcu tej odpowiedzi. Ale aby odpowiedzieć na pytanie za pomocą openssl:

Aby Zaszyfrować:

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

Aby Odszyfrować:

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

Uwaga: podczas szyfrowania lub odszyfrowywania pojawi się monit o podanie hasła.


Długa Odpowiedź:

Twoim najlepszym źródłem informacji dla openssl enc będzie prawdopodobnie: https://www.openssl.org/docs/apps/enc.html

Wiersz poleceń: openssl enc przyjmuje następującą postać:

openssl enc -ciphername [-in filename] [-out filename] [-pass arg]
[-e] [-d] [-a/-base64] [-A] [-k password] [-kfile filename] 
[-K key] [-iv IV] [-S salt] [-salt] [-nosalt] [-z] [-md] [-p] [-P] 
[-bufsize number] [-nopad] [-debug] [-none] [-engine id]

Wyjaśnienie najbardziej przydatnych parametrów w odniesieniu do twojego pytania:

-e
    Encrypt the input data: this is the default.

-d    
    Decrypt the input data.

-k <password>
    Only use this if you want to pass the password as an argument. 
    Usually you can leave this out and you will be prompted for a 
    password. The password is used to derive the actual key which 
    is used to encrypt your data. Using this parameter is typically
    not considered secure because your password appears in 
    plain-text on the command line and will likely be recorded in 
    bash history.

-kfile <filename>
    Read the password from the first line of <filename> instead of
    from the command line as above.

-a
    base64 process the data. This means that if encryption is taking 
    place the data is base64 encoded after encryption. If decryption 
    is set then the input data is base64 decoded before being 
    decrypted.
    You likely DON'T need to use this. This will likely increase the
    file size for non-text data. Only use this if you need to send 
    data in the form of text format via email etc.

-salt
    To use a salt (randomly generated) when encrypting. You always
    want to use a salt while encrypting. This parameter is actually
    redundant because a salt is used whether you use this or not 
    which is why it was not used in the "Short Answer" above!

-K key    
    The actual key to use: this must be represented as a string
    comprised only of hex digits. If only the key is specified, the
    IV must additionally be specified using the -iv option. When 
    both a key and a password are specified, the key given with the
    -K option will be used and the IV generated from the password 
    will be taken. It probably does not make much sense to specify 
    both key and password.

-iv IV
    The actual IV to use: this must be represented as a string 
    comprised only of hex digits. When only the key is specified 
    using the -K option, the IV must explicitly be defined. When a
    password is being specified using one of the other options, the 
    IV is generated from this password.

Uwagi Dodatkowe:

Chociaż pytałeś o OpenSSL, powinieneś rozważyć użycie GPG zamiast do szyfrowania w oparciu o ten artykuł OpenSSL vs GPG do szyfrowania kopii zapasowych poza siedzibą?

Aby użyć Aby zrobić to samo, należy użyć następujących poleceń:

Aby Zaszyfrować:

gpg --output encrypted.data --symmetric --cipher-algo AES256 un_encrypted.data

Aby Odszyfrować:

gpg --output un_encrypted.data --decrypt encrypted.data

Uwaga: podczas szyfrowania lub odszyfrowywania pojawi się monit o podanie hasła.

 106
Author: moo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:33:24

Szyfruj:

openssl enc -in infile.txt -out encrypted.dat -e -aes256 -k symmetrickey

Odszyfrować:

openssl enc -in encrypted.dat -out outfile.txt -d -aes256 -k symmetrickey

Po szczegóły patrz openssl(1) docs.

 29
Author: Ken Cheung,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-04 21:13:12

Aby Zaszyfrować:

$ openssl bf < arquivo.txt > arquivo.txt.bf

Aby Odszyfrować:

$ openssl bf -d < arquivo.txt.bf > arquivo.txt

Bf === Blowfish w trybie CBC

 3
Author: fabio almeida,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-31 18:54:34

Aktualizacja przy użyciu losowo wygenerowanego klucza publicznego.

Encypt:

openssl enc -aes-256-cbc -a -salt -in {raw data} -out {encrypted data} -pass file:{random key}

Odszyfruj:

openssl enc -d -aes-256-cbc -in {ciphered data} -out {raw data}

Mam pełny tutorial na ten temat w http://bigthinkingapplied.com/key-based-encryption-using-openssl/

 3
Author: nneko,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-31 19:43:56

Istnieje program open source, który znalazłem w Internecie używa openssl do szyfrowania i deszyfrowania plików. Robi to za pomocą jednego hasła. Wspaniałą rzeczą w tym skrypcie open source jest to, że usuwa oryginalny niezaszyfrowany plik przez rozdrobnienie pliku. Ale niebezpieczną rzeczą jest to, że gdy oryginalny niezaszyfrowany plik zniknie, musisz upewnić się, że pamiętasz hasło, w przeciwnym razie nie będzie innego sposobu odszyfrowywania pliku.

Tutaj link jest na github

Https://github.com/EgbieAnderson1/linux_file_encryptor/blob/master/file_encrypt.py

 2
Author: Michael linkston,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-01 16:06:17

Zauważ, że OpenSSL CLI używa słabego niestandardowego algorytmu do konwersji hasła na klucz, a instalacja GPG skutkuje różnymi plikami dodanymi do katalogu domowego i uruchomionym procesem w tle gpg-agent. Jeśli chcesz uzyskać maksymalną przenośność i kontrolę za pomocą istniejących narzędzi, możesz użyć PHP lub Pythona, aby uzyskać dostęp do interfejsów API niższego poziomu i bezpośrednio przekazać pełny klucz AES i IV. {]}

Przykład wywołania PHP przez Bash:

IV='c2FtcGxlLWFlcy1pdjEyMw=='
KEY='Twsn8eh2w2HbVCF5zKArlY+Mv5ZwVyaGlk5QkeoSlmc='
INPUT=123456789023456

ENCRYPTED=$(php -r "print(openssl_encrypt('$INPUT','aes-256-ctr',base64_decode('$KEY'),OPENSSL_ZERO_PADDING,base64_decode('$IV')));")
echo '$ENCRYPTED='$ENCRYPTED
DECRYPTED=$(php -r "print(openssl_decrypt('$ENCRYPTED','aes-256-ctr',base64_decode('$KEY'),OPENSSL_ZERO_PADDING,base64_decode('$IV')));")
echo '$DECRYPTED='$DECRYPTED

To wyjście:

$ENCRYPTED=nzRi252dayEsGXZOTPXW
$DECRYPTED=123456789023456
Można też użyj funkcji PHP openssl_pbkdf2, aby bezpiecznie przekonwertować hasło na klucz.
 1
Author: zeroimpl,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-08 02:22:10