This is a java program for decryption and encryption purpose of text documents it could be run everywhere and can be used to encrypt and decrypt such confidential text documents .
I use the concept of serialization and some mathematical algorithm to encrypt and decrypt the document.After encryption the program will generate a cipher text and a decryption key both of which are ".ser" files.
How to use :-1. If you are encrypting the text file then you have to give the path of that text document upto .txt extension like "D:\power.txt".
2. If you are decrypting the file then you have to give the path of the cipher text and the decryption key both of which are ".ser" files for example:-
cipher Text - "d:\cipher.ser"
decryption Key - "d:\key.ser"
You can use this program to extent your big projects.
algorithm of the code is here
here is the code....
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
public class Encrypt {
int len;
public double yo;
public int[] key;
static String str;
static String data;
public String cipherPath;
public String plainPath;
public String original1;
public int[] cipher;
public int[] c;
public int[] d;
public int[] plain1;
public int[] plain2;
public int[] ascii;
public int[] plain;
static char[] arrow;
char[] original;
/**
* This function starts the encryption process
* @param ascii contains ascii values of all the characters from the input file
*/
public void Encryption_logic(int[] ascii) {
c = new int[ascii.length / 2];
d = new int[ascii.length / 2];
if (ascii.length % 2 == 0) {
for (int k = 0; k < ascii.length / 2; k++) {
c[k] = ascii[k];
c[k] = c[k] * (k + 2);
}
for (int f = 0; f < ascii.length / 2; f++) {
d[f] = ascii[ascii.length / 2 + f];
d[f] = d[f] * (f + 3);
}
}
ciphertext(c, d);
}
/**
* This function prepares the final cipher text(encrypted Text) and decryption key and writes it on to the persistent storage
* @param c
* @param d
*/
public void ciphertext(int[] c, int[] d) {
try {
cipher = new int[c.length + d.length];
for (int q = 0, l = 0, p = 0; q < c.length + d.length; q++) {
if (q % 2 == 0 || q == 0) {
cipher[q] = c[l];
l++;
}
else {
cipher[q] = d[p];
p++;
}
}
System.out
.println("Please Enter The Location Where You Want To Save The Encrypted Cipher Text And The Encryption Key");
BufferedReader br2 = new BufferedReader(new InputStreamReader(
System.in));
cipherPath = br2.readLine();
FileOutputStream file2 = new FileOutputStream(cipherPath
+ "cipher.ser");
ObjectOutputStream obj2 = new ObjectOutputStream(file2);
obj2.writeObject(cipher);
obj2.close();
System.out
.println("your encrypted text and key is saved to the location:- "
+ cipherPath);
key = new int[5];
key[0] = cipher[0] + cipher[cipher.length - 2];
key[1] = d[0];
key[2] = d[0] + c[0];
key[3] = c[c.length - 1];
key[4] = (cipher.length - 1);
FileOutputStream file3 = new FileOutputStream(cipherPath
+ "key.ser");
ObjectOutputStream obj3 = new ObjectOutputStream(file3);
obj3.writeObject(key);
obj3.close();
} catch (Exception e) {
System.out.println("\nexception caught");
}
}
/**
*
* @param cipher is the array containing cipher text
* @param key is the decryption key
*/
public void decryption_logic(int[] cipher, int[] key) {
try {
plain1 = new int[cipher.length / 2];
plain2 = new int[cipher.length / 2];
if ((key[0] == (cipher[0] + cipher[cipher.length - 2]))
&& (key[4] == cipher.length - 1)
&& (key[2] == (cipher[0] + cipher[1]))
&& (key[1] == cipher[1])
&& (key[3] == cipher[cipher.length - 2])) {
for (int p = 0, a = 0, z = 0; p < cipher.length; p++) {
if (p % 2 == 0 || p == 0) {
plain1[a] = cipher[p];
plain1[a] = plain1[a] / (a + 2);
a++;
}
else {
plain2[z] = cipher[p];
plain2[z] = plain2[z] / (z + 3);
z++;
}
}
}
else {
System.out.println("\ndecryption key is not valid");
}
plain = new int[plain1.length + plain2.length];
for (int e = 0, b = 0; e < (plain1.length + plain2.length); e++) {
if (e < plain1.length) {
plain[e] = plain1[e];
} else {
plain[e] = plain2[b];
b++;
}
}
originalText(plain);
} catch (Exception u) {
u.printStackTrace();
}
}
/**
* This function will write the original text to the text file after decryption and store the file at user specified location
* @param plain
* @return
*/
public char[] originalText(int[] plain) {
try {
if (plain[plain.length - 1] == '?') {
original = new char[plain.length - 1];
System.out.println("\n");
for (int o = 0; o < (plain.length - 1); o++) {
original[o] = (char) plain[o];
// System.out.print(original[o]);
}
} else {
original = new char[plain.length];
System.out.println("\n");
for (int o = 0; o < plain.length; o++) {
original[o] = (char) plain[o];
// System.out.print(original[o]);
}
}
original1 = new String(original);
// System.out.println(original1);
System.out
.println("\nPlease enter the location where you want to store the plain text after decryption \n");
BufferedReader br4 = new BufferedReader(new InputStreamReader(
System.in));
plainPath = br4.readLine();
File file4 = new File(plainPath + "plainText.txt");
FileWriter fw = new FileWriter(file4);
PrintWriter pw = new PrintWriter(fw);
pw.write(original1);
System.out.println("your plainText is stored at location :-"
+ plainPath);
pw.flush();
pw.close();
} catch (Exception q) {
q.printStackTrace();
}
return original;
}
/**
* initializing an integer array with the ascii values of the characters from @arrow
* @param arrow
*/
public void asciiarray(char[] arrow) {
int j;
ascii = new int[arrow.length];
System.out.print("\n");
for (j = 0; j < arrow.length; j++) {
ascii[j] = arrow[j];
}
Encryption_logic(ascii);
}
/**
*
* Here '?' is added at the last index of string if its length is not even to make the algorithm work
* and initializing a character array with characters from @data
* @param data contains whole input file data
*/
public void initial(String data) {
int i;
if (data.length() % 2 != 0) {
arrow = new char[data.length() + 1];
arrow[data.length()] = '?';
} else {
arrow = new char[data.length()];
}
for (i = 0; i < data.length(); i++) {
arrow[i] = data.charAt(i);
}
}
/**
* This function will take the file to be encrypted and store its whole content in a single String object.
* @return
*/
public String inputData() {
System.out
.println("Please Enter The Address Of the File To Be Encrypted\n");
try {
BufferedReader br6 = new BufferedReader(new InputStreamReader(
System.in));
str = br6.readLine();
String[] fileExtension=str.split("\\.");
if(!fileExtension[1].equals("txt")){
System.out.println("File to be encrypted is not a text file ...Plese try again");
}
File file = new File(str);
FileReader file1 = new FileReader(file);
BufferedReader br = new BufferedReader(file1);
String line = null;
StringBuilder sb = new StringBuilder();
String is = System.getProperty("line.separator");
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append(is);
}
data = sb.toString();
// System.out.println(data);
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
public static void main(String args[]) {
int choice;
Encrypt ko = new Encrypt();
try {
BufferedReader br1 = new BufferedReader(new InputStreamReader(
System.in));
System.out
.println("what you want to do ???encryption or decryption enter 1 for encryption and 2 for decryption \n");
choice = Integer.parseInt(br1.readLine());
switch (choice) {
case 1:
ko.inputData();
ko.initial(data);
ko.asciiarray(arrow);
break;
case 2:
String encryptedPath;
String keyPath;
BufferedReader br5 = new BufferedReader(new InputStreamReader(
System.in));
System.out
.println("please enter the location of the cipher text\n");
encryptedPath = br5.readLine();
System.out
.println("please enter the location of the decryption key\n");
keyPath = br5.readLine();
FileInputStream file5 = new FileInputStream(encryptedPath);
ObjectInputStream read = new ObjectInputStream(file5);
int[] cipher1 = (int[]) read.readObject();
FileInputStream file6 = new FileInputStream(keyPath);
ObjectInputStream read1 = new ObjectInputStream(file6);
int[] key1 = (int[]) read1.readObject();
ko.decryption_logic(cipher1, key1);
break;
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
while executing your code I am getting following error
ReplyDeletejava.io.FileNotFoundException: E:\File_encryption\document.txt.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(FileInputStream.java:138)
at java.io.FileReader.(FileReader.java:72)
at Encrypt.inputData(Encrypt.java:215)
at Encrypt.main(Encrypt.java:241)
java.lang.NullPointerException
at Encrypt.initial(Encrypt.java:196)
at Encrypt.main(Encrypt.java:242)
Well buddy there must be something wrong with the path of the file you are providing please try removing ".txt" extension from the file path as it can clearly seen above in the Exception two ".txt" are there in the path "E:\File_encryption\document.txt.txt" ,program will automatically append ".txt" in the path so you need not to add it in the path .Try path like below -
Delete"E:\File_encryption\document"
This might help you
bro encryption is done but how to do decryption what to enter in location of cipher text and decryption key
ReplyDeletefollow this pattern to give input cipher text and key while decryption-
Deletecipher path-E:\all stuff\gaurav\folder\all credentials\cipher
key path-E:\all stuff\gaurav\folder\all credentials\key
here "E:\all stuff\gaurav\folder\all credentials\" will be your directory path where you have stored "cipher.ser" and "key.ser" while encrypting the file just remove the ".ser" extenstion while giving input in the decryption program will automatically append that.
have you made documentation of this project if yes will u mail me??
ReplyDeleteWhat type of algorithm is used in this code?
ReplyDelete
ReplyDeletejava.lang.ArrayIndexOutOfBoundsException: 1
at Encrypt.inputData(Encrypt.java:250)
at Encrypt.main(Encrypt.java:287)
java.lang.NullPointerException
at Encrypt.initial(Encrypt.java:227)
at Encrypt.main(Encrypt.java:288)
I am getting the above errors.