Kamis, 15 Desember 2016

Source Code Kriptografi


String doc2String(String fileName) {
        BufferedReader be = null;
        String data = "";
                 
       try {
            String current;
            int s;
            be = new BufferedReader(new FileReader(fileName));
            while ((current = be.readLine()) != null) {
                data +=  current;
            }
         }
        catch(Exception ee){}
     return data;
}

String doc2String(String fileName) throws IOException{
String gab="";
Path path = Paths.get(fileName);
byte[] data = Files.readAllBytes(path);
    for(int i=0; i < data.length; i++) {
        char c=(char)(int)data[i];
        gab=gab+c;
    }
 return gab;
}

byte[]doc2Byte(String fileName){
        Path path = Paths.get(fileName);
        byte[] data = Files.readAllBytes(path); //membaca semua bytes yang ada di object
                     
        String[] data1 = new String[data.length];
        for(int i=0; i < data.length; i++) {
            data1[i] = String.valueOf(data[i] & 0xff); //unsigned integer
         }
return data1;
}

byte[] doc2Byte(String al)throws IOException{
        Path path = Paths.get(al);
        byte[] data = Files.readAllBytes(path);
      return data;
}


+++++++++++++++++++++++++++++++++++

void saveByte(byte[] data, String namaFile) throws IOException {
       Path path = Paths.get(namaFile);
       Files.write(path, dataDecrypt);
     }

void saveString(String data,String namaFile) {
       try {
           String content = data;
           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();
       }
   }


===========================

String byte2String(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;
}

++++++++++++++++++++++++++++++


String getNamaFile(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;
}

String getExt(String fileName){
    int dot = fileName.lastIndexOf(".");
    String extendeed= fileName.substring(dot + 1);
    return extendeed;
}


boolean cekFile(String nf){
    boolean sudah=true;
        File file = new File(nf);
        if (!file.exists()) {
         sudah=false;
         }
    return sudah;
}


boolean cekFileDel(String nf){
    boolean sudah=false;
        File file = new File(nf);
        if (file.exists()) {
         file.delete();
         sudah=true;
         }
    return sudah;
}

++++++++++++++
 String sTB(byte[]result){
            String str = Base64.encode(result);//encodeBase64String
            return str;
        }

byte [] sTB2(String s){
            char [] x = s.toCharArray();
            byte [] hs = new byte[x.length];
            for (int i = 0;i<x.length;i++){
                hs [i] = (byte) (int)x[i];
            }
            return hs;
 }

byte[] sTB (String s) throws Base64DecodingException{
            byte [] hs = Base64.decode(s);
            return hs;
     }

byte[]btS(String content) throws Base64DecodingException{
        byte[]dika=Base64.decode(content);
        return dika;
    }
     
String bTS(byte[]result){
            String str = Base64.encode(result);//encodeBase64String
            return str;
        }

   
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