/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package enkripsirc6;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class appRC4 {
private static String algorithm = "RC4";
public static void main(String[]args){
appRC4 x=new appRC4();
x.mulai();
}
void mulai(){
try {
String password="1234567890";
String path=System.getProperty("user.dir")+"\\tesdoc\\";
System.out.println("path="+path);
String pathFile=path+"tesdoc.docx";
File fpathFile=new File(pathFile);
String nf=fpathFile.getName();//tesdoc.doc
String absnf=fpathFile.getParent();//nama folder
appRC4 x=new appRC4();
byte[]doc2Byte=x.doc2Byte(pathFile);
String kalisidoc=x.byte2String(doc2Byte);
String nfEn=absnf+"\\EN-"+nf;
byte[] encrypted = x.encrypt(kalisidoc, password);
x.saveByte(encrypted,nfEn);
//+++++++++++++++++++++++++++++++++++++
//Baca FIle Dekrip
String passworddek="1234567890";
// String pilihfile="";//D:\foldrer/a.doc
// String saveke="";//D:\skripsi\
// File ff=new File(pilihfile);
// String NFs=ff.getName();
// pathFileEn=saveke+"\\EN-"+NFs;
String pathFileEn=nfEn;
File fpathFileDek=new File(pathFileEn);
String nfdec=fpathFileDek.getName();//tesdoc.doc
String absnfdec=fpathFileDek.getParent();
appRC4 xx=new appRC4();
byte[]doc2ByteDek=xx.doc2Byte2(pathFileEn);
//String kalimatdek=xx.byte2String(doc2ByteDek);
String nfDek=absnfdec+"\\DEK-"+nfdec;
String dekrip=xx.decrypt(doc2ByteDek,password);
byte[]doc2ByteXX=xx.string2Byte(dekrip);
xx.saveByte(doc2ByteXX,nfDek);
} catch (Exception ex) {
Logger.getLogger(appRC4.class.getName()).log(Level.SEVERE, null, ex);
}
}
void plays(){
try {
appRC4 xx=new appRC4();
String toEncrypt = "apa kabar bro";
System.out.println("Encrypting...");
byte[] encrypted = xx.encrypt(toEncrypt, "123");//password
System.out.println("Encrypted text : "+toEncrypt);
String hasil="";
String[] data1 = new String[encrypted.length];
for(int i=0; i < encrypted.length; i++) {
data1[i] = String.valueOf(encrypted[i] & 0xff); //unsigned integer
hasil+=data1[i];
}
System.out.println("Decrypting...");
String decrypted = xx.decrypt(encrypted, "123");//password
System.out.println("Decrypted text: " + decrypted);
} catch (Exception ex) {
Logger.getLogger(appRC4.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static byte[] encrypt(String toEncrypt, String key) throws Exception {
SecureRandom sr = new SecureRandom(key.getBytes());
KeyGenerator kg = KeyGenerator.getInstance(algorithm);
kg.init(sr);
SecretKey sk = kg.generateKey();
// create an instance of cipher
Cipher cipher = Cipher.getInstance(algorithm);
// initialize the cipher with the key
cipher.init(Cipher.ENCRYPT_MODE, sk);
// enctypt!
byte[] encrypted = cipher.doFinal(toEncrypt.getBytes());
return encrypted;
}
public static String decrypt(byte[] toDecrypt, String key) throws Exception {
// create a binary key from the argument key (seed)
SecureRandom sr = new SecureRandom(key.getBytes());
KeyGenerator kg = KeyGenerator.getInstance(algorithm);
kg.init(sr);
SecretKey sk = kg.generateKey();
// do the decryption with that key
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, sk);
byte[] decrypted = cipher.doFinal(toDecrypt);
return new String(decrypted);
}
String decrypt(String gabEnkrip, String PASS) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private String byteArrayToHexString(String b) {
StringBuffer sb = new StringBuffer(b.length() * 2);
for (int i = 0; i < b.length(); i++) {
char[]bb=b.toCharArray();////
int v = bb[i] & 0xff;
if (v < 16) {
sb.append('0');
}
sb.append(Integer.toHexString(v));
}
return sb.toString().toUpperCase();
}
byte[] doc2Byte2(String NF){
File f = new File(NF);
byte[] bit = new byte[(int)f.length()];
try{
FileInputStream fis = new FileInputStream(f);
fis.read(bit);
fis.close();
}catch(Exception exc){}
//hasil = DatatypeConverter.printBase64Binary(bit);
return bit;
}
byte[] doc2Byte(String NF){
Path path = Paths.get(NF);
byte[] data=null;
try {
data = Files.readAllBytes(path);
} catch (IOException ex) {
Logger.getLogger(appRC4.class.getName()).log(Level.SEVERE, null, ex);
}
return data;
}
String byte2String(byte[]data){
String gab="";
for(int i=0; i < data.length; i++) {
String al = String.valueOf(data[i]& 0xff ); // & 0xff unsigned integer
int ii=Integer.parseInt(al);
char c=(char)ii;
gab=gab+c;
}
return gab;
}
private void saveString(String dataString,String NF) {
try {
String content = dataString;
File file = new File(NF);
System.out.println(NF);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("File Berhasil di simpan ! lihat di : "+NF);
} catch (IOException e) {
e.printStackTrace();
}
}
private void saveByte(byte[] databyte,String NF) {
try {
Path path = Paths.get(NF);
Files.write(path, databyte); //creates, overwrites
System.out.println("File Berhasil di dekripsi ! lihat di : " + NF);
} catch (IOException ex) {
Logger.getLogger(appRC4.class.getName()).log(Level.SEVERE, null, ex);
}
}
String byte2String2(byte[]data){
String gab="";
for(int i=0; i < data.length; i++) {
char c=(char)(int)data[i];
gab=gab+c;
}
return gab;
}
byte[] string2Byte(String data){
char[]ar=data.toCharArray();
byte[]bit=new byte[ar.length];
for(int i=0; i < ar.length; i++) {
int c=(int)ar[i];
bit[i]=(byte)c;
}
return bit;
}
}
Tidak ada komentar:
Posting Komentar