Advertisement
Poitiers732

Enigma

Apr 25th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4.  
  5. public class enigma3 {
  6.  
  7.     public static void main(String[] args) {
  8.         //String str = new String("");
  9.         String str = new String("I have to put data");
  10.         int[] code =  generateRandomCode(str.length());
  11.         int[] code2 =  new int[]  {11, 9, 2, 16, 6, 7, 4, 5, 3, 12, 14, 10, 15, 17, 13, 0, 1, 8};
  12.         System.out.println("code: " + Arrays.toString(code));
  13.  
  14.         //decypher(cypher(str,code),code);
  15.         cypher(str, code2);
  16.         decypher("\u0080)j\u0084&{zjd\u0080rzpr-I!w",code2);
  17.     }
  18.  
  19.     public static String cypher(String str, int[] iArr){
  20.         char[] cArr = str.toCharArray();
  21.         char[] result = new char[cArr.length];
  22.         int ascii;
  23.  
  24.         for (int i = 0; i < cArr.length ; i++) {
  25.             result [i] = cArr[ iArr[i] ];
  26.             ascii = (int) result [i];
  27.             ascii += iArr[i];
  28.             result [i] = (char) ascii;
  29.         }
  30.         System.out.println();
  31.         System.out.println("cyphered: " + new String(result));
  32.  
  33.         return new String(result);
  34.     }
  35.  
  36.     public static String decypher(String str, int[] iArr){
  37.         char[] cArr = str.toCharArray();
  38.         char[] result = new char[cArr.length];
  39.         int ascii;
  40.  
  41.         for (int i = 0; i < cArr.length ; i++) {
  42.             result[ iArr[i] ] = cArr[i];
  43.             ascii = (int) result [iArr[i]];
  44.             ascii -= iArr[i];
  45.             result [iArr[i]] = (char) ascii;
  46.         }
  47.         System.out.println();
  48.         System.out.println("decyphered: " + new String(result));
  49.  
  50.         return new String(result);
  51.     }
  52.  
  53.     public static int[] generateRandomCode(int length){
  54.         int[] iArr = new int[length];
  55.         Set<Integer> generated = new LinkedHashSet<Integer>();
  56.         Random rng = new Random();
  57.  
  58.         while (generated.size() < length) {
  59.             Integer next = rng.nextInt(length) + 0;
  60.             generated.add(next);
  61.         }
  62.  
  63.         List<Integer> list = new ArrayList<Integer>(generated);
  64.  
  65.         for (int i = 0; i < length; i++) {
  66.             iArr[i] = list.get(i);
  67.         }
  68.         return iArr;
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement