2 import java.util.Random;
4 public class ScharedSecret {
5 static Random rand=new Random();
7 public static int r10(){
8 return rand.nextInt(10);
11 public static int[] encodeBit(boolean bit){
15 int low = threshold-dist;
16 int high = threshold+dist;
19 //System.out.println("\nbit: "+(bit?1:0));
21 //System.out.println("bounds: ["+threshold+"..."+high+"]");
23 while(d1+18<threshold) d1=r10();
24 //System.out.println("d1 = "+d1);
27 while (d1+d2+9<threshold||d1+d2>high) d2=r10();
28 //System.out.println("d2 = "+d2);
31 while (d1+d2+d3<threshold||d1+d2+d3>high)d3=r10();
32 //System.out.println("d3 = "+d3);
35 while (d1+d2+d3+d4 > high) d4=r10();
36 //System.out.println("d4 = "+d4);
39 while (d1+d2+d3+d4+d5 > high) d5=r10();
40 //System.out.println("d5 = "+d5);
44 //System.out.println("bounds: ["+low+"..."+threshold+"]");
46 while(d1+18<low) d1=r10();
47 //System.out.println("d1 = "+d1);
50 while (d1+d2+9<low) d2=r10();
51 //System.out.println("d2 = "+d2);
54 while (d1+d2+d3>threshold||d1+d2+d3<low)d3=r10();
55 //System.out.println("d3 = "+d3);
58 while (d1+d2+d3+d4>threshold||d1+d2+d3+d4<low) d4=r10();
59 //System.out.println("d4 = "+d4);
62 while (d1+d2+d3+d4+d5 > threshold||d1+d2+d3+d4+d5<low) d5=r10();
63 //System.out.println("d5 = "+d5);
66 //System.out.println(""+d1+d2+d3+d4+d5+" ("+(d1+d2+d3+d4+d5)+")");
67 int[] result = {d1,d2,d3,d4,d5};
71 public static String[] encodeChar(char c){
72 String binary = Integer.toBinaryString(c & 0xFF);
74 while (binary.length()<8) binary='0'+binary;
75 System.out.print(binary+" ");
81 for (int i=0; i<8;i++){
82 int[] digits = encodeBit(binary.charAt(i)=='1');
83 int index = rand.nextInt(4);
85 if (index!=4)digits[index]=digits[4]; // move last digit to position of used digit
87 index = rand.nextInt(3);
89 if (index!=3)digits[index]=digits[3]; // move last digit to position of used digit
91 index = rand.nextInt(2);
93 if (index!=2)digits[index]=digits[2]; // move last digit to position of used digit
95 index = rand.nextInt(1);
97 code5+=digits[1-index];
99 String[] result = {code1,code2,code3,code4,code5};
103 public static void main(String[] args) {
104 String secret="gnome-keyring";
110 System.out.print(" Binary: ");
111 for (int i=0; i<secret.length();i++){
112 String[] codes = encodeChar(secret.charAt(i));
119 System.out.println();
120 System.out.println("\nSecret 1: "+code1);
121 System.out.println("\nSecret 2: "+code2);
122 System.out.println("\nSecret 3: "+code3);
123 System.out.println("\nSecret 4: "+code4);
124 System.out.println("\nSecret 5: "+code5);
125 System.out.print("\nChecksum: ");
126 for (int i=0; i<code1.length(); i++){
129 System.out.print(" ");
131 System.out.print((code1.charAt(i)+code2.charAt(i)+code3.charAt(i)+code4.charAt(i)+code5.charAt(i))>(240+22)?1:0);