PGP Encryption with RSA Algorithm
The PGP keypair can be generated using a variety of encryption algorithms; however, when you manually generate the PGP keys, it provides RSA as the default algorithm option. The RSA algorithm was created by “Rivest-Shamir-Adleman” in 1978. It uses public and private keys for data encryption and decryption. For instance, a client sends the server its public key and requests some data. Then, the server encrypts the data with the help of the client’s public key and sends it to them. After receiving the data, the client decrypts it by utilizing its private key.
RSA permits you to secure your file or message before sending them. Using RSA in the PGP encrypting method lets you certify your data, so the receiver knows if it is altered or modified during the transmission. This write-up will guide you about PGP encryption in java using the RSA algorithm. Moreover, we will also demonstrate a java program for PGP encryption and decryption in Linux. So, let’s start!
Note: Before jumping into the PGP encryption process in Java, install Java JDK on your Linux system if you do not have it already.
How to install Java JDK 17 in Linux
To install Java JDK 17 on your system, firstly, press “CTRL+ALT+T” to open up the terminal and then execute the following curl command:
The error-free output declares that the download is completed. Now, we will extract the “jdk-17_linux-x64_bin.tar.gz” with the help of the below-given “tar” command:
In the next step, we will move the “jdk-17.0.1” extracted directory to the “/opt/” location:
After that, set the Java environment variable and add the path:
Till this point, you have installed and configured Java JDK on your system. For the verification version, type out the “java” command with the “–version” option:
PGP Encryption in Java using RSA
Now, we will move ahead and create a Java program for encrypting and decrypting a message with the help of PGP keys. The algorithm we are going to use is “RSA”, with “PKCS1” padding and “ECB” block mode. The “ECB” or Electronic Code Book mode is the most straightforward approach used to process a sequence of message blocks, whereas the “PKSC1” padding assists RSA in defining the mathematical properties of the PGP key pair and the encryption process.
First of all, we will create a “RSA” class:
}
This “RSA” class will have two variables, the “privateKey” and the “publicKey”:
We will assign the value to the defined PGP keys using a “KeyPairGenerator”. The “KeyPairGenerator” will utilize the “RSA” algorithm for generating the PGP keypair values in the Constructor of RSA class:
try {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(1024);
KeyPair pair = generator.generateKeyPair();
privateKey = pair.getPrivate();
publicKey = pair.getPublic();
} catch (Exception ignored) {}
}
In the next step, we will create a “encode()” and “decode()” function that will be used for encoding and decoding the message with the “base64” (binary to text) encoder and decoder:
Now, add an encrypt() function in the RSA class. In the “encrypt()” function, we will first convert the added message to bytes as the specified encryption can be done using a byte array. After doing so, we will specify the algorithm, block mode, and the padding as “RSA/ECB/PKCS1Padding” for a “cipher” object. Then, we will initialize the cipher with the “ENCRYPT_MODE” and the PGP “publicKey”.
This encrypt() function will use our PGP “publicKey” to encrypt the message and return the encrypted bytes:
Similarly, we will define another function named “decrypt()” to decode the strings that will use our PGP “privateKey” to decrypt the encrypted message in the “DECRYPT_MODE” and return the decrypted array:
byte[] encryptedBytes = decode(encryptedMessage);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE,privateKey);
byte[] decryptedMessage = cipher.doFinal(encryptedBytes);
return new String(decryptedMessage,"UTF8");
}
Now, let’s check the “main()” method functionality. In “main(),”, firstly, we will define two Strings “encryptedMessage” and “decryptedMessage”. The “encryptedMessage” will comprise the encrypted form of our added string “This is linuxhint.com” which we will encrypt using RSA encrypt() function, and the “decryptedMessage” variable will store the decrypted message:
RSA rsa = new RSA();
try{
String encryptedMessage = rsa.encrypt("This is linuxhint.com");
String decryptedMessage = rsa.decrypt(encryptedMessage);
System.err.println("Encrypted:\n"+encryptedMessage);
System.err.println("Decrypted:\n"+decryptedMessage);
}catch (Exception ignored){}
}
Implementation of PGP Encryption in Java
We hope that our given instructions helped you to understand the above-given code. This section will implement the provided PGP encryption Java code on our Linux system to show you its output. For this purpose, firstly, we will create an “RSA.java” java file using the “nano” editor:
Your “RSA.java” file will initially look like this:
Now, add the following code in your “RSA.java” file and press “CTRL+O” to save the added changes:
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
public class RSA {
private PrivateKey privateKey;
private PublicKey publicKey;
public RSA() {
try {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(1024);
KeyPair pair = generator.generateKeyPair();
privateKey = pair.getPrivate();
publicKey = pair.getPublic();
} catch (Exception ignored) {
}
}
public String encrypt(String message) throws Exception{
byte[] messageToBytes = message.getBytes();
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE,publicKey);
byte[] encryptedBytes = cipher.doFinal(messageToBytes);
return encode(encryptedBytes);
}
private String encode(byte[] data){
return Base64.getEncoder().encodeToString(data);
}
public String decrypt(String encryptedMessage) throws Exception{
byte[] encryptedBytes = decode(encryptedMessage);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE,privateKey);
byte[] decryptedMessage = cipher.doFinal(encryptedBytes);
return new String(decryptedMessage,"UTF8");
}
private byte[] decode(String data){
return Base64.getDecoder().decode(data);
}
public static void main(String[] args) {
RSA rsa = new RSA();
try{
String encryptedMessage = rsa.encrypt("This is Linuxhint.com");
String decryptedMessage = rsa.decrypt(encryptedMessage);
System.err.println("Encrypted:\n"+encryptedMessage);
System.err.println("Decrypted:\n"+decryptedMessage);
}catch (Exception ignored){}
}
}
Next, compile the “RSA.java” file by writing out the following command:
The compilation of the specified file will result a “RSA” binary file, execute it to get the output of PGP encryption and decryption in your Linux terminal:
From the output, you can check out the encrypted and decrypted strings:
Conclusion
PGP encryption permits you to send messages or emails securely by using its combination of “Public” and “Private” keys. To show that a sender is the rightful owner of communication, it provides a digital signature for private and public keys. PGP encryption is also used for confirming whether a message is delivered to the intended receiver or not. This write-up guided you about PGP encryption in java using the RSA algorithm. Moreover, we also demonstrated a java program for PGP encryption and decryption in our Linux system.