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