+++ /dev/null
-import java.io.IOException;
-import java.sql.Time;
-import java.util.Random;
-
-public class ScharedSecret {
- static Random rand=new Random();
-
- public static int r10(){
- return rand.nextInt(10);
- }
-
- public static int[] encodeBit(boolean bit){
- int threshold=22;
- int dist=3;
-
- int low = threshold-dist;
- int high = threshold+dist;
-
- int d1,d2,d3,d4,d5;
- //System.out.println("\nbit: "+(bit?1:0));
- if (bit){
- //System.out.println("bounds: ["+threshold+"..."+high+"]");
- d1=r10();
- while(d1+18<threshold) d1=r10();
- //System.out.println("d1 = "+d1);
-
- d2=r10();
- while (d1+d2+9<threshold||d1+d2>high) d2=r10();
- //System.out.println("d2 = "+d2);
-
- d3=r10();
- while (d1+d2+d3<threshold||d1+d2+d3>high)d3=r10();
- //System.out.println("d3 = "+d3);
-
- d4=r10();
- while (d1+d2+d3+d4 > high) d4=r10();
- //System.out.println("d4 = "+d4);
-
- d5=r10();
- while (d1+d2+d3+d4+d5 > high) d5=r10();
- //System.out.println("d5 = "+d5);
-
-
- } else {
- //System.out.println("bounds: ["+low+"..."+threshold+"]");
- d1=r10();
- while(d1+18<low) d1=r10();
- //System.out.println("d1 = "+d1);
-
- d2=r10();
- while (d1+d2+9<low) d2=r10();
- //System.out.println("d2 = "+d2);
-
- d3=r10();
- while (d1+d2+d3>threshold||d1+d2+d3<low)d3=r10();
- //System.out.println("d3 = "+d3);
-
- d4=r10();
- while (d1+d2+d3+d4>threshold||d1+d2+d3+d4<low) d4=r10();
- //System.out.println("d4 = "+d4);
-
- d5=r10();
- while (d1+d2+d3+d4+d5 > threshold||d1+d2+d3+d4+d5<low) d5=r10();
- //System.out.println("d5 = "+d5);
- }
-
- //System.out.println(""+d1+d2+d3+d4+d5+" ("+(d1+d2+d3+d4+d5)+")");
- int[] result = {d1,d2,d3,d4,d5};
- return result;
- }
-
- public static String[] encodeChar(char c){
- String binary = Integer.toBinaryString(c & 0xFF);
-
- while (binary.length()<8) binary='0'+binary;
- System.out.print(binary+" ");
- String code1="";
- String code2="";
- String code3="";
- String code4="";
- String code5="";
- for (int i=0; i<8;i++){
- int[] digits = encodeBit(binary.charAt(i)=='1');
- int index = rand.nextInt(4);
- code1+=digits[index];
- if (index!=4)digits[index]=digits[4]; // move last digit to position of used digit
-
- index = rand.nextInt(3);
- code2+=digits[index];
- if (index!=3)digits[index]=digits[3]; // move last digit to position of used digit
-
- index = rand.nextInt(2);
- code3+=digits[index];
- if (index!=2)digits[index]=digits[2]; // move last digit to position of used digit
-
- index = rand.nextInt(1);
- code4+=digits[index];
- code5+=digits[1-index];
- }
- String[] result = {code1,code2,code3,code4,code5};
- return result;
- }
-
- private static String secret(String[] args){
- if (args != null && args.length > 0) return args[0];
-
- try {
- String secret = "";
- System.out.println("Please enter your secret:");
- int c;
- while ((c = System.in.read()) != 10){
- secret+=(char)c;
- }
- return secret;
- } catch (IOException e) {
- e.printStackTrace();
- System.exit(1);
- }
- return null;
- }
-
- public static void main(String[] args) {
- String secret = secret(args);
- String [] codes = { "", "", "", "", "" };
- System.out.print(" Binary: ");
- for (int i=0; i<secret.length();i++){
- String[] chars = encodeChar(secret.charAt(i));
- for (int j = 0; j<5; j++) codes[j] += chars[j]+" ";
- }
- System.out.println();
-
- for (int j=0; j<5; j++) System.out.println("\nSecret "+j+": "+codes[j]);
- System.out.print("\nChecksum: ");
- for (int i=0; i<codes[0].length(); i++){
-
- if (i%9==8) {
- System.out.print(" ");
- } else {
- System.out.print((codes[0].charAt(i)
- +codes[1].charAt(i)
- +codes[2].charAt(i)
- +codes[3].charAt(i)
- +codes[4].charAt(i))>(240+22)?1:0);
- }
- }
- }
-}
+++ /dev/null
-import java.security.InvalidParameterException;
-import java.util.Random;
-import java.util.Vector;
-
-public class ShamirsSecret {
- private static Random rand = new Random();
- private static int prime = 257;
-
- public static int ggT(int a, int b){
- if (b<0) b=-b;
- if (a==0) return b;
- if (a<0) a=-a;
- while (b != 0) if (a>b) a-=b; else b-=a;
- return a;
- }
-
-
- public static class Quotient{
- int divident;
- int divisor;
-
- public Quotient(int i) {
- this.divident=i;
- this.divisor=1;
- }
-
- public Quotient(int divident,int divisor) throws InvalidParameterException{
- if (divisor == 0) throw new InvalidParameterException("Divisor of quotient must not be zero!");
- int ggt = ggT(divident,divisor);
- if (divisor<0) ggt = -ggt;
- this.divident=divident/ggt;
- this.divisor=divisor/ggt;
- }
-
- public Quotient times(Quotient q) throws InvalidParameterException{
- return new Quotient(divident * q.divident,divisor * q.divisor);
- }
-
- public Quotient times(int f) throws InvalidParameterException{
- return new Quotient(f*divident,divisor);
- }
-
- @Override
- public String toString() {
- return (divisor == 1) ? ""+divident : "["+divident+"/"+divisor+"]";
- }
-
- public int val() throws InvalidParameterException {
- if (divisor != 1) throw new InvalidParameterException("Divisor should not be "+divisor);
- return divident;
- }
-
- public Quotient add(int i) throws InvalidParameterException{
- return add(new Quotient(i));
- }
-
- public Quotient add(Quotient q) throws InvalidParameterException {
- return new Quotient(divident*q.divisor + divisor*q.divident,divisor*q.divisor);
- }
-
- public int mod(int q) throws InvalidParameterException {
- int inv = 1;
- while ((divisor*inv)%q !=1)inv++; // search the inverse of the divisor
- return (divident*inv)%q;
- }
- }
-
- public static class Share{
- private int x,y;
-
- public Share(int x, int[] coefficients) {
- this.x=x;
- int sum=0;
- for (int i=0; i<coefficients.length; i++) sum+=coefficients[i]*pow(x,i);
- this.y=sum % prime;
- }
-
- @Override
- public String toString() {
- return "Secret(x = "+x+", y = "+y+")";
- }
- }
-
- private static int pow(int x,int e){
- return (e==0) ? 1 : x*pow(x,e-1);
- }
-
- private static Vector<Share> createSecrets(int secret, int count, int requires) {
- Vector<Share> result = new Vector<Share>();
-
- int [] coefficients = new int[requires];
- coefficients[0] = secret;
- for (int i=1; i<requires; i++) coefficients[i]=rand.nextInt(256);
- for (int x=1; x<=count; x++) result.add(new Share(x, coefficients));
- return result;
- }
-
- private static int reconstruct(Vector<Share> shares) throws InvalidParameterException{
- Quotient sum = new Quotient(0);
- for (Share share_j:shares){
- Quotient prod = new Quotient(1);
- for (Share share_m:shares){
- if (share_j == share_m) continue;
- prod = prod.times(new Quotient(share_m.x,share_m.x-share_j.x));
- }
- sum = sum.add(prod.times(share_j.y));
- }
- return sum.mod(prime);
- }
-
- public static void main(String[] args) throws InvalidParameterException {
- int secret = rand.nextInt(255);
- System.out.println(secret);
- Vector<Share> shares = createSecrets(secret,5,3);
- System.out.println(shares);
-
- shares.remove(0);
- shares.remove(0);
- System.out.println(shares);
-
- secret=reconstruct(shares);
- System.out.println(secret);
- }
-}
+++ /dev/null
-<html>
-<head></head>
-<body>
-<script type="text/javascript">
-var prime = 1237;
-
-/* Split number into the shares */
-function split(number, available, needed) {
-
- var coef = [number, 166, 94], x, exp, c, accum, shares = [];
- for(c = 1, coef[0] = number; c < needed; c++) coef[c] = Math.floor(Math.random() * (prime - 1));
- for(x = 1; x <= available; x++) {
- /* coef = [1234, 166, 94] which is 1234x^0 + 166x^1 + 94x^2 */
- for(exp = 1, accum = coef[0]; exp < needed; exp++) accum = (accum + (coef[exp] * (Math.pow(x, exp) % prime) % prime)) % prime;
- /* Store values as (1, 132), (2, 66), (3, 188), (4, 241), (5, 225) (6, 140) */
- shares[x - 1] = [x, accum];
- }
- return shares;
-}
-
-/* Gives the decomposition of the gcd of a and b. Returns [x,y,z] such that x = gcd(a,b) and y*a + z*b = x */
-function gcdD(a,b) {
- if (b == 0) return [a, 1, 0];
- else {
- var n = Math.floor(a/b), c = a % b, r = gcdD(b,c);
- return [r[0], r[2], r[1]-r[2]*n];
- }
-}
-
-/* Gives the multiplicative inverse of k mod prime. In other words (k * modInverse(k)) % prime = 1 for all prime > k >= 1 */
-function modInverse(k) {
- k = k % prime;
- var r = (k < 0) ? -gcdD(prime,-k)[2] : gcdD(prime,k)[2];
- return (prime + r) % prime;
-}
-
-/* Join the shares into a number */
-function join(shares) {
- var accum, count, formula, startposition, nextposition, value, numerator, denominator;
- for(formula = accum = 0; formula < shares.length; formula++) {
- /* Multiply the numerator across the top and denominators across the bottom to do Lagrange's interpolation
- * Result is x0(2), x1(4), x2(5) -> -4*-5 and (2-4=-2)(2-5=-3), etc for l0, l1, l2...
- */
- for(count = 0, numerator = denominator = 1; count < shares.length; count++) {
- if(formula == count) continue; // If not the same value
- startposition = shares[formula][0];
- nextposition = shares[count][0];
- numerator = (numerator * -nextposition) % prime;
- denominator = (denominator * (startposition - nextposition)) % prime;
- }
- value = shares[formula][1];
- accum = (prime + accum + (value * numerator * modInverse(denominator))) % prime;
- }
- return accum;
-}
-
-var sh = split(1234, 6, 3) /* split the secret value 129 into 6 components - at least 3 of which will be needed to figure out the secret value */
-for (var i=0; i<sh.length; i++) alert(sh[i]);
-var newshares = [sh[1], sh[3], sh[4]]; /* pick any selection of 3 shared keys from sh */
-
-alert(join(newshares));
-
-</script>
-</body>
-</html>
\ No newline at end of file