Comment utiliser OpenSSL pour chiffrer/déchiffrer des fichiers?
7 réponses
c'est la meilleure réponse à votre question de google: http://tombuntu.com/index.php/2007/12/12/simple-file-encryption-with-openssl /
Chiffrer:
openssl aes-256-cbc -a -salt -in secrets.txt -out secrets.txt.enc
décrypter:
openssl aes-256-cbc -d -a -in secrets.txt.enc -out secrets.txt.new
mais cela ne fait pas du tout usage de l'infrastructure à clé publique, donc un peu comme enfoncer un clou avec un tournevis: -)
Courte Réponse:
vous voudrez probablement utiliser gpg
au lieu de openssl
alors voir "notes supplémentaires" à la fin de cette réponse. Mais pour répondre à la question en utilisant openssl
:
Pour Chiffrer:
openssl enc -aes-256-cbc -in un_encrypted.data -out encrypted.data
À Déchiffrer:
openssl enc -d -aes-256-cbc -in encrypted.data -out un_encrypted.data
Note: Vous sera invité pour un mot de passe lors du cryptage ou de déchiffrer.
Longue Réponse:
votre meilleure source d'information pour openssl enc
serait probablement: https://www.openssl.org/docs/apps/enc.html
ligne de commande:
openssl enc
prend la forme suivante:
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]
Explication des plus utiles paramètres en ce qui concerne votre question:
-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.
Notes Complémentaires:
bien que vous ayez spécifiquement demandé à propos D'OpenSSL, vous pourriez vouloir envisager d'utiliser GPG à la place pour le cryptage basé sur cet article OpenSSL vs GPG pour le cryptage des sauvegardes hors site?
pour utiliser GPG pour faire la même chose, vous utiliseriez les commandes suivantes:
Pour Chiffrer:
gpg --output encrypted.data --symmetric --cipher-algo AES256 un_encrypted.data
À Déchiffrer:
gpg --output un_encrypted.data --decrypt encrypted.data
Note: un mot de passe vous sera demandé lors du chiffrement ou du déchiffrement.
Chiffrer:
openssl enc -in infile.txt -out encrypted.dat -e -aes256 -k symmetrickey
décrypter:
openssl enc -in encrypted.dat -out outfile.txt -d -aes256 -k symmetrickey
pour plus de détails, voir openssl(1)
docs.
Pour Chiffrer:
$ openssl bf < arquivo.txt > arquivo.txt.bf
À Déchiffrer:
$ openssl bf -d < arquivo.txt.bf > arquivo.txt
bf === Blowfish en mode CBC
mise à Jour à l'aide d'un aléatoire de la clé publique générée.
Encypt:
openssl enc -aes-256-cbc -a -salt -in {raw data} -out {encrypted data} -pass file:{random key}
Déchiffrer:
openssl enc -d -aes-256-cbc -in {ciphered data} -out {raw data}
j'ai un tutoriel complet à ce sujet à http://bigthinkingapplied.com/key-based-encryption-using-openssl /
il y a un programme open source que je trouve en ligne il utilise openssl pour chiffrer et déchiffrer des fichiers. Il le fait avec un seul mot de passe. La grande chose à propos de ce script open source est qu'il supprime le fichier original non chiffré en déchiquetant le fichier. Mais la chose dangereuse à propos de est une fois que le fichier original non crypté est parti, vous devez vous assurer que vous vous souvenez de votre mot de passe sinon ils ne sont pas une autre façon de décrypter votre fichier.
voici le lien il est sur github
https://github.com/EgbieAnderson1/linux_file_encryptor/blob/master/file_encrypt.py
notez que le CLI D'OpenSSL utilise un algorithme non standard faible pour convertir la phrase de passe en une clé, et l'installation de GPG résulte en divers fichiers ajoutés à votre répertoire personnel et un processus d'arrière-plan gpg-agent en cours d'exécution. Si vous souhaitez un maximum de portabilité et de contrôle avec les outils existants, vous pouvez utiliser PHP ou Python pour accéder aux API de niveau inférieur et passer directement dans une clé AES complète et IV.
exemple d'invocation PHP via 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
Il en résulte:
$ENCRYPTED=nzRi252dayEsGXZOTPXW
$DECRYPTED=123456789023456
vous pouvez également utiliser la fonction openssl_pbkdf2
de PHP pour convertir une phrase de passe en une clé en toute sécurité.