Advertisement
Guest User

Untitled

a guest
Oct 30th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Crypto {
  4.     public static String encrypt(String plainText, String key) {
  5.         char[] encText = plainText.toCharArray();
  6.         Random r = new Random(key.length());
  7.         Random[] randoms = new Random[key.length()];
  8.         int i = -1;
  9.         while (++i < randoms.length)
  10.             randoms[i] = new Random(key.charAt(i) + r.nextInt());
  11.         i = -1;
  12.         while (++i < encText.length)
  13.             encText[i] += randoms[i % randoms.length].nextInt(2 * Short.MAX_VALUE);
  14.         return new String(encText);
  15.     }
  16.  
  17.     public static String decrypt(String encText, String key) {
  18.         char[] plainText = encText.toCharArray();
  19.         Random r = new Random(key.length());
  20.         Random[] randoms = new Random[key.length()];
  21.         int i = -1;
  22.         while (++i < randoms.length)
  23.             randoms[i] = new Random(key.charAt(i) + r.nextInt());
  24.         i = -1;
  25.         while (++i < plainText.length)
  26.             plainText[i] -= randoms[i % randoms.length].nextInt(2 * Short.MAX_VALUE);
  27.         return new String(plainText);
  28.     }
  29.  
  30.     //Demo
  31.     public static void main(String[] args) {
  32.         String text = "This is just some sample text to make the attack easy. I like using simple letters and stuff. This scoring system is very simple. A smarter scoring system would be able to decrypt anything.";
  33.         String key1 = "01234567";
  34.         String encrypted1;
  35.         encrypted1 = encrypt(text, key1);
  36.         System.out.println("Encrypted text: '" + encrypted1 + "'");
  37.  
  38.         // Let's attack!
  39.         int keylen = key1.length(); // assuming we've already guessed or just know the key length
  40.         Random r = new Random(keylen);
  41.         Random randoms[] = new Random[keylen];
  42.         StringBuilder key = new StringBuilder(keylen);
  43.         for (int i=0; i<keylen; ++i) {
  44.             key.append("\0");
  45.         }
  46.  
  47.         for (int i=0; i<keylen; ++i) {
  48.             char winner = '\0';
  49.             int score = 0;
  50.             for (char guess = 0; guess < Short.MAX_VALUE; ++guess) {
  51.                 key.replace(i, i+1, Character.toString(guess));
  52.                 String decrypted = decrypt(encrypted1, key.toString());
  53.                 StringBuilder tmp = new StringBuilder(decrypted.length()/keylen);
  54.                 for (int j=0; j<decrypted.length()/keylen; j += keylen) {
  55.                     tmp.append(Character.toString(decrypted.charAt(i+j)));
  56.                 }
  57.                 int _score = calcScore(tmp.toString());
  58.                 if (_score > score) {
  59.                     score = _score;
  60.                     winner = guess;
  61.                 }
  62.             }
  63.             key.replace(i, i+1, Character.toString(winner));
  64.         }
  65.  
  66.         System.out.println("Decrypted:");
  67.         System.out.println(decrypt(encrypted1, key.toString()));
  68.  
  69.     }
  70.  
  71.     public static int calcScore(String s) {
  72.         int score = 0;
  73.         for (int i=0; i<s.length(); ++i) {
  74.             char ch = s.charAt(i);
  75.             short c = (short)ch;
  76.             if (c < 32 || c >= 127) {
  77.                 return -1;
  78.             } else if (ch == 'e') {
  79.                 score += 12;
  80.             } else if (ch == 't') {
  81.                 score += 10;
  82.             } else if (ch == 'a') {
  83.                 score += 9;
  84.             } else if (ch == 'o') {
  85.                 score += 8;
  86.             } else if (ch == 'i' || ch == 'n') {
  87.                 score += 7;
  88.             } else if (ch == 's' || ch == 'h' || ch == 'r') {
  89.                 score += 6;
  90.             } else if (Character.isLowerCase(ch)) {
  91.                 score += 4;
  92.             } else if (Character.isUpperCase(ch)) {
  93.                 score += 2;
  94.             } else {
  95.                 score += 1;
  96.             }
  97.         }
  98.         return score;
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement