How to use the command "age" (with examples)
“age” is a simple, modern, and secure file encryption tool. It allows users to encrypt and decrypt files using passphrase-based encryption or public key-based encryption.
Use case 1: Generate an encrypted file that can be decrypted with a passphrase
Code:
age --passphrase --output path/to/encrypted_file path/to/unencrypted_file
Motivation: This use case is helpful when you want to secure sensitive data with a passphrase that is known only to you. By encrypting a file with a passphrase, you can ensure that the file remains confidential and can be decrypted only by entering the correct passphrase.
Explanation:
--passphrase
flag instructs the “age” command to encrypt the file using a passphrase.--output
specifies the path where the encrypted file will be saved.path/to/encrypted_file
is the location and name of the encrypted file.path/to/unencrypted_file
is the location and name of the file to be encrypted.
Example output:
The unencrypted file located at path/to/unencrypted_file
will be encrypted using a passphrase. The encrypted file will be saved at path/to/encrypted_file
.
Use case 2: Encrypt a file with one or more public keys entered as literals
Code:
age --recipient public_key --output path/to/encrypted_file path/to/unencrypted_file
Motivation: Encrypting a file with one or more public keys ensures that only the intended recipients can decrypt and access the file. This method is useful when you want to share encrypted files securely with specific individuals.
Explanation:
--recipient public_key
flag specifies the public key of one recipient. To specify multiple recipients, you can repeat the--recipient
flag.--output
specifies the path where the encrypted file will be saved.path/to/encrypted_file
is the location and name of the encrypted file.path/to/unencrypted_file
is the location and name of the file to be encrypted.
Example output:
The unencrypted file located at path/to/unencrypted_file
will be encrypted using the public key specified in public_key
. The encrypted file will be saved at path/to/encrypted_file
.
Use case 3: Encrypt a file to one or more recipients with their public keys specified in a file
Code:
age --recipients-file path/to/recipients_file --output path/to/encrypted_file path/to/unencrypted_file
Motivation: This use case is helpful when you want to encrypt a file for multiple recipients without having to enter each recipient’s public key individually. By specifying the recipients’ public keys in a file, you can easily encrypt files for a group of individuals.
Explanation:
--recipients-file path/to/recipients_file
flag specifies the file containing the public keys of multiple recipients. Each key should be on a separate line in the file.--output
specifies the path where the encrypted file will be saved.path/to/encrypted_file
is the location and name of the encrypted file.path/to/unencrypted_file
is the location and name of the file to be encrypted.
Example output:
The unencrypted file located at path/to/unencrypted_file
will be encrypted for each recipient specified in the recipients_file
. The encrypted file will be saved at path/to/encrypted_file
.
Use case 4: Decrypt a file with a passphrase
Code:
age --decrypt --output path/to/decrypted_file path/to/encrypted_file
Motivation: When you receive a file encrypted with a passphrase, you need to decrypt it to access the actual content. By using this use case, you can decrypt the file using the correct passphrase and obtain the original unencrypted content.
Explanation:
--decrypt
flag instructs the “age” command to decrypt the file.--output
specifies the path where the decrypted file will be saved.path/to/decrypted_file
is the location and name of the decrypted file.path/to/encrypted_file
is the location and name of the file to be decrypted.
Example output:
The encrypted file located at path/to/encrypted_file
will be decrypted using the correct passphrase. The decrypted content will be saved at path/to/decrypted_file
.
Use case 5: Decrypt a file with a private key file
Code:
age --decrypt --identity path/to/private_key_file --output path/to/decrypted_file path/to/encrypted_file
Motivation: This use case is applicable when you possess the private key associated with an encrypted file. By decrypting the file using the private key, you can access its original content.
Explanation:
--decrypt
flag instructs the “age” command to decrypt the file.--identity path/to/private_key_file
specifies the private key file that corresponds to the encrypted file.--output
specifies the path where the decrypted file will be saved.path/to/decrypted_file
is the location and name of the decrypted file.path/to/encrypted_file
is the location and name of the file to be decrypted.
Example output:
The encrypted file located at path/to/encrypted_file
will be decrypted using the corresponding private key file at path/to/private_key_file
. The decrypted content will be saved at path/to/decrypted_file
.
Conclusion:
The “age” command provides a simple and secure way to encrypt and decrypt files. Whether you want to encrypt files with a passphrase or encrypt them for specific recipients using public key cryptography, “age” offers a user-friendly interface to ensure the confidentiality and privacy of your data.