Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class Crypto {
- public static String encrypt(String plainText, String key) {
- char[] encText = plainText.toCharArray();
- Random r = new Random(key.length());
- Random[] randoms = new Random[key.length()];
- int i = -1;
- while (++i < randoms.length)
- randoms[i] = new Random(key.charAt(i) + r.nextInt());
- i = -1;
- while (++i < encText.length)
- encText[i] += randoms[i % randoms.length].nextInt(2 * Short.MAX_VALUE);
- return new String(encText);
- }
- public static String decrypt(String encText, String key) {
- char[] plainText = encText.toCharArray();
- Random r = new Random(key.length());
- Random[] randoms = new Random[key.length()];
- int i = -1;
- while (++i < randoms.length)
- randoms[i] = new Random(key.charAt(i) + r.nextInt());
- i = -1;
- while (++i < plainText.length)
- plainText[i] -= randoms[i % randoms.length].nextInt(2 * Short.MAX_VALUE);
- return new String(plainText);
- }
- //Demo
- public static void main(String[] args) {
- 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.";
- String key1 = "01234567";
- String encrypted1;
- encrypted1 = encrypt(text, key1);
- System.out.println("Encrypted text: '" + encrypted1 + "'");
- // Let's attack!
- int keylen = key1.length(); // assuming we've already guessed or just know the key length
- Random r = new Random(keylen);
- Random randoms[] = new Random[keylen];
- StringBuilder key = new StringBuilder(keylen);
- for (int i=0; i<keylen; ++i) {
- key.append("\0");
- }
- for (int i=0; i<keylen; ++i) {
- char winner = '\0';
- int score = 0;
- for (char guess = 0; guess < Short.MAX_VALUE; ++guess) {
- key.replace(i, i+1, Character.toString(guess));
- String decrypted = decrypt(encrypted1, key.toString());
- StringBuilder tmp = new StringBuilder(decrypted.length()/keylen);
- for (int j=0; j<decrypted.length()/keylen; j += keylen) {
- tmp.append(Character.toString(decrypted.charAt(i+j)));
- }
- int _score = calcScore(tmp.toString());
- if (_score > score) {
- score = _score;
- winner = guess;
- }
- }
- key.replace(i, i+1, Character.toString(winner));
- }
- System.out.println("Decrypted:");
- System.out.println(decrypt(encrypted1, key.toString()));
- }
- public static int calcScore(String s) {
- int score = 0;
- for (int i=0; i<s.length(); ++i) {
- char ch = s.charAt(i);
- short c = (short)ch;
- if (c < 32 || c >= 127) {
- return -1;
- } else if (ch == 'e') {
- score += 12;
- } else if (ch == 't') {
- score += 10;
- } else if (ch == 'a') {
- score += 9;
- } else if (ch == 'o') {
- score += 8;
- } else if (ch == 'i' || ch == 'n') {
- score += 7;
- } else if (ch == 's' || ch == 'h' || ch == 'r') {
- score += 6;
- } else if (Character.isLowerCase(ch)) {
- score += 4;
- } else if (Character.isUpperCase(ch)) {
- score += 2;
- } else {
- score += 1;
- }
- }
- return score;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement