Advertisement
Guest User

Untitled

a guest
Oct 25th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 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; bitSize2 < finalCheckSum2.length();)
  74. {
  75. indeces2.add(convertThing(finalCheckSum2.substring(i, bitSize2)));
  76. i = bitSize2;
  77. bitSize2 += bitSize2;
  78. }
  79.  
  80. for(int i = 0; bitSize1 < finalCheckSum1.length();)
  81. {
  82.  
  83. indeces1.add(convertThing(finalCheckSum1.substring(i, bitSize1)));
  84. i = bitSize1;
  85. bitSize1 += 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,2);
  106. RESTClient.sendOutput(output2,2);
  107. }
  108.  
  109. public static int convertThing(String s)
  110. {
  111. int sum = 0;
  112. for(int x = 0; x < s.length(); x++)
  113. {
  114. sum += Math.pow(2 * Integer.parseInt(s.charAt(x) + "") , s.length() - x);
  115. }
  116. System.out.println(sum);
  117. return sum;
  118. }
  119.  
  120. public static String convertArrayToString(boolean[] arr)
  121. {
  122. String result = "";
  123. for(Boolean b: arr)
  124. {
  125. if(b == true)
  126. result += 1;
  127. else
  128. result += 0;
  129. }
  130.  
  131. return result;
  132. }
  133.  
  134. public static boolean[] byteArray2BitArray(byte[] bytes)
  135. {
  136. boolean[] bits = new boolean[bytes.length * 8];
  137. for (int i = 0; i < bytes.length * 8; i++)
  138. {
  139. if ((bytes[i / 8] & (1 << (7 - (i % 8)))) > 0)
  140. bits[i] = true;
  141. else
  142. bits[i] = false;
  143.  
  144. }
  145. return bits;
  146. }
  147.  
  148. public static byte[] hashingEnt(String in) throws NoSuchAlgorithmException
  149. {
  150.  
  151. MessageDigest md = MessageDigest.getInstance("SHA-256");
  152. md.update(in.getBytes());
  153. byte byteData[] = md.digest();
  154. return byteData;
  155. }
  156.  
  157.  
  158.  
  159. public static void loadFiles() throws IOException
  160. {
  161.  
  162. Scanner in;
  163. String line;
  164.  
  165. in = new Scanner(wordListInput);
  166. while(in.hasNext())
  167. {
  168. line = in.nextLine();
  169. if(!(line.isEmpty() || line.equals("")))
  170. wordList.add(line);
  171. }
  172. in.close();
  173. }
  174.  
  175. public static ArrayList<String> getWords(boolean[] concatBits) {
  176. /*
  177. * Guide to get a word from bits, will have to be put in for loop to go
  178. * through whole boolean array:
  179. *
  180. * for (int j = 0; j < groupOfBits; ++j)
  181. *{
  182. * index <<= 1;
  183. * if(concatBits[(i * groupOfBits) + j])
  184. * index |= 0x1
  185. *}
  186. * words.add(wordList.get(index));
  187. *
  188. */
  189.  
  190. // replace null with result object
  191. return null;
  192. }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement