Intenta entender el código correctamente antes de adaptarlo a tu proyecto y si ttienes algo que aportar puedes dejarlo en los comentarios.
Solución:
Advertencia de seguridad: AES-256-CBC no proporciona cifrado autenticado y es vulnerable a los ataques de Oracle de relleno. Deberías usar algo como la edad en su lugar.
Cifrar:
openssl aes-256-cbc -a -salt -in secrets.txt -out secrets.txt.enc
Descifrar:
openssl aes-256-cbc -d -a -in secrets.txt.enc -out secrets.txt.new
Más detalles sobre las distintas banderas
Respuesta corta:
Es probable que desee utilizar gpg
en lugar de openssl
así que vea “Notas adicionales” al final de esta respuesta. Pero para responder a la pregunta usando openssl
:
Para cifrar:
openssl enc -aes-256-cbc -in un_encrypted.data -out encrypted.data
Para descifrar:
openssl enc -d -aes-256-cbc -in encrypted.data -out un_encrypted.data
Nota: Se le pedirá una contraseña al cifrar o descifrar.
Respuesta larga:
Su mejor fuente de información para openssl enc
probablemente sería: https://www.openssl.org/docs/man1.1.1/man1/enc.html
Línea de comando:openssl enc
toma la siguiente forma:
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]
Explicación de los parámetros más útiles con respecto a su pregunta:
-e
Encrypt the input data: this is the default.
-d
Decrypt the input data.
-k
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
Read the password from the first line of 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.
-md digest
Use the specified digest to create the key from the passphrase.
The default algorithm as of this writing is sha-256. But this
has changed over time. It was md5 in the past. So you might want
to specify this parameter every time to alleviate problems when
moving your encrypted data from one system to another or when
updating openssl to a newer version.
Notas adicionales:
Aunque ha preguntado específicamente sobre OpenSSL, es posible que desee considerar usar GPG en su lugar con el propósito de encriptar según este artículo ¿OpenSSL vs GPG para encriptar copias de seguridad fuera del sitio?
Para usar GPG para hacer lo mismo, usaría los siguientes comandos:
Para cifrar:
gpg --output encrypted.data --symmetric --cipher-algo AES256 un_encrypted.data
Para descifrar:
gpg --output un_encrypted.data --decrypt encrypted.data
Nota: Se le pedirá una contraseña al cifrar o descifrar.
Cifrar:
openssl enc -in infile.txt -out encrypted.dat -e -aes256 -k symmetrickey
Descifrar:
openssl enc -in encrypted.dat -out outfile.txt -d -aes256 -k symmetrickey
Para más detalles, consulte el openssl(1)
documentos
Te invitamos a ayudar nuestro análisis fijando un comentario y valorándolo te lo agradecemos.