1. CodersCay »
  2. PGP »
  3. How to Encrypt and Decrypt the files and text using GPG/PGP tool

Published On: 11/15/2018

CodersCay Logo

How to Encrypt and Decrypt the files and text using GPG/PGP tool

PGP (Pretty Good Privacy) is an encrypted file format by combination of public and private that used to share the data or message with confidential. Say for example, in the hackers world we cannot say the data transfers are safe to client and we need to tight up the security on data using some tool.


PGP is used to sign messages so that the receiver can verify both the identity of the sender and the integrity of the content. PGP uses a private key that must be kept secret and a public key that sender and receiver must share. The technology is also known as GPG (Gnu Privacy Guard or GnuPG), which is a fully compatible GPL-licensed alternative.
File Encrypt and Decrypt process diagram using PGP tool

We can encrypt/decrypt the file using GPG tool once we have configured the public and private key after the GPG tool installation. The following example will show you how to encrypt a file located in a different directory, to a specified user.


Manual Encryption/Decryption using PGP tool

Encrypt the Text manually using PGP tool

Once completed the PGP tool installation and configuration with public and private key then we can encrypt the file/text manually using the tool as below.

Step 1: Open GPA tool using desktop shortcut icon and type or paste the text on clipboard window.

Step 2: Click Encrypt icon from the tool bar and choose either public or private key on next window to encrypt the text.

Step 3: Click Ok button and the text will be encrypted like as below.

Before EncryptionAfter Encryption
Before encryption the text in PGP tool
Ater encrypting the text in PGP tool

We can share this encrypted text to client and they can do the text decryption by using decrypt option from tool bar.

Encrypt the file manually using Kleopatra tool

Kleopatra is an another tool to encrypt and decrypt the file manually which we can be obtained during the gpg4win installation.

Step 1: Open the Kleopatra tool using desktop shortcut and select Sign/Encrypt.. button from the tool bar.

Step 2: Choose a file from local drive which one you want to do encryption and assign the target destination to encryption file as shown picture below.

Step 3: We have to authenticate the Passphrase which we created during the PGP tool configuration.

File encryption using kleopatra/PGP tool

The encrypted file name extension would be .GPG and the file content cannot be read even if we open with notepad.


Encrypting the file using GPG command line

We can perform the file encryption in command prompt by calling PGP executable file from the installation path.

From the command prompt, type:

gpg.exe --batch --recipient <key> --output "C:\EncryptOut\fileName.txt.pgp" --encrypt --passphrase <password> --sign "C:\FlatFiles\fileName.txt"

Parameters required to Encrypt the file using command line


Parameter Name Description
Batch (--batch)Encryption using Batch mode.
Recipient (--recipient)Specify the recipient of a public-key encrypted document.
Output (--output)Specify the file in which to place output.
Encrypt (--encrypt)Specify the document to Encrypt the document.
Passphrase (--passphrase)Specify the password which we defined during the configuration.
Sign (--sign)Sign a document.


Decrypting the file using GPG command line

From the command prompt, type:

gpg.exe --batch --passphrase <password> --out "C:\DecryptOut\fileName.txt" --decrypt "C:\EncryptOut\fileName.txt.pgp"

Parameters required to Decrypt the file using command line


Parameter Name Description
Batch (--batch)Encryption using Batch mode.
Output (--out)Specify the file in which to place output.
Decrypt (--encrypt)Specify the operation to Encrypt the document.
Passphrase (--passphrase)Specify the password which we defined during the configuration.

Note: We can create a batch file, there we can use the same command line to execute the GPG encryption tool.


Encrypt the file using SSIS Package

It is an another automate way to encrypt the file using GPG tool in SSIS package. As shown in picture below, we can execute the GPG tool in SSIS package using execute process task tool. There are two mandatory parameters needs to be configured on the task such as Executable and Arguments.

Encrypt the file in SSIS package using PGP tool

Process Parameters
Executable - To mention the GPG tool installation path.
Arguments - We have to mention the parameters which we used in command prompt line.

Also, we need to ensure the GPG tool installation path and file path to avoid file path issues. To modify our task to use a variable, you need to create the variable in the designer and then you can reference the variable in the Expressions tab.

For example, I am using the Expressions page to modify the argument value with the following:

"--batch --recipient " + @[User::GPG_PublicKey] + " --output " + "\""+ @[User::OutputFolder] + "\\" + @[User::FlatFileName] + ".pgp" + "\" --encrypt --passphrase " + @[User::GPG_PrivatePassPhrase] + " --sign " + "\""+ @[User::InputFolder] + "\\" + @[User::FlatFileName] + "\""
SSIS expression for GPG/PGP encryption

That’s it. We can use the same method for file decryption process in SSIS package using GPG tool.



No comments:

Post a Comment