1 import java.io.IOException;
3 import java.util.Random;
5 public class ScharedSecret {
6 static Random rand=new Random();
8 public static int r10(){
9 return rand.nextInt(10);
12 public static int[] encodeBit(boolean bit){
16 int low = threshold-dist;
17 int high = threshold+dist;
20 //System.out.println("\nbit: "+(bit?1:0));
22 //System.out.println("bounds: ["+threshold+"..."+high+"]");
24 while(d1+18<threshold) d1=r10();
25 //System.out.println("d1 = "+d1);
28 while (d1+d2+9<threshold||d1+d2>high) d2=r10();
29 //System.out.println("d2 = "+d2);
32 while (d1+d2+d3<threshold||d1+d2+d3>high)d3=r10();
33 //System.out.println("d3 = "+d3);
36 while (d1+d2+d3+d4 > high) d4=r10();
37 //System.out.println("d4 = "+d4);
40 while (d1+d2+d3+d4+d5 > high) d5=r10();
41 //System.out.println("d5 = "+d5);
45 //System.out.println("bounds: ["+low+"..."+threshold+"]");
47 while(d1+18<low) d1=r10();
48 //System.out.println("d1 = "+d1);
51 while (d1+d2+9<low) d2=r10();
52 //System.out.println("d2 = "+d2);
55 while (d1+d2+d3>threshold||d1+d2+d3<low)d3=r10();
56 //System.out.println("d3 = "+d3);
59 while (d1+d2+d3+d4>threshold||d1+d2+d3+d4<low) d4=r10();
60 //System.out.println("d4 = "+d4);
63 while (d1+d2+d3+d4+d5 > threshold||d1+d2+d3+d4+d5<low) d5=r10();
64 //System.out.println("d5 = "+d5);
67 //System.out.println(""+d1+d2+d3+d4+d5+" ("+(d1+d2+d3+d4+d5)+")");
68 int[] result = {d1,d2,d3,d4,d5};
72 public static String[] encodeChar(char c){
73 String binary = Integer.toBinaryString(c & 0xFF);
75 while (binary.length()<8) binary='0'+binary;
76 System.out.print(binary+" ");
82 for (int i=0; i<8;i++){
83 int[] digits = encodeBit(binary.charAt(i)=='1');
84 int index = rand.nextInt(4);
86 if (index!=4)digits[index]=digits[4]; // move last digit to position of used digit
88 index = rand.nextInt(3);
90 if (index!=3)digits[index]=digits[3]; // move last digit to position of used digit
92 index = rand.nextInt(2);
94 if (index!=2)digits[index]=digits[2]; // move last digit to position of used digit
96 index = rand.nextInt(1);
98 code5+=digits[1-index];
100 String[] result = {code1,code2,code3,code4,code5};
104 private static String secret(String[] args){
105 if (args != null && args.length > 0) return args[0];
109 System.out.println("Please enter your secret:");
111 while ((c = System.in.read()) != 10){
115 } catch (IOException e) {
122 public static void main(String[] args) {
123 String secret = secret(args);
124 String [] codes = { "", "", "", "", "" };
125 System.out.print(" Binary: ");
126 for (int i=0; i<secret.length();i++){
127 String[] chars = encodeChar(secret.charAt(i));
128 for (int j = 0; j<5; j++) codes[j] += chars[j]+" ";
130 System.out.println();
132 for (int j=0; j<5; j++) System.out.println("\nSecret "+j+": "+codes[j]);
133 System.out.print("\nChecksum: ");
134 for (int i=0; i<codes[0].length(); i++){
137 System.out.print(" ");
139 System.out.print((codes[0].charAt(i)
143 +codes[4].charAt(i))>(240+22)?1:0);