Advertisement
Guest User

Untitled

a guest
Oct 25th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. package com.aciw.foobar.mnemonic;
  2.  
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. import java.util.*;
  6. import java.io.*;
  7.  
  8. import com.aciw.foobar.utility.RESTClient;
  9.  
  10. /**
  11. *
  12. * @author Administrator
  13. *
  14. */
  15. public class App {
  16. private static final String username = "Team Break";
  17. private static final String password = "E7WBVI3";
  18.  
  19. // Inputs for set1 (Ent1) and the bonus set (Ent2)
  20. private static final String Ent1 = "7f7f7f7f7f7f7f7f";
  21. private static final String Ent2 = "7f7f7f7f7f7f7f7e";
  22.  
  23.  
  24. public static ArrayList<String> wordList = new ArrayList<String>();
  25.  
  26. public static File wordListInput = new File("./files/1/Word_List_Input_Set_1.txt");
  27.  
  28. // This skeleton is convert a single entropy to a mnemonic sentence.
  29. // You must modify the code to convert 2 entropys to mnemonic sentences.
  30. public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
  31.  
  32. RESTClient.setUsername(username);
  33. RESTClient.setPassword(password);
  34.  
  35. // Insert Code here...
  36. loadFiles();
  37.  
  38. byte[] ent1Array = Ent1.getBytes("UTF-8");
  39. byte[] ent2Array = Ent2.getBytes("UTF-8");
  40. byte[] hashEnt1Array = hashingEnt(Ent1);
  41. byte[] hashEnt2Array = hashingEnt(Ent2);
  42. boolean[] bitEnt1Array = byteArray2BitArray(ent1Array);
  43. boolean[] bitHashEnt1Array = byteArray2BitArray(hashEnt1Array);
  44. boolean[] bitEnt2Array = byteArray2BitArray(ent2Array);
  45. boolean[] bitHashEnt2Array = byteArray2BitArray(hashEnt2Array);
  46.  
  47. int checkSum1 = bitEnt1Array.length/32;
  48. int checkSum2 = bitEnt2Array.length/32;
  49.  
  50. String bCheckSum1 = Integer.toBinaryString(checkSum1);
  51. String bCheckSum2 = Integer.toBinaryString(checkSum2);
  52.  
  53. String finalCheckSum1 = convertArrayToString(bitEnt1Array) + bCheckSum1;
  54. String finalCheckSum2 = convertArrayToString(bitEnt2Array) + bCheckSum2;
  55. //Methods begin here
  56.  
  57. System.out.println("length1: " +finalCheckSum1.length());
  58. System.out.println("length2: " +finalCheckSum2.length());
  59.  
  60. int bitSize1 = (finalCheckSum1.length())/12;
  61.  
  62. int bitSize2 = (finalCheckSum2.length())/22;
  63.  
  64. System.out.println("bitsize1: " +bitSize1);
  65. System.out.println("bitsize2: " +bitSize2);
  66.  
  67. ArrayList<Integer> indeces1 = new ArrayList<Integer>();
  68. ArrayList<Integer> indeces2 = new ArrayList<Integer>();
  69.  
  70. ArrayList<String> output1 = new ArrayList<String>();
  71. ArrayList<String> output2 = new ArrayList<String>();
  72.  
  73. for(int i = 0, j = bitSize2; i < finalCheckSum2.length()-1;)
  74. {
  75. indeces2.add(convertThing(finalCheckSum2.substring(i, j)));
  76. i = j;
  77. j += bitSize2;
  78. }
  79.  
  80. for(int i = 0, j = bitSize1; i < finalCheckSum1.length()-1;)
  81. {
  82.  
  83. indeces1.add(convertThing(finalCheckSum1.substring(i, j)));
  84. i = j;
  85. j += bitSize1;
  86. }
  87.  
  88. System.out.println(indeces1);
  89. System.out.println(indeces2);
  90.  
  91. for(Integer index : indeces1)
  92. output1.add(wordList.get(index));
  93. for(Integer index : indeces2)
  94. output2.add(wordList.get(index));
  95.  
  96.  
  97.  
  98. // The final step is to take these concatenated bits and split
  99. // them into groups of bits. Each group encodes number
  100. // which is a position in a wordlist. We convert numbers into
  101. // words and use joined words as mnemonic sentence.
  102. // ArrayList<String> OutputWords = getWords(concatBits);
  103.  
  104. // Submit answer
  105. RESTClient.sendOutput(output1,1);
  106. RESTClient.sendOutput(output2,2);
  107. }
  108.  
  109. public static int convertThing(String s)
  110. {
  111. int sum = 0;
  112.  
  113. for(int x = 0; x < s.length(); x++)
  114. {
  115. sum += Math.pow(2 * Integer.parseInt(s.charAt(x) + "") , x);
  116. System.out.println(sum);
  117. }
  118.  
  119. return sum;
  120. }
  121.  
  122. public static String convertArrayToString(boolean[] arr)
  123. {
  124. String result = "";
  125. for(Boolean b: arr)
  126. {
  127. if(b == true)
  128. result += 1;
  129. else
  130. result += 0;
  131. }
  132.  
  133. return result;
  134. }
  135.  
  136. public static boolean[] byteArray2BitArray(byte[] bytes)
  137. {
  138. boolean[] bits = new boolean[bytes.length * 8];
  139. for (int i = 0; i < bytes.length * 8; i++)
  140. {
  141. if ((bytes[i / 8] & (1 << (7 - (i % 8)))) > 0)
  142. bits[i] = true;
  143. else
  144. bits[i] = false;
  145.  
  146. }
  147. return bits;
  148. }
  149.  
  150. public static byte[] hashingEnt(String in) throws NoSuchAlgorithmException
  151. {
  152.  
  153. MessageDigest md = MessageDigest.getInstance("SHA-256");
  154. md.update(in.getBytes());
  155. byte byteData[] = md.digest();
  156. return byteData;
  157. }
  158.  
  159.  
  160.  
  161. public static void loadFiles() throws IOException
  162. {
  163.  
  164. Scanner in;
  165. String line;
  166.  
  167. in = new Scanner(wordListInput);
  168. while(in.hasNext())
  169. {
  170. line = in.nextLine();
  171. if(!(line.isEmpty() || line.equals("")))
  172. wordList.add(line);
  173. }
  174. in.close();
  175. }
  176.  
  177. public static ArrayList<String> getWords(boolean[] concatBits) {
  178. /*
  179. * Guide to get a word from bits, will have to be put in for loop to go
  180. * through whole boolean array:
  181. *
  182. * for (int j = 0; j < groupOfBits; ++j)
  183. *{
  184. * index <<= 1;
  185. * if(concatBits[(i * groupOfBits) + j])
  186. * index |= 0x1
  187. *}
  188. * words.add(wordList.get(index));
  189. *
  190. */
  191.  
  192. // replace null with result object
  193. return null;
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement