Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. package fixedxor;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. public class FixedXor {
  8.  
  9. private String someString;
  10.  
  11. public FixedXor(String someString) {
  12. this.someString = someString;
  13. }
  14.  
  15. public ArrayList<Integer> stringToHex() {
  16. ArrayList<Integer> asciiList = new ArrayList<Integer>();
  17. for(int i=0; i<someString.length(); i++) {
  18. //asciiList.add((int)word.charAt(i));
  19. asciiList.add(hexTable(someString.toUpperCase().charAt(i)));
  20. }
  21. return asciiList;
  22. }
  23.  
  24. private int hexTable(Character c) {
  25. Map<Character, Integer> table = new HashMap<Character, Integer>();
  26. table.put('0', 0);
  27. table.put('1', 1);
  28. table.put('2', 2);
  29. table.put('3', 3);
  30. table.put('4', 4);
  31. table.put('5', 5);
  32. table.put('6', 6);
  33. table.put('7', 7);
  34. table.put('8', 8);
  35. table.put('9', 9);
  36. table.put('A', 10);
  37. table.put('B', 11);
  38. table.put('C', 12);
  39. table.put('D', 13);
  40. table.put('E', 14);
  41. table.put('F', 15);
  42. return table.get(c);
  43. }
  44.  
  45. public ArrayList<String> hexToBinary(ArrayList<Integer> hex){
  46. ArrayList<String> binary = new ArrayList<String>();
  47. for(int i=0; i<hex.size(); i++) {
  48. if(hex.get(i) == 0) {
  49. binary.add("0000");
  50. }else if(hex.get(i) == 1) {
  51. binary.add("0001");
  52. }else {
  53. binary.add(convertToBinary(hex.get(i)));
  54. }
  55. }
  56. return binary;
  57. }
  58.  
  59. public String convertToBinary(int number) {
  60. String binary = "";
  61. while (number != 1) {
  62. binary += number % 2;
  63. number = number / 2;
  64. }
  65. binary += number % 2;
  66. if(binary.length() == 2) {
  67. //return "0"+new StringBuilder(binary).reverse().toString();
  68. binary = binary+"00";
  69. }else if(binary.length() == 3) {
  70. binary = binary+"0";
  71. }
  72. return new StringBuilder(binary).reverse().toString();
  73. }
  74.  
  75. public String binaryCombiner(ArrayList<String> binary) {
  76. String combinedBinary = "";
  77. for (String binaries : binary) {
  78. combinedBinary += binaries;
  79. }
  80. return combinedBinary;
  81. }
  82.  
  83. public static ArrayList<String> fourDigitBinary(String binaryXored) {
  84. ArrayList<String> fourValuedBinary = new ArrayList<String>();
  85. String fourBinary = "";
  86. int count = 0;
  87.  
  88. for(int i=0; i<binaryXored.length(); i++) {
  89. if(count != 4) {
  90. fourBinary += binaryXored.charAt(i);
  91. count++;
  92. }else {
  93. fourValuedBinary.add(fourBinary);
  94. count = 0;
  95. fourBinary = "";
  96. i-=1;
  97. }
  98. }
  99. fourValuedBinary.add(fourBinary);
  100. return fourValuedBinary;
  101. }
  102.  
  103. public static ArrayList<String> binaryToBytes(ArrayList<String> fourBinary){
  104. ArrayList<String> bytes = new ArrayList<String>();
  105. for (String binaries : fourBinary) {
  106. int count = 3;
  107. int byteValue = 0;
  108. for(int i=0; i<binaries.length(); i++) {
  109. if(i == 3 && binaries.charAt(i) == '1') {
  110. byteValue += 1;
  111. }
  112. else if(binaries.charAt(i) == '1') {
  113. byteValue += Math.pow(2, count);
  114. }
  115. count--;
  116. }
  117. bytes.add(String.valueOf(byteValue));
  118. }
  119. return bytes;
  120. }
  121.  
  122. public static void main(String[] args) {
  123. String binaryXored = "";
  124. String HexValue = "";
  125. //String finalHexValue = "";
  126. String[] hextable = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
  127. FixedXor fx = new FixedXor("1c0111001f010100061a024b53535009181c");
  128. ArrayList<Integer> hexValues = fx.stringToHex();
  129. ArrayList<String> binaries = fx.hexToBinary(hexValues);
  130. String combinedBinary1 = fx.binaryCombiner(binaries);
  131.  
  132. FixedXor fx1 = new FixedXor("686974207468652062756c6c277320657965");
  133. ArrayList<Integer> hexValue1 = fx1.stringToHex();
  134. ArrayList<String> binaries1 = fx1.hexToBinary(hexValue1);
  135. String combinedBinary2 = fx1.binaryCombiner(binaries1);
  136.  
  137. for(int i=0; i<combinedBinary1.length(); i++) {
  138. if((combinedBinary1.charAt(i) == '0' && combinedBinary2.charAt(i) == '0') ||
  139. (combinedBinary1.charAt(i) == '1' && combinedBinary2.charAt(i) == '1')) {
  140. binaryXored += '0';
  141. }else {
  142. binaryXored += '1';
  143. }
  144. }
  145.  
  146. ArrayList<String> hexDecoded = fourDigitBinary(binaryXored);
  147. ArrayList<String> bytes = binaryToBytes(hexDecoded);
  148. for (String s : bytes) {
  149. HexValue += hextable[Integer.valueOf(s)];
  150. }
  151. System.out.println(HexValue);
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement