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.
First Class
package main.java;
import javax.crypto.Cipher;
import java.security.*;
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);
}
}
Second Class
package main.java;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
public class EncryptDecryptFileRSA {
public static void main(String[] args) throws Exception {
String fileToEncrypt = "D:/FILE/Encrypt-Decrypt-File-RSA/original-file.txt";
String encryptedFile = "D:/FILE/Encrypt-Decrypt-File-RSA/file-after-encryption";
String decryptedFile = "D:/FILE/Encrypt-Decrypt-File-RSA/file-after-decryption";
// Generate a key-pair
KeyPair keyPari = MyRSACipher.getRSAKeyPair();
PublicKey publicKey = keyPari.getPublic();
System.out.println("publicKey-->"+publicKey);
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
publicKey.getEncoded());
FileOutputStream fos = new FileOutputStream("D:/FILE/Encrypt-Decrypt-File-RSA/");
fos.write(x509EncodedKeySpec.getEncoded());
fos.close();
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);
}
}
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.
First Class
package main.java;
import javax.crypto.Cipher;
import java.security.*;
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);
}
}
Second Class
package main.java;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
public class EncryptDecryptFileRSA {
public static void main(String[] args) throws Exception {
String fileToEncrypt = "D:/FILE/Encrypt-Decrypt-File-RSA/original-file.txt";
String encryptedFile = "D:/FILE/Encrypt-Decrypt-File-RSA/file-after-encryption";
String decryptedFile = "D:/FILE/Encrypt-Decrypt-File-RSA/file-after-decryption";
// Generate a key-pair
KeyPair keyPari = MyRSACipher.getRSAKeyPair();
PublicKey publicKey = keyPari.getPublic();
System.out.println("publicKey-->"+publicKey);
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
publicKey.getEncoded());
FileOutputStream fos = new FileOutputStream("D:/FILE/Encrypt-Decrypt-File-RSA/");
fos.write(x509EncodedKeySpec.getEncoded());
fos.close();
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);
}
}
Output
publicKey-->Sun RSA public key, 2048 bits
modulus: 20108450618577999400803556398621748138483970228282681454137798459807080261849680669638196429890566086258004486706030281913277961816017283164262033948903401817430194761131221479641410324078985541628396085943772105824997639007344052983515804354069396321195716125886288792880800227623319031215812130828151773288013561509336038201981689698922415872642267396880594792523207347110325195088649817799583135198697012029627292298420178416877457628346215748630564677263667173774605499785542537261333161213324221714085092610331914821417368301540182275767852914224702064724805388822136284841746800779767545813037678351422016587421
public exponent: 65537
Encrypted file : D:/FILE/Encrypt-Decrypt-File-RSA/file-after-encryption
Decrypted file : D:/FILE/Encrypt-Decrypt-File-RSA/file-after-decryption
HELLO Welcome To Bugreaper.blogspot.in
The above text is Encrypted To
/„ Aï> Ḿ6‰×܉^U\s{ˆ74ÛˆéÃr œ4Ü ü”/Í‘`üî^„ä:wC¥\0Š×ÂÅ ™ç^è€ÂGà >À~ ðN
6ËSû÷«ß_½ h;ŒŽ» þ~Ä£’QMò…'ÑÓ•~ >·Zû¯ä D‘
ðÇÒga<ßåäá—Nðœüš”—Ã1 î6êK½+Ó—ü^S7$ W¨ÁôPLªAÓ?×ÎUã0S¤$Éð ¢u׶ êÀÈI_ƒŽ9“©îA:R * \ŠÚ ”í ž!Ñëƒ0‡Õ¬ÐôíI ²H¤Þð ¦^KIÀÿç)çwîoÉLÞô‹eÉ cXÒ X:ÔÓ£
No comments:
Post a Comment