# Encrypt & Decrypt Text

To begin encryption and decryption operations, you must first prepare your plain
text. Additionally, you will need a public key that is capable of performing
encryption operations. Information on how to generate such a key can be found in
the chapter on generating a key pair.

## Encrypt

The Encrypt operation itself uses a public key and does not require a private
key. Remember that whoever you want to send it to encrypts it with whose public
key. For people who don't use gpg very often, they often get confused and use
their own keys to encrypt ciphertext.

Note that if you want to encrypt your text and send it to "foo", you need to
know foo's public key first. Then you need to encrypt your text with foo's
public key instead of using your public key. Before encrypting, check to see if
the public key has cryptographic capabilities. This can be viewed in the usage
column of the key toolbox (the letter E stands for encryption).

It is worth mentioning that if you only use foo's public key to encrypt the
ciphertext, no one but foo's own private key can decrypt the ciphertext. There
is a situation where you want ciphertexts to be decrypted by multiple people (
including yourself), please check their corresponding public keys before doing
so. After an encryption operation, if you misuse (some people's public keys are
found to be missing), you can use the undo operation to restore your original
ciphertext before closing GpgFrontend.

### Only Encrypt

In this case, you only encrypt the ciphertext, which results in a shorter
ciphertext. This is because the ciphertext does not contain additional signature
information that identifies the encryptor. If you do not want the recipient to
know your identity, use this method to generate your ciphertexts.

After the encryption operation, no additional information will be displayed in
the information board except for a prompt indicating whether the operation was
successful.

![Peek 2022-01-12 07-16](https://www.bktus.com/wp-content/uploads/2023/08/Peek-2022-01-12-07-16.gif)

### Encrypt Sign

To encrypt and sign text, you need to first prepare the plaintext and have a
public key that can perform encryption operations. The public key used for
encryption should belong to the intended recipient, not yourself. It is
important to verify that the recipient's public key has cryptographic
capabilities for encryption before proceeding.

If you want the recipient to know that the ciphertext is from you, you can also
choose to sign the text while encrypting it. This ensures that the decrypted
text is credible and comes from your hand. This method is commonly used when
both parties need to ensure the authenticity of the decrypted text, and
typically in scenarios where both parties know each other.

To encrypt and sign at the same time, select the public key(s) you need to use
for encryption, and during the encryption process, select the private key you
need to use for signing. This private key should belong to you and should have
the capability for signature operations. You can verify this by checking the
usage column in the key toolbox on the right (letter S stands for signature).

The ciphertext generated by this operation is longer than ciphertext generated
by only encryption because of the additional signature information attached to
it. After the operation is complete, information about the cryptographic and
signature operations will be displayed in the Infomation Board, including
information about the signature pattern and algorithm used. Note that the dates
used in the signature messages are in UTC, not your local time.

To verify the authenticity of the ciphertext before decryption, you can use the
validate operation. Once the ciphertext is verified, you can proceed with
decryption using your private key.

![GIF](https://github.com/saturneric/Blob/blob/master/gif/encrypt-sign.gif?raw=true)

## Decrypt

When decrypting the ciphertext, you can simply paste the obtained ciphertext
into GpgFrontend, and it will automatically select the appropriate private key
for decryption. It is important to note that decryption must be performed with
the private key associated with the public key used for encryption.

![Peek 2022-01-12 07-18](https://www.bktus.com/wp-content/uploads/2023/08/Peek-2022-01-12-07-18.gif)

When decrypting a ciphertext, it is not necessary to check the usage column in
the key toolbox to determine if the key is valid for decryption. Instead, you
need to use a valid private key that corresponds to the public key used for
encryption. You can identify whether a key is a public key or a private key by
checking the type column in the key toolbox. If all your local keys are not
valid for decryption of the ciphertext, the program will display a decryption
failure message.

## Decrypt Verify

During decryption with verification, GPG will check the signature attached to
the ciphertext to ensure its authenticity. This provides an additional layer of
security and helps to prevent tampering with the encrypted message.

To perform decryption with verification, you need to select a file with a ".gpg"
or ".asc" extension, which contains the ciphertext and signature content. If the
signature is valid, GPG will decrypt the message and display it in plain text.
Otherwise, it will display an error message indicating that the signature is not
valid.

It is important to note that whether a key pair can be used for verification
will not be displayed in the usage column. Instead, you need to remember that a
valid public key can be used for verification. Therefore, it is a good habit to
always verify the signature during decryption, regardless of whether the
encryptor has signed in advance. This helps to ensure the authenticity and
integrity of the decrypted message.

![Peek 2022-01-12 07-10](https://www.bktus.com/wp-content/uploads/2023/08/Peek-2022-01-12-07-10.gif)
