Encrypt Decrypt File in Java Using Cipher class and RSA algorithm





How to Encrypt Decrypt File in Java ?

Using Cipher class and RSA algorithm we can encrypt and decrypt a file.

What is Cipher Class?

This is a java class, use cryptographic algorithm for encryption and decryption.

RSA Algorithm

RSA, is an asymmetric cryptographic algorithm used for message encryption and decryption.
Asymmetric means that there are two different keys used for encryption and decryption.
For encryption we use public key and for decryption we use private key.
public key is given to everyone but the other key(private) must be kept secrete.

Code for File Encryption and Decryption using RSA algorithm

package com.javaant;

import java.io.File;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.Cipher;

import org.apache.commons.io.FileUtils;

class MyRSACipher {
	public static KeyPair getRSAKeyPair() throws NoSuchAlgorithmException {
		KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
		kpg.initialize(2048);
		KeyPair kp = kpg.generateKeyPair();
		return kp;
	}

	public static byte[] encryptFile(byte[] inputBytes, PublicKey key, String xform) throws Exception {
		Cipher cipher = Cipher.getInstance(xform);
		cipher.init(Cipher.ENCRYPT_MODE, key);
		return cipher.doFinal(inputBytes);
	}

	public static byte[] decryptFile(byte[] inputBytes, PrivateKey key, String xform) throws Exception {
		Cipher cipher = Cipher.getInstance(xform);
		cipher.init(Cipher.DECRYPT_MODE, key);
		return cipher.doFinal(inputBytes);
	}
}

public class EncryptDecryptFileRSA {
	public static void main(String[] args) throws Exception {

		// TODO Auto-generated method stub
		String fileToEncrypt = "D:/Java_Ant_Post/Encrypt-Decrypt-File-RSA/original-file";
		String encryptedFile = "D:/Java_Ant_Post/Encrypt-Decrypt-File-RSA/file-after-encryption";
		String decryptedFile = "D:/Java_Ant_Post/Encrypt-Decrypt-File-RSA/file-after-decryption";

		// Generate a key-pair

		KeyPair keyPari = MyRSACipher.getRSAKeyPair();
		PublicKey publicKey = keyPari.getPublic();
		PrivateKey privatekey = keyPari.getPrivate();

		File file = new File(fileToEncrypt);
		byte[] dataBytes = FileUtils.readFileToByteArray(file);

		// Encrypt the file

		String algo = "RSA/ECB/PKCS1Padding";
		byte[] encryptedBytes = MyRSACipher.encryptFile(dataBytes, publicKey, algo);
		file = new File(encryptedFile);
		FileUtils.writeByteArrayToFile(file, encryptedBytes);
		System.out.println("Encrypted file : " + encryptedFile);

		// Decrypt the file

		byte[] decryptedBytes = MyRSACipher.decryptFile(encryptedBytes, privatekey, algo);
		file = new File(decryptedFile);
		FileUtils.writeByteArrayToFile(file, decryptedBytes);
		System.out.println("Decrypted file : " + decryptedFile);

	}

}

download Code

Profile photo of Nirmal Dhara
About Nirmal Dhara 28 Articles
Java Developer

Leave a Reply

Your email address will not be published.


*