Advertisement
pendekar_langit

Encript

May 13th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package Utils;
  7.  
  8. import engine.Message;
  9. import java.io.IOException;
  10. import java.nio.charset.StandardCharsets;
  11. import java.util.ArrayList;
  12. import java.util.Arrays;
  13. import java.util.Collections;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import sun.misc.BASE64Encoder;
  17. import sun.misc.BASE64Decoder;
  18.  
  19. /**
  20.  *
  21.  * @author root
  22.  */
  23. public class Encrypt {
  24.     private final Message message = new Message();
  25.     private HashMap keyEncode;
  26.     private HashMap keyDecode;
  27.     public Encrypt(){
  28.         List <String> besar = new ArrayList <> ();
  29.         for (char b = 'A'; b <= 'Z'; b++){
  30.             besar.add(String.valueOf(b));
  31.         }
  32.         List<String> angka = new ArrayList<>();
  33.         for(int a = 0; a <= 9; a++){
  34.             angka.add(String.valueOf(a));
  35.         }
  36.         List<String> kecil = new ArrayList<>();
  37.         for(char c = 'a'; c <= 'z'; c++){
  38.             kecil.add(String.valueOf(c));
  39.         }
  40.         String symbol = "\"`~!@#$%^&*()_-=+[]{}\\|:;<>,.?/ ";
  41.         List<String> a = new ArrayList<>();
  42.         a.addAll(kecil);
  43.         Collections.reverse(besar);
  44.         a.addAll(besar);
  45.         a.addAll(angka);
  46.         a.addAll(Arrays.asList(symbol.split("")));
  47.         List<String> chiper = new ArrayList<>();
  48.         for(int d = 0; d <= 94; d++){
  49.             chiper.add(String.valueOf(d));
  50.         }
  51.         Collections.reverse(chiper);
  52.         keyDecode = new HashMap();
  53.         for(int x = 0; x < chiper.size(); x++){
  54.             keyDecode.put(chiper.get(x), a.get(x));
  55.         }        
  56.         keyEncode = new HashMap();
  57.         for(int x = 0; x < chiper.size(); x++){
  58.             keyEncode.put(a.get(x), chiper.get(x));
  59.         }
  60.     }
  61.     public String encode(String plain){
  62.         String result = "";
  63.         for(int i = 0; i < plain.length(); i++){
  64.             result += keyEncode.get(String.valueOf(plain.charAt(i))).toString()+",";
  65.         }
  66.         return encodeBase64(new StringBuilder(result).reverse().toString());
  67.     }
  68.     public String decode(String chiper) throws IOException{
  69.         String result = "";
  70.         String[] text = new StringBuilder(decodeBase64(chiper)).reverse().toString().split(",");
  71.         for(int i = 0; i < text.length; i++){
  72.             result += keyDecode.get(String.valueOf(text[i])).toString();
  73.         }
  74.         return result;
  75.     }
  76.  
  77.    private static String encodeBase64(String value) {
  78.       final BASE64Encoder encoder = new BASE64Encoder();
  79.       return new StringBuilder(encoder.encode(value.getBytes(StandardCharsets.UTF_8))).reverse().toString();
  80.    }
  81.  
  82.    private static String decodeBase64(String value) throws IOException {
  83.        final BASE64Decoder decoder = new BASE64Decoder();
  84.        return new String(decoder.decodeBuffer(new StringBuilder(value).reverse().toString()), StandardCharsets.UTF_8);
  85.    }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement