Voglio crittografare e decifrare un file usando una password.
Come posso usare OpenSSL per farlo?
Encrypt:
openssl aes-256-cbc -a -salt -in secrets.txt -out secrets.txt.enc
Decrypt:
openssl aes-256-cbc -d -a -in secrets.txt.enc -out secrets.txt.new
Probabilmente vuoi usare gpg
invece di openssl
quindi vedi "Note aggiuntive" alla fine di questa risposta. Ma per rispondere alla domanda usando openssl
:
Per crittografare:
openssl enc -aes-256-cbc -in un_encrypted.data -out encrypted.data
Per decifrare:
openssl enc -d -aes-256-cbc -in encrypted.data -out un_encrypted.data
Nota: verrà richiesta una password per la crittografia o la decrittografia.
La tua migliore fonte di informazioni per openssl enc
sarebbe probabilmente: https://www.openssl.org/docs/apps/enc.html
Riga di comando: openssl enc
assume il seguente formato:
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]
Spiegazione dei parametri più utili per quanto riguarda la tua domanda:
-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.
Sebbene tu abbia chiesto esplicitamente su OpenSSL, potresti prendere in considerazione l'idea di utilizzare GPG invece per lo scopo di crittografia basato su questo articolo OpenSSL vs GPG per crittografare i backup esterni al sito?
Per usare GPG per fare lo stesso, useresti i seguenti comandi:
Per crittografare:
gpg --output encrypted.data --symmetric --cipher-algo AES256 un_encrypted.data
Per decifrare:
gpg --output un_encrypted.data --decrypt encrypted.data
Nota: verrà richiesta una password per la crittografia o la decrittografia.
Encrypt:
openssl enc -in infile.txt -out encrypted.dat -e -aes256 -k symmetrickey
Decrypt:
openssl enc -in encrypted.dat -out outfile.txt -d -aes256 -k symmetrickey
Per i dettagli, vedere i documenti openssl(1)
.
Per crittografare:
$ openssl bf < arquivo.txt > arquivo.txt.bf
Per decifrare:
$ openssl bf -d < arquivo.txt.bf > arquivo.txt
bf === Blowfish in modalità CBC
Si noti che OpenSSL CLI utilizza un algoritmo non standard debole per convertire la passphrase in una chiave, e l'installazione dei risultati GPG in vari file aggiunti alla propria directory home e un processo in background di gpg-agent in esecuzione. Se si desidera la massima portabilità e controllo con gli strumenti esistenti, è possibile utilizzare PHP o Python per accedere alle API di livello inferiore e passare direttamente in una chiave AES e IV complete.
Esempio PHP invocazione tramite 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
Questo produce:
$ENCRYPTED=nzRi252dayEsGXZOTPXW
$DECRYPTED=123456789023456
È anche possibile utilizzare la funzione openssl_pbkdf2
di PHP per convertire in modo sicuro una passphrase in una chiave.
Aggiorna usando una chiave pubblica generata casualmente.
encypt:
openssl enc -aes-256-cbc -a -salt -in {raw data} -out {encrypted data} -pass file:{random key}
Decrypt:
openssl enc -d -aes-256-cbc -in {ciphered data} -out {raw data}
Ho un tutorial completo su questo a http://bigthinkingapplied.com/key-based-encryption-using-openssl/
C'è un programma open source che trovo online utilizza openssl per crittografare e decodificare i file. Lo fa con una sola password. La cosa grandiosa di questo script open source è che elimina il file originale non crittografato distruggendo il file. Ma la cosa pericolosa è che una volta che il file originale non criptato è andato devi assicurarti di ricordare la tua password altrimenti non sono altro modo per decifrare il tuo file.
Qui il link è su github
https://github.com/EgbieAnderson1/linux_file_encryptor/blob/master/file_encrypt.py
NON USARE DERIVAZIONE CHIAVE PREDEFINITA OPENSSL.
Attualmente la risposta accettata ne fa uso e non è più raccomandata e sicura.
È molto fattibile per un utente malintenzionato semplicemente forzare la chiave.
https://www.ietf.org/rfc/rfc2898.txt
PBKDF1 applica una funzione di hash, che deve essere MD2 [6], MD5 [19] o SHA-1 [18], per derivare le chiavi. La lunghezza della chiave derivata è limitata dalla lunghezza dell'output della funzione hash, ovvero 16 ottetti per MD2 e MD5 e 20 ottetti per SHA-1. PBKDF1 è compatibile con il processo di derivazione della chiave in PKCS # 5 v1.5. PBKDF1 è consigliato solo per la compatibilità con le applicazioni esistenti poiché le chiavi che produce potrebbero non essere abbastanza grandi per alcune applicazioni.
PBKDF2 applica una funzione pseudocasuale (vedi Appendice B.1 per un esempio) per derivare le chiavi. La lunghezza della chiave derivata è essenzialmente illimitata. (Tuttavia, lo spazio di ricerca efficace massimo per la chiave derivata può essere limitato dalla struttura della funzione pseudocasuale sottostante.Vedere l'Appendice B.1 per ulteriori discussioni.) PBKDF2 è raccomandato per nuove applicazioni.
Fai questo:
openssl enc -aes-256-cbc -pbkdf2 -iter 20000 -in hello -out hello.enc -k meow
openssl enc -d -aes-256-cbc -pbkdf2 -iter 20000 -in hello.enc -out hello.out
Nota : Le iterazioni nella decrittografia devono essere uguali alle iterazioni nella crittografia.
Le iterazioni devono essere un minimo di 10000. Ecco una buona risposta sul numero di iterazioni: https://security.stackexchange.com/a/3993
Inoltre ... abbiamo abbastanza persone qui che raccomandano GPG. Leggi la dannata domanda.