Projekt neu eingecheckt
[shamirs_secret_web_implementation.git] / src / ScharedSecret.java
1 import java.sql.Time;
2 import java.util.Random;
3
4 public class ScharedSecret {
5         static Random rand=new Random();
6         
7         public static int r10(){
8                 return rand.nextInt(10);
9         }
10         
11         public static int[] encodeBit(boolean bit){
12                 int threshold=22;
13                 int dist=3;
14                 
15                 int low = threshold-dist;
16                 int high = threshold+dist;
17                 
18                 int d1,d2,d3,d4,d5;
19                 //System.out.println("\nbit: "+(bit?1:0));              
20                         if (bit){                               
21                                 //System.out.println("bounds: ["+threshold+"..."+high+"]");
22                                 d1=r10();
23                                 while(d1+18<threshold) d1=r10();
24                                 //System.out.println("d1 = "+d1);
25                                 
26                                 d2=r10();
27                                 while (d1+d2+9<threshold||d1+d2>high) d2=r10();
28                                 //System.out.println("d2 = "+d2);
29                                 
30                                 d3=r10();
31                                 while (d1+d2+d3<threshold||d1+d2+d3>high)d3=r10();
32                                 //System.out.println("d3 = "+d3);
33                                 
34                                 d4=r10();                               
35                                 while (d1+d2+d3+d4 > high) d4=r10();
36                                 //System.out.println("d4 = "+d4);
37                                 
38                                 d5=r10();                               
39                                 while (d1+d2+d3+d4+d5 > high) d5=r10();
40                                 //System.out.println("d5 = "+d5);
41                                 
42                                 
43                         } else {
44                                 //System.out.println("bounds: ["+low+"..."+threshold+"]");
45                                 d1=r10();
46                                 while(d1+18<low) d1=r10();
47                                 //System.out.println("d1 = "+d1);
48                                 
49                                 d2=r10();
50                                 while (d1+d2+9<low) d2=r10();
51                                 //System.out.println("d2 = "+d2);
52                                 
53                                 d3=r10();
54                                 while (d1+d2+d3>threshold||d1+d2+d3<low)d3=r10();
55                                 //System.out.println("d3 = "+d3);
56                                 
57                                 d4=r10();                               
58                                 while (d1+d2+d3+d4>threshold||d1+d2+d3+d4<low) d4=r10();
59                                 //System.out.println("d4 = "+d4);
60                                 
61                                 d5=r10();                               
62                                 while (d1+d2+d3+d4+d5 > threshold||d1+d2+d3+d4+d5<low) d5=r10();
63                                 //System.out.println("d5 = "+d5);
64                         }                       
65                 
66                         //System.out.println(""+d1+d2+d3+d4+d5+" ("+(d1+d2+d3+d4+d5)+")");
67                 int[] result = {d1,d2,d3,d4,d5};
68                 return result;
69         }
70         
71         public static String[] encodeChar(char c){
72                 String binary = Integer.toBinaryString(c & 0xFF);
73                 
74                 while (binary.length()<8) binary='0'+binary;
75                 System.out.print(binary+" ");
76                 String code1="";
77                 String code2="";
78                 String code3="";
79                 String code4="";
80                 String code5="";
81                 for (int i=0; i<8;i++){
82                         int[] digits = encodeBit(binary.charAt(i)=='1');
83                         int index = rand.nextInt(4);                    
84                         code1+=digits[index];
85                         if (index!=4)digits[index]=digits[4]; // move last digit to position of used digit
86                         
87                         index = rand.nextInt(3);
88                         code2+=digits[index];
89                         if (index!=3)digits[index]=digits[3]; // move last digit to position of used digit
90                         
91                         index = rand.nextInt(2);                        
92                         code3+=digits[index];
93                         if (index!=2)digits[index]=digits[2]; // move last digit to position of used digit
94                         
95                         index = rand.nextInt(1);
96                         code4+=digits[index];
97                         code5+=digits[1-index];
98                 }
99                 String[] result = {code1,code2,code3,code4,code5};
100                 return result;
101         }
102         
103         public static void main(String[] args) {
104                 String secret="gnome-keyring";
105                 String code1="";
106                 String code2="";
107                 String code3="";
108                 String code4="";
109                 String code5="";
110                 System.out.print("  Binary: ");
111                 for (int i=0; i<secret.length();i++){
112                         String[] codes = encodeChar(secret.charAt(i));
113                         code1+=codes[0]+" ";
114                         code2+=codes[1]+" ";
115                         code3+=codes[2]+" ";
116                         code4+=codes[3]+" ";
117                         code5+=codes[4]+" ";
118                 }
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++){
127                         
128                         if (i%9==8) {
129                                 System.out.print(" ");
130                         } else {
131                                 System.out.print((code1.charAt(i)+code2.charAt(i)+code3.charAt(i)+code4.charAt(i)+code5.charAt(i))>(240+22)?1:0);
132                         }
133                 }
134         }
135 }