Skip to content

Instantly share code, notes, and snippets.

@oseme-techguy
Last active February 25, 2021 13:02
Show Gist options
  • Save oseme-techguy/fb33eedc0cdb73a78347a42cc7be6ddd to your computer and use it in GitHub Desktop.
Save oseme-techguy/fb33eedc0cdb73a78347a42cc7be6ddd to your computer and use it in GitHub Desktop.
How to use the JAVA PGP library - These are the steps required to use the CoralPay JAVA PGP Library.

These are the steps required to use the CoralPay JAVA PGP Library.
The library's binaries can be downloaded from here: CoralPay JAVA PGP Library Link .

Build the Library:

To build the library, run .\gradlew.bat build --refresh-dependencies
This should build the library file into a .jar in the \build\libs directory.
Copy the library out of the directory to the target project that needs to use the libary
and follow the steps below to add the required dependecies to use with the library for that target project.

Required Dependencies:

The PGP library depends on some dependencies for it to function properly.
If you are using the Maven dependency manager, kindly add these to your root pom.xml file:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20080701</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.8.1</version>
</dependency>
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.9</version>
</dependency>
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.60</version>
</dependency>
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcpg-jdk15on</artifactId>
    <version>1.60</version>
</dependency>

On the other hand, if you are using Gradle to manage your dependencies, add this to your root build.gradle file:

implementation 'org.json:json:20080701'
implementation 'commons-io:commons-io:2.6'
implementation 'org.apache.commons:commons-lang3:3.8.1'
implementation 'commons-codec:commons-codec:1.9'

 // for encryption and decryption
implementation "org.bouncycastle:bcprov-jdk15on:1.60" // general
implementation "org.bouncycastle:bcpg-jdk15on:1.60" // for openPGP support

Steps:

Use the library like this

import com.coralpay.pgp.PGPEncryption;
import com.coralpay.pgp.Helper;


String privateKeyPath="C:\\users\\path\\to\\your\\private\\key\\privatekey.txt";    // you have to escape the backslash on windows
String privateKeyPassword="password-of-your-private-key";
String publicKeyPath="C:\\users\\path\\to\\CGATE\\public\\key\\publickey.txt";  // you have to escape the backslash on window

// Load the private and public keys here
private byte[] privateKeyFileContent = Helper.getKeyFileContentAsByteArray(privateKeyPath);
private byte[] publicKeyFileContent = Helper.getKeyFileContentAsByteArray(publicKeyPath);

// the request payload to encrypt
String compactJson = "{\"userInfo\":" +
        "{\"userName\":\"user-name-here\",\"password\":\"password-here\"}," +
        "\"posRequest\":{\"terminalId\":\"xxxxxxxx\",\"amount\":2000.0," +
        "\"merchantId\":\"xxxxxxxx\"}}";

String encryptedRequestString = PGPEncryption.encryptRequest(compactJson, publicKeyFileContent);

// you can then send this string to us using the Content-Type: text/plain
System.out.println(encryptedRequestString); // log it to console

The com.coralpay.pgp.PGPEncryption.encryptRequest(); method will do the encryption and convert it to Hex for you
To decrypt the response from Cgate, do:

String encryptedResponseString = "85010c030bb829ea73cc45510107ff7a6e9299412eff1ca945e78ba2e718f62e06807291b75036fea07b5a07959f0a8cf2bb3628f3c09a55f70e4603f55320d6a59a6957b67e0d90be95efa9ad13b376fb8ac5d23c898fdf927aa7912823a6d44985330fa5ff44d5ecc76039610477e7c8e7b6bfd2bb04b314c93566b0714ce7344b623b56477a9b64a3c518f54a63cee3f12cd2f76c0e75dd1c7c4fdd6e35e6fa2e4a08573b408b6441554010e60987f1d27df30a27a58ab81d686430d7861b780ec6bc4e21f9d7e69bb581a5eb5fb5238dce4cd8810371df16c7e9ffee419e375d770f45332d32224669d13d874a7392ec9ffff563d3d84d5e12c37cbc452b39ea21d84cf3bb9f31416d64f35dbbc97a620714f69415afc93f11b8794bbe4a17c867c0b2ea3b2d4be86437677a8be3d42636f76280129374c11509a577b95e6b2c07c2e22394b14ae20335739595f3f26afbdbaa1d08684cd8d187c4fb8d8467c0fe15c59cd3762164718fb6b1c2ea4b8e1d17cfa9a15817e6b0d65e0f018acfbb1266182ea46668c0fb";


String decryptedJSONOutPut = PGPEncryption.decryptResponseString(
        encryptedResponseString, privateKeyFileContent, privateKeyPassword
);

// you can then use the JSON response here 
System.out.println(decryptedJSONOutPut); // log it to console

That is all it takes to use the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment