Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 Utils;
- import engine.Message;
- import java.io.IOException;
- import java.nio.charset.StandardCharsets;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.List;
- import sun.misc.BASE64Encoder;
- import sun.misc.BASE64Decoder;
- /**
- *
- * @author root
- */
- public class Encrypt {
- private final Message message = new Message();
- private HashMap keyEncode;
- private HashMap keyDecode;
- public Encrypt(){
- List <String> besar = new ArrayList <> ();
- for (char b = 'A'; b <= 'Z'; b++){
- besar.add(String.valueOf(b));
- }
- List<String> angka = new ArrayList<>();
- for(int a = 0; a <= 9; a++){
- angka.add(String.valueOf(a));
- }
- List<String> kecil = new ArrayList<>();
- for(char c = 'a'; c <= 'z'; c++){
- kecil.add(String.valueOf(c));
- }
- String symbol = "\"`~!@#$%^&*()_-=+[]{}\\|:;<>,.?/ ";
- List<String> a = new ArrayList<>();
- a.addAll(kecil);
- Collections.reverse(besar);
- a.addAll(besar);
- a.addAll(angka);
- a.addAll(Arrays.asList(symbol.split("")));
- List<String> chiper = new ArrayList<>();
- for(int d = 0; d <= 94; d++){
- chiper.add(String.valueOf(d));
- }
- Collections.reverse(chiper);
- keyDecode = new HashMap();
- for(int x = 0; x < chiper.size(); x++){
- keyDecode.put(chiper.get(x), a.get(x));
- }
- keyEncode = new HashMap();
- for(int x = 0; x < chiper.size(); x++){
- keyEncode.put(a.get(x), chiper.get(x));
- }
- }
- public String encode(String plain){
- String result = "";
- for(int i = 0; i < plain.length(); i++){
- result += keyEncode.get(String.valueOf(plain.charAt(i))).toString()+",";
- }
- return encodeBase64(new StringBuilder(result).reverse().toString());
- }
- public String decode(String chiper) throws IOException{
- String result = "";
- String[] text = new StringBuilder(decodeBase64(chiper)).reverse().toString().split(",");
- for(int i = 0; i < text.length; i++){
- result += keyDecode.get(String.valueOf(text[i])).toString();
- }
- return result;
- }
- private static String encodeBase64(String value) {
- final BASE64Encoder encoder = new BASE64Encoder();
- return new StringBuilder(encoder.encode(value.getBytes(StandardCharsets.UTF_8))).reverse().toString();
- }
- private static String decodeBase64(String value) throws IOException {
- final BASE64Decoder decoder = new BASE64Decoder();
- return new String(decoder.decodeBuffer(new StringBuilder(value).reverse().toString()), StandardCharsets.UTF_8);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement