package twofish;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
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.InvalidKeyException;
import java.util.logging.Level;
import java.util.logging.Logger;
import twofish.Twofish_Algorithm;
public class TWOFISH {
static String[]arx;
static int lop;
//String getDoc(String al){
//String h="";
// File file = null;
// WordExtractor extractor = null;
// try {
// file = new File(al);
// FileInputStream fis = new FileInputStream(file.getAbsolutePath());
// HWPFDocument document = new HWPFDocument(fis);
// extractor = new WordExtractor(document);
// String[] fileData = extractor.getParagraphText();
// for (int i = 0; i < fileData.length; i++){
// if (fileData[i] != null)
// h+=fileData[i];
// }
// }
// catch (Exception exep){
// exep.printStackTrace();
// }
// return h;
//}
String getDefault(String fileName) throws IOException{
String gab="";
Path path = Paths.get(fileName);
byte[] data = Files.readAllBytes(path); //membaca semua bytes yang ada di object
for(int i=0; i < data.length; i++) {
String al = String.valueOf(data[i] & 0xff);
int ii=Integer.parseInt(al);
char c=(char)ii;
gab=gab+c;
}
BufferedReader be = null;
String current;
be = new BufferedReader(new FileReader(fileName));
while ((current = be.readLine()) != null) {
gab += current;
}
return gab;
}
static String getNF(String fileName){
int dot = fileName.lastIndexOf(".");
int sep = fileName.replace("\\", "/").lastIndexOf("/");
String NF=fileName.substring(sep + 1, dot);
String extendeed= fileName.substring(dot + 1);
NF=NF+"."+extendeed;
return NF;
}
static String getExt(String fileName){
int dot = fileName.lastIndexOf(".");
String extendeed= fileName.substring(dot + 1);
return extendeed;
}
public static void main(String[] args) throws IOException {
TWOFISH x=new TWOFISH();
String ext=".pdf";
String fileName="D:\\wav\\contoh"+ext;
String PASS = "1234567890";
String NF=TWOFISH.getNF(fileName);
String pathnya=fileName.replaceAll(NF, "");
System.out.println("fileName="+fileName);
System.out.println("NF="+NF);
System.out.println("pathnya="+pathnya);
String gab="";
String EXT=TWOFISH.getExt(fileName);
System.out.println("Proses="+EXT);
if(EXT.equalsIgnoreCase("doc")){
gab=x.getDefault(fileName);//getDoc
}
else if(EXT.equalsIgnoreCase("rtf")){
gab=x.getDefault(fileName);
}
else{
gab=x.getDefault(fileName);
}
String enNF=pathnya+"en_"+NF;
System.out.println("enNF="+enNF);
String strEnkrip=TWOFISH.myEnkrip(gab,PASS);
saveString(strEnkrip,enNF);
String dekNF=pathnya+"dec_"+NF;
System.out.println("dekNF="+dekNF);
String strDekrip=TWOFISH.myDekrip(strEnkrip,PASS,dekNF);
saveString(strDekrip,dekNF);
}//main
static String myEnkrip(String PTEXT,String PASS){
int p=PTEXT.length();
int split=16;
if(p>split){
int sisa=p%16;
lop=p/16;
if(sisa>0){lop=lop+1;}
arx=new String[lop];
int awal=0;
for(int l=0;l<lop-1;l++){
int next=(awal+split);
arx[l]=PTEXT.substring(awal,next);
// System.out.println(l+"="+arx[l]+"=="+awal+","+(next));
awal+=split;
}
if(sisa>0){
arx[lop-1]=PTEXT.substring(awal,awal+sisa);
}
}//>16
String gabEnkrip="";
for(int i=0;i<lop;i++){
String cip=TWOFISH.enkrip(arx[i], PASS);
gabEnkrip+=cip;
}
return gabEnkrip;
}
static String myDekrip(String gabEnkrip,String PASS,String newfileName){
String gabDek="";
int split=16;
int pdek=gabEnkrip.length();
int lopdek=pdek/split;
int awaldek=0;
String[]ard=new String[lopdek];
for(int l=0;l<lopdek-0;l++){
int next=awaldek+split;
ard[l]=gabEnkrip.substring(awaldek,next);
String dek=TWOFISH.dekrip(ard[l], PASS);
//System.out.println(l+"="+ard[l]+"=="+awaldek+","+next+"=="+dek);
gabDek+=dek.trim();
awaldek+=split;
}
return gabDek;
}
static String enkrip(String PTEXT,String PASS){
byte tempKey[] = PASS.getBytes();
byte tempPlainText[] = null;
try{
tempPlainText= PTEXT.getBytes();
}
catch(Exception ee){}
byte key[] = new byte[32];
byte plainText[] = new byte[128];
int i;
for(i=0; i<32;i++){
if(i<tempKey.length) key[i] = tempKey[i];
else key[i] = (byte)0;
}
for(i=0; i<128;i++){
if(i<tempPlainText.length) {
try{
plainText[i] = tempPlainText[i];
}
catch(Exception ee){}
}
else plainText[i] = (byte)0;
}
Object K = null;
try {
K = Twofish_Algorithm.makeKey(key);
} catch (InvalidKeyException ex) {
Logger.getLogger(TWOFISH.class.getName()).log(Level.SEVERE, null, ex);
}
byte[] ct = Twofish_Algorithm.blockEncrypt(plainText, 0, K);
String cipherText="";
for( i=0;i<ct.length;i++){
cipherText+=String.valueOf((char)ct[i]);
}
return cipherText;
}
static String dekrip(String cipherText,String PASS){
byte tempKey2[] = PASS.getBytes();
byte key2[] = new byte[32];
for(int i=0; i<32;i++){
if(i<tempKey2.length) key2[i] = tempKey2[i];
else key2[i] = (byte)0;
}
char[]arh=cipherText.toCharArray();
byte[]cth=new byte[arh.length];
for(int i=0;i<arh.length;i++){
cth[i]=(byte)(int)arh[i];
}
Object K2 = null;
try {
K2 = Twofish_Algorithm.makeKey(key2);
} catch (InvalidKeyException ex) {
Logger.getLogger(TWOFISH.class.getName()).log(Level.SEVERE, null, ex);
}
byte[] cpt = Twofish_Algorithm.blockDecrypt(cth, 0, K2);//ct
String ot = new String(cpt);
return ot;
}
private void saveData(String dataEncrypt,String mpath) {
String kompress=dataEncrypt;//lz.compress(dataEncrypt, null);
char[]ar2=kompress.toCharArray();
byte[]data3=new byte[ar2.length];
for(int i=0;i<ar2.length;i++){
data3[i]=(byte)(int)ar2[i];
}
try{
this.saveByte(data3,mpath);
} catch (Exception e) {
e.printStackTrace();
}
}
private void saveByte(byte[] dataDecrypt,String fileNameEncrypt) throws IOException {
Path path = Paths.get(fileNameEncrypt);
Files.write(path, dataDecrypt); //creates, overwrites
System.out.println("File Berhasil di dekripsi ! lihat di : " +fileNameEncrypt);
}
static void saveString(String dataEncrypt,String namaFile) {
try {
String content = dataEncrypt;
File file = new File(namaFile);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


Tidak ada komentar:
Posting Komentar