Source Code :

1. file XOREnkripsi.java

class  XOREnkripsi{

	public String charToBinary(char c){
		int ascii = (int)c;
		String binaryCar = Integer.toBinaryString(ascii);
		String addBit="";
		if(binaryCar.length()<8){
			for(int index=0; index < 8-binaryCar.length(); index++){
				addBit = "0" +addBit;
			}
		}
		return addBit+binaryCar;
	}

	public String XORKriptografi (String plain, String key){
		StringBuffer enskripsi = new StringBuffer();
		for(int index=0; index < plain.length(); index++){
			int longKey = key.length();
			Character textJ = new Character(plain.charAt(index));
			Character keyJ = new Character(key.charAt(index%longKey));
			int xor = Integer.parseInt(textJ.toString()) ^ Integer.parseInt(keyJ.toString());
			enskripsi.append(xor);
		}
		return enskripsi.toString();
	}

	public String binaryToAsciiToString(String onebit){
		int ascii;
		ascii = Integer.parseInt(onebit, 2);
		return Character.toString((char)ascii);
	}

}

2. File PlainToChiper.java

import java.io.*;
import java.util.*;

class PlainToChiper{
	public static void main(String[] args)
	{
		XOREnkripsi xor = new XOREnkripsi();

		FileInputStream fis;
		PrintWriter pw;
		File file;

		DataInputStream dis;
		BufferedReader br;

		String plainText = new String();
		String plainKey = new String();
		String binaryText = new String();
		String binaryKey = new String();
		String binaryChiper = new String();
		String plainChiper = new String();
		String chipperAllText = new String();

		//plainText to binaryText
		try{

			//Reading keyText.txt file
			//get plain text from file
			fis = new FileInputStream("keyText.txt");
			// Get the object of DataInputStream
			dis = new DataInputStream(fis);
			br = new BufferedReader(new InputStreamReader(dis));

			//System.out.println("\nKey : ");
			while ((plainKey = br.readLine()) != null)   {
				//System.out.println(plainKey);
				//plainKey to binaryKey
				for(int index=0; index < plainKey.length(); index++){
					binaryKey = binaryKey + xor.charToBinary(plainKey.charAt(index));
				}
			}

			//System.out.println("Binary Key : ");
			//System.out.println(binaryKey+"\n");

			//Encripting Process and Writing to File
			//Writing Chipertext File
			file= new File("chiperText.txt");
			file.createNewFile();
			pw = new PrintWriter(new FileWriter(file));

			//get plain text from file
			fis = new FileInputStream("plainText.txt");
			// Get the object of DataInputStream
			dis = new DataInputStream(fis);
			br = new BufferedReader(new InputStreamReader(dis));

			//System.out.println("\nPlain Text : ");
			while ((plainText = br.readLine()) != null)   {
				binaryText="";
				plainChiper="";
				for(int index=0; index < plainText.length(); index++){
					binaryText = binaryText + xor.charToBinary(plainText.charAt(index));
				}
				//System.out.println(plainText);
				binaryChiper = xor.XORKriptografi(binaryText,binaryKey);
				for(int index=0; index < binaryChiper.length(); index=index+8){
					plainChiper=plainChiper+xor.binaryToAsciiToString(binaryChiper.substring(index,index+8));
				}
				pw.println(plainChiper);
				chipperAllText = chipperAllText + plainChiper + "\n" ;

			}
			pw.flush();
			//System.out.println("Binary Plain Text :");
			//System.out.println(binaryText+"\n");

			//Print Chippertext
			System.out.println("\nChippertext : ");
			System.out.println(chipperAllText+"\n");

		}catch (Exception e){
		  System.err.println("Error: " + e.getMessage());
		}
	}
}

3. File ChiperToPlain.java

import java.io.*;
import java.util.*;

class ChiperToPlain{
	public static void main(String[] args)
	{
		XOREnkripsi xor = new XOREnkripsi();

		FileInputStream fis;
		PrintWriter pw;
		File file;

		DataInputStream dis;
		BufferedReader br;

		String plainText = new String();
		String plainKey = new String();
		String binaryText = new String();
		String binaryKey = new String();
		String binaryChiper = new String();
		String plainChiper = new String();
		String chipperAllText = new String();
		String plainTextAfterEnkripsi = new String();

		//plainText to binaryText
		try{

			//Reading keyText.txt file
			//get plain text from file
			fis = new FileInputStream("keyText.txt");
			// Get the object of DataInputStream
			dis = new DataInputStream(fis);
			br = new BufferedReader(new InputStreamReader(dis));

			//System.out.println("\nKey : ");
			while ((plainKey = br.readLine()) != null)   {
				//System.out.println(plainKey);
				//plainKey to binaryKey
				for(int index=0; index < plainKey.length(); index++){
					binaryKey = binaryKey + xor.charToBinary(plainKey.charAt(index));
				}
			}

			//Reading from file and Descripting Process
			//get chiptertext from file
			fis = new FileInputStream("chiperText.txt");
			// Get the object of DataInputStream
			dis = new DataInputStream(fis);
			br = new BufferedReader(new InputStreamReader(dis));

			//get chippertext from file
			System.out.println("\nPlain Text After Enkripsi : ");
			while ((plainChiper = br.readLine()) != null)   {
				plainTextAfterEnkripsi="";
				binaryChiper="";
				//chiperText to chiperBinary
				for(int index=0; index < plainChiper.length(); index++){
					binaryChiper = binaryChiper + xor.charToBinary(plainChiper.charAt(index));
				}
				//System.out.println(binaryChiper);
				binaryText =  xor.XORKriptografi(binaryChiper,binaryKey);
				for(int index=0; index < binaryText.length(); index=index+8){
					plainTextAfterEnkripsi=plainTextAfterEnkripsi+xor.binaryToAsciiToString(binaryText.substring(index,index+8));
				}
				System.out.println(plainTextAfterEnkripsi);
			}
		}catch (Exception e){
		  System.err.println("Error: " + e.getMessage());
		}
	}
}

Langkah-langkah Penggunaan Aplikasi

Enkripsi XOR

1. Buat file bernama “plainText.txt”


2. Buat sebuah file bernama “keyText.txt”


3. Jalankan program “java PlainToChiper”

4. Hasil yang didapat sebuah file bernama “chiperText.txt”

Dekripsi XOR

1. Jalankan program “java ChiperToPlain”