import java.io.*; public class StreamEncrypt{ private BufferedReader br; private File files; private FileInputStream baca; private FileOutputStream tulis; private byte[] temp, dataIn; public StreamEncrypt(){ } private void init() throws IOException{ temp = new byte[(int)files.length()]; dataIn = new byte[(int)files.length()]; this.readFile(); return; } private String getInput() throws IOException{ br = new BufferedReader(new InputStreamReader(System.in)); return br.readLine(); } private boolean checkFile(File file){ return ((file.exists()) & (file.isFile())); } private void readFile() throws IOException{ baca = new FileInputStream(files); int i, j = 0, k = 0; System.out.print("reading "+ File.separator + files.getName()+ "..."); if(files.canRead()){ while((i = baca.read()) != -1){ temp[j++] = (byte) i; dataIn[k++] = (byte) i; } baca.close(); System.out.print("done\n"); this.exe(); return; } return; } private void writeFile(byte[] dataOut) throws IOException{ String fileName = files.getName(); String path = ""; File file; System.out.println("select location to write file "+ fileName +" on:"); path = this.getInput() + File.separator + fileName; file = new File(path); if(file.createNewFile()){ if(this.checkFile(file)){ if(file.canWrite()){ tulis = new FileOutputStream(file); System.out.print("writing "+ File.separator + fileName +"..."); tulis.write(dataOut); tulis.close(); if(files.delete()) System.out.print("done\n"); return; } } } return; } public void exe() throws IOException{ System.out.print("Encrypting..."); Crypt cr = new Crypt(temp); System.out.print("done\n"); this.writeFile(cr.getResult()); return; } public static void main(String[] args){ StreamEncrypt se = new StreamEncrypt(); String path = ""; System.out.println("Select a file:"); try{ path = se.getInput(); se.files = new File(path); if(se.checkFile(se.files)) se.init(); else System.out.println(se.checkFile(se.files)); } catch(IOException ie){ } } class Crypt extends Thread{ byte[] data, result; static final byte key = 69; Crypt(byte[] data){ result = new byte[(int) data.length]; this.data = new byte[(int) data.length]; this.data = data; start(); } public void run(){ int i = 0; while(i<data.length){ result[i] = ((byte) (data[i] ^ key)); i++; } } public byte[] getResult(){ return result; } } }
Pages
Sunday, August 25, 2013
Membuat Aplikasi Enkripsi Sederhana Menggunakan Java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment