Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Find passwords. Which text strings preoduce the wanted numbers?
- *
- * ASCII coding: All characters are represented by a number, (32-127) The
- * American standard (without åäö) is used in this lab.
- */
- public class Crack {
- // kungen == 1643565271408557312
- public static void main(String[] args) {
- // praktisk, för att inte datorns ska "dö" när ni kör koden.
- Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
- System.out.println("ASCII-tabellen");
- for (int i = 32; i < 128; i++) {
- System.out.printf("%3d ", i);
- }
- System.out.println("");
- for (int i = 32; i < 128; i++) {
- System.out.print(" " + (char) i + " ");
- }
- System.out.println("");
- System.out.println(easypz("3479515037239494400"));
- System.out.println("Uppgift 0:");
- System.out.println("Hitta den textstäng som easy() kodar till 75097108108101046");
- System.out.println("");
- System.out.println("Uppgift 1:");
- System.out.println("Hitta den textstäng som first() kodar till 3700833");
- System.out.println("");
- System.out.println("Uppgift 2:");
- System.out.println("Hitta den textstäng som hard() kodar till 3479515037239494400");
- }
- public static long easy(String pwd) {
- if (pwd == null) {
- return 0;
- }
- long code = 0;
- if (pwd.length() < 1) {
- return 0;
- }
- for (int i = 0; i < pwd.length(); i++) {
- char c = pwd.charAt(i);
- code = 1000 * code + c;
- }
- return code;
- }
- public static long easypz(String pwd) {
- String Password = pwd;
- int x1 = 0;
- int x2 = 0;
- int x3 = 0;
- int x4 = 0;
- int x5 = 0;
- int x6 = 0;
- int x7 = 0;
- int x8 = 0;
- String alphabet = new String("!#¤%&/()*+´-,.0123456789:;><?@QWERTYUIOPÅÄÖLKJHGFDSAZXCVBNMqwertyuiopöäålkjhgfdsazxcvbnm"); //39
- char[] arr = alphabet.toCharArray();
- while (true) {
- String guess = null;
- if (x1 > 0) {
- guess = new String(arr[x1] + "");
- }
- if (x2 > 0) {
- guess = new String(arr[x1] + "" + arr[x2]);
- }
- if (x3 > 0) {
- guess = new String(arr[x1] + "" + arr[x2] + "" + arr[x3]);
- }
- if (x4 > 0) {
- guess = new String(arr[x1] + "" + arr[x2] + "" + arr[x3] + "" + arr[x4]);
- }
- if (x5 > 0) {
- guess = new String(arr[x1] + "" + arr[x2] + "" + arr[x3] + "" + arr[x4] + "" + arr[x5]);
- }
- if (x6 > 0) {
- guess = new String(arr[x1] + "" + arr[x2] + "" + arr[x3] + "" + arr[x4] + "" + arr[x5] + "" + arr[x6]);
- }
- if (x7 > 0) {
- guess = new String(arr[x1] + "" + arr[x2] + "" + arr[x3] + "" + arr[x4] + "" + arr[x5] + "" + arr[x6] + "" + arr[x7]);
- }
- if (x8 > 0) {
- guess = new String(arr[x1] + "" + arr[x2] + "" + arr[x3] + "" + arr[x4] + "" + arr[x5] + "" + arr[x6] + "" + arr[x7] + "" + arr[x8]);
- }
- x1++;
- if (x1 == alphabet.length()) {
- x1 = 0;
- x2 += 1;
- }
- if (x2 == alphabet.length()) {
- x2 = 0;
- x3 += 1;
- }
- if (x3 == alphabet.length()) {
- x3 = 0;
- x4 += 1;
- }
- if (x4 == alphabet.length()) {
- x4 = 0;
- x5 += 1;
- }
- if (x5 == alphabet.length()) {
- x5 = 0;
- x6 += 1;
- }
- if (x6 == alphabet.length()) {
- x6 = 0;
- x7 += 1;
- }
- if (x7 == alphabet.length()) {
- x7 = 0;
- x8 += 1;
- }
- if (x8 == alphabet.length()) {
- x8 = 0;
- System.out.println(guess);
- return 0;
- }
- //System.out.println(guess);
- long code = hard(guess);
- //System.out.println(code);
- if(code == 3479515037239494400L){
- System.out.println(guess);
- break;
- }
- }
- return 0;
- }
- //////////////////////////////////////////
- public static long first(String pwd) {
- if( pwd==null)return 0;
- long code = 1;
- for (int i = 0; i < pwd.length(); i++) {
- char c = pwd.charAt(i);
- code = 128 * code + c;
- }
- return code;
- }
- /////////////////////////////////////////////
- public static long hard(String pwd) {
- if(pwd==null)return 0;
- int[] primes = {2, 3, 5, 7, 11, 13, 17, 19};
- long code = 1;
- int max = 5;
- if (pwd.length() < 5) {
- max = pwd.length();
- }
- for (int i = 0; i < max; i++) {
- char c = pwd.charAt(i);
- code = (primes[i] + c * c) * primes[i] * code;
- }
- return code;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement