Advertisement
Guest User

geylord

a guest
Dec 8th, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1.  
  2. /**
  3. * Find passwords. Which text strings preoduce the wanted numbers?
  4. *
  5. * ASCII coding: All characters are represented by a number, (32-127) The
  6. * American standard (without åäö) is used in this lab.
  7. */
  8. public class Crack {
  9.  
  10. // kungen == 1643565271408557312
  11. public static void main(String[] args) {
  12. // praktisk, för att inte datorns ska "dö" när ni kör koden.
  13. Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  14.  
  15. System.out.println("ASCII-tabellen");
  16. for (int i = 32; i < 128; i++) {
  17. System.out.printf("%3d ", i);
  18. }
  19. System.out.println("");
  20. for (int i = 32; i < 128; i++) {
  21. System.out.print(" " + (char) i + " ");
  22. }
  23. System.out.println("");
  24. System.out.println(easypz("3479515037239494400"));
  25.  
  26. System.out.println("Uppgift 0:");
  27. System.out.println("Hitta den textstäng som easy() kodar till 75097108108101046");
  28.  
  29. System.out.println("");
  30. System.out.println("Uppgift 1:");
  31. System.out.println("Hitta den textstäng som first() kodar till 3700833");
  32.  
  33. System.out.println("");
  34. System.out.println("Uppgift 2:");
  35. System.out.println("Hitta den textstäng som hard() kodar till 3479515037239494400");
  36. }
  37.  
  38. public static long easy(String pwd) {
  39. if (pwd == null) {
  40. return 0;
  41. }
  42. long code = 0;
  43. if (pwd.length() < 1) {
  44. return 0;
  45. }
  46.  
  47. for (int i = 0; i < pwd.length(); i++) {
  48. char c = pwd.charAt(i);
  49. code = 1000 * code + c;
  50. }
  51. return code;
  52. }
  53.  
  54. public static long easypz(String pwd) {
  55. String Password = pwd;
  56.  
  57. int x1 = 0;
  58. int x2 = 0;
  59. int x3 = 0;
  60. int x4 = 0;
  61. int x5 = 0;
  62. int x6 = 0;
  63. int x7 = 0;
  64. int x8 = 0;
  65.  
  66. String alphabet = new String("!#¤%&/()*+´-,.0123456789:;><?@QWERTYUIOPÅÄÖLKJHGFDSAZXCVBNMqwertyuiopöäålkjhgfdsazxcvbnm"); //39
  67. char[] arr = alphabet.toCharArray();
  68.  
  69. while (true) {
  70. String guess = null;
  71. if (x1 > 0) {
  72. guess = new String(arr[x1] + "");
  73. }
  74. if (x2 > 0) {
  75. guess = new String(arr[x1] + "" + arr[x2]);
  76. }
  77. if (x3 > 0) {
  78. guess = new String(arr[x1] + "" + arr[x2] + "" + arr[x3]);
  79. }
  80. if (x4 > 0) {
  81. guess = new String(arr[x1] + "" + arr[x2] + "" + arr[x3] + "" + arr[x4]);
  82. }
  83. if (x5 > 0) {
  84. guess = new String(arr[x1] + "" + arr[x2] + "" + arr[x3] + "" + arr[x4] + "" + arr[x5]);
  85. }
  86. if (x6 > 0) {
  87. guess = new String(arr[x1] + "" + arr[x2] + "" + arr[x3] + "" + arr[x4] + "" + arr[x5] + "" + arr[x6]);
  88. }
  89. if (x7 > 0) {
  90. guess = new String(arr[x1] + "" + arr[x2] + "" + arr[x3] + "" + arr[x4] + "" + arr[x5] + "" + arr[x6] + "" + arr[x7]);
  91. }
  92. if (x8 > 0) {
  93. guess = new String(arr[x1] + "" + arr[x2] + "" + arr[x3] + "" + arr[x4] + "" + arr[x5] + "" + arr[x6] + "" + arr[x7] + "" + arr[x8]);
  94. }
  95.  
  96. x1++;
  97.  
  98. if (x1 == alphabet.length()) {
  99. x1 = 0;
  100. x2 += 1;
  101. }
  102. if (x2 == alphabet.length()) {
  103. x2 = 0;
  104. x3 += 1;
  105. }
  106. if (x3 == alphabet.length()) {
  107. x3 = 0;
  108. x4 += 1;
  109. }
  110. if (x4 == alphabet.length()) {
  111. x4 = 0;
  112. x5 += 1;
  113. }
  114. if (x5 == alphabet.length()) {
  115. x5 = 0;
  116. x6 += 1;
  117. }
  118. if (x6 == alphabet.length()) {
  119. x6 = 0;
  120. x7 += 1;
  121. }
  122. if (x7 == alphabet.length()) {
  123. x7 = 0;
  124. x8 += 1;
  125. }
  126. if (x8 == alphabet.length()) {
  127. x8 = 0;
  128. System.out.println(guess);
  129. return 0;
  130. }
  131. //System.out.println(guess);
  132.  
  133. long code = hard(guess);
  134.  
  135. //System.out.println(code);
  136.  
  137. if(code == 3479515037239494400L){
  138. System.out.println(guess);
  139. break;
  140.  
  141. }
  142.  
  143. }
  144. return 0;
  145.  
  146. }
  147.  
  148. //////////////////////////////////////////
  149. public static long first(String pwd) {
  150. if( pwd==null)return 0;
  151. long code = 1;
  152.  
  153. for (int i = 0; i < pwd.length(); i++) {
  154. char c = pwd.charAt(i);
  155. code = 128 * code + c;
  156. }
  157. return code;
  158. }
  159. /////////////////////////////////////////////
  160.  
  161. public static long hard(String pwd) {
  162. if(pwd==null)return 0;
  163. int[] primes = {2, 3, 5, 7, 11, 13, 17, 19};
  164. long code = 1;
  165.  
  166. int max = 5;
  167. if (pwd.length() < 5) {
  168. max = pwd.length();
  169. }
  170. for (int i = 0; i < max; i++) {
  171. char c = pwd.charAt(i);
  172. code = (primes[i] + c * c) * primes[i] * code;
  173. }
  174. return code;
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement