Tuesday, November 19, 2024

EDI : Create pgp Encryption and Decryption

 Here’s a detailed guide on how to create PGP encryption using the GPG tool with examples.

Step 1: Install GPG

You’ll first need to install GPG, which is an open-source implementation of PGP (Pretty Good Privacy).

  • For Linux (Ubuntu/Debian):

    bash

    sudo apt-get install gnupg
  • For macOS:

    bash

    brew install gnupg
  • For Windows: Download and install Gpg4win.

Step 2: Generate a Key Pair

You need to create a public and private key pair. The public key is used to encrypt, and the private key is used to decrypt.

  1. Open your terminal or command prompt.

  2. Run the following command to generate the key:

    bash

    gpg --gen-key

    The tool will ask you for some details:

    • Key type: Choose (1) RSA and RSA.
    • Key size: 2048 bits (or 4096 for more security).
    • Expiration: Choose an expiration date or let it never expire.
    • Name and Email: Provide your name and email. These are tied to your key.
    • Passphrase: Set a passphrase to protect your private key.

Example Output:

plaintext

gpg: key A1B2C3D4E5 created gpg: public key exported gpg: private key saved

Step 3: Export Your Public Key

You’ll need to share your public key with anyone who wants to send encrypted data to you.

  1. Run the following command to export your public key:

    bash

    gpg --armor --export your_email@example.com
  2. The output will look something like this:

    plaintext

    -----BEGIN PGP PUBLIC KEY BLOCK----- mQENBF9d4FsBCADSVW9wWn8OqLkkFWdZJ3a6LbPNcvKxyAYxzS1DULuKMXyw VY8s6+UgndOpzR7OEOW0dsIz7p9mUPUVQ== -----END PGP PUBLIC KEY BLOCK-----
  3. Copy the block and share it with people who will use it to encrypt files for you.

Step 4: Encrypt a File

Now, let’s say you want to encrypt a file using someone's public key. First, import the recipient’s public key.

  1. To import the key (assuming you’ve received it as recipient_key.asc):

    bash

    gpg --import recipient_key.asc
  2. Now, you can encrypt a file for that recipient. Suppose you want to encrypt example.txt:

    bash

    gpg --output example.txt.gpg --encrypt --recipient recipient_email@example.com example.txt
  3. This will create example.txt.gpg, which is the encrypted file.

Example Output:

plaintext

gpg: encrypted file created: example.txt.gpg

Step 5: Decrypt a File

To decrypt a file that was encrypted with your public key:

  1. Run the following command:

    bash

    gpg --output decrypted_example.txt --decrypt example.txt.gpg
  2. You’ll be prompted to enter your passphrase. After that, the decrypted file will be saved as decrypted_example.txt.

Example Output:

plaintext

gpg: AES encrypted data gpg: decryption successful, saved as 'decrypted_example.txt'

Step 6: Signing a File (Optional)

If you want to sign a file to prove its authenticity, you can do so with your private key.

  1. To sign a file, run:

    bash

    gpg --output example.txt.sig --sign example.txt
  2. This creates a signature file (example.txt.sig) that others can use to verify the file’s integrity and authenticity.

Example Output:

plaintext

gpg: signing file created: example.txt.sig

Step 7: Verifying a Signature (Optional)

To verify a signature on a file you’ve received, use:

bash

gpg --verify example.txt.sig example.txt

This will check if the file matches the signature and if it was signed by a valid key.

Complete Example Scenario

  1. Generate your key pair:

    bash

    gpg --gen-key
  2. Export your public key:

    bash

    gpg --armor --export your_email@example.com > mypublickey.asc
  3. Encrypt a file for a recipient:

    bash

    gpg --output encrypted.txt.gpg --encrypt --recipient recipient@example.com plaintext.txt
  4. Decrypt a received file:

    bash

    gpg --output decrypted.txt --decrypt encrypted.txt.gpg
  5. Sign a file:

    bash

    gpg --output signed.txt.sig --sign plaintext.txt
  6. Verify a signature:

    bash

    gpg --verify signed.txt.sig plaintext.txt

Let me know if you need further assistance or more examples!

1 comment:

Anonymous said...

Very informative