Advertisement
DJTommek

Drali's alberti cipher disk keyword solver

Aug 16th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package javaapplication3;
  6.  
  7. /**
  8. *
  9. * @author dita
  10. */
  11. public class JavaApplication3 {
  12.  
  13. /**
  14. * @param args the command line arguments
  15. */
  16. public static void main(String[] args) {
  17. // TODO code application logic here
  18.  
  19. java.util.Scanner s = new java.util.Scanner(System.in);
  20. String code = "TZTAWUXWAAMENWWDMGKT"
  21. + "QHRCJTKQVEILTMQPRLEQ"
  22. + "ZUYHALQLMGZWPQVHSREJ"
  23. + "ZVRIWMUQRRUKKEZAVRAM";
  24. code = code.toLowerCase();
  25.  
  26. //char[] charcode = code.toCharArray();
  27. String key2 = "rokcjvbndwexmpaslhyftgzqiu";
  28. String key1 = "rykqcpmxfsaltnzbeodujhviwg";
  29.  
  30. //int rotation;
  31. String result;
  32. /*
  33. for (rotation = 0; rotation < 26; rotation++) {
  34. result = code;
  35. int shift = 0;
  36. for (char ch : charcode) {
  37. result = result.replace(ch, (char) (key2.charAt((key1.indexOf(ch) + rotation + shift) % 26) - 32));
  38. shift++;
  39. }
  40. System.out.println(result);
  41. }
  42.  
  43. System.out.println(" ");
  44. for (rotation = 0; rotation < 26; rotation++) {
  45. result = code;
  46. for (char ch : charcode) {
  47. result = result.replace(ch, (char) (key1.charAt((key2.indexOf(ch) + rotation + shift) % 26) - 32));
  48. }
  49. System.out.println(result);
  50. }*/
  51. /*
  52. result = code;
  53. int i; // kde jsme ve vstupu
  54. int rotation; //aktualni pootoceni
  55. int rotationBase;
  56. //int oKolik = 2; //o kolik
  57. int jakCasto; //jak casto
  58. for (rotationBase = 0; rotationBase < 26; rotationBase++) {
  59. for (jakCasto = 1; jakCasto < result.length(); jakCasto++) {
  60. i = 0;
  61. rotation = rotationBase;
  62. int XQWcount = 0;
  63. for (char ch : result.toCharArray()) {
  64. if (i % jakCasto == 0 && i != 0) {
  65. rotation = (rotation + oKolik) % result.length();
  66. }
  67. char goal = (char) (key2.charAt((key1.indexOf(ch) + rotation) % 26) - 32);
  68. if (goal == (char) 'W' || goal == (char) 'Q' || goal == (char) 'X' || goal == (char) 'G') {
  69. XQWcount++;
  70. }
  71. result = result.replace(ch, goal);
  72. i++;
  73. }
  74. /*
  75. System.out.print(XQWcount);
  76. if(XQWcount < 5){
  77. System.out.println(result);
  78. }
  79. System.out.println(result);
  80. result = code;
  81. }
  82. }*/
  83.  
  84. System.out.println("Zadejte heslo: ");
  85. String key = s.nextLine().toLowerCase();
  86.  
  87. while (!key.equals("0")) {
  88.  
  89. int i = 0;
  90. for (char ch : code.toCharArray()) {
  91. //System.out.println("Hledame hodnotu " + ch + " s klicovou abecedou " + key.charAt(i%key.length()) + ", vysledek je " +decode(key.charAt(i%key.length()), ch) );
  92. System.out.print(decode(key.charAt(i % key.length()), ch));
  93. i++;
  94. }
  95.  
  96. System.out.println();
  97. System.out.println("Zadejte heslo: ");
  98. key = s.nextLine().toLowerCase();
  99.  
  100. /*for (int j = 0; j < 4; j++) {
  101. for(int k = 0; k < 26; k++){
  102.  
  103. }
  104. }*/
  105. }
  106.  
  107. }
  108.  
  109. public static char decode(char keyChar, char codeChar) {
  110. String key1 = "rykqcpmxfsaltnzbeodujhviwg";
  111. String key2 = "aslhyftgzqiurokcjvbndwexmp";
  112. char goal;
  113.  
  114. int rotate = key2.indexOf(keyChar);
  115. int b = key1.indexOf(codeChar);
  116. goal = key2.charAt((b + rotate) % 26);
  117.  
  118. /*
  119. int shift = key1.lastIndexOf(keyChar);
  120. int index = key2.lastIndexOf(codeChar);
  121. //char tmp = key2.charAt(index + 26 - shift) % 26;
  122. //System.out.println("Shift: " + shift + ", index: " + index + ", tmp: " );
  123.  
  124. char goal = (char) (key2.charAt((key1.indexOf(codeChar) + 26 - shift) % 26) - 32);*/
  125. return goal;
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement