예제 #1
0
        /* Reconstructs secret from first "k" share files from the array of file locations (ShareFilesNames)
         * then writes the secret into SecretFileLocation */
        //TODO : Handle file I/O exceptions
        //TODO : Filter only .share files?
        public static void ReconstructFileSecret(string[] ShareFilesLocations, byte k, string SecretFileLocation, byte[] key)
        {
            if (ShareFilesLocations.Length == 0 || k == 0)
            {
                throw new System.ArgumentException("Share file locations cannot be empty and k cannot be 0", "ShareFilesNames and k");
            }
            if (ShareFilesLocations.Length < k)
            {
                throw new System.ArgumentException("The number of Share files cannot be less than k", "ShareFilesNames and k");
            }

            try
            {
                Share[][] Shares = new Share[k][];

                //reading file to shares
                for (int i = 0; i < k; i++)
                {
                    using (FileStream fs = new FileStream(ShareFilesLocations[i], FileMode.Open, FileAccess.Read))
                    {
                        byte    X         = (byte)i; //default value
                        Share[] CurShares = new Share[fs.Length - 1];

                        for (int j = 0; j < fs.Length; j++)
                        {
                            //getting the x value in front of the file
                            if (j == 0)
                            {
                                X = (byte)fs.ReadByte();
                            }
                            //convert the rest of file to Share
                            else
                            {
                                CurShares[j - 1] = new Share((Field)X, (Field)fs.ReadByte());
                            }
                        }
                        Shares[i] = CurShares;
                    }
                }

                //reconstruction process
                Field[] Secret      = new Field[Shares[0].Length];
                byte[]  SecretBytes = new byte[Secret.Length];
                for (int i = 0; i < Shares[0].Length; i++)
                {
                    Share[] CurShares = new Share[k];
                    for (int j = 0; j < k; j++)
                    {
                        CurShares[j] = Shares[j][i];
                    }
                    Secret[i]      = ReconstructSecret(CurShares, k);
                    SecretBytes[i] = (byte)Secret[i];
                }
                SecretBytes = AESDecryptBytes(SecretBytes, key);
                //writing secret to file
                using (FileStream fsWrite = new FileStream(SecretFileLocation, FileMode.Create, FileAccess.Write))
                {
                    fsWrite.Write(SecretBytes, 0, SecretBytes.Length);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
예제 #2
0
 public Share(Share S)
 {
     t = new Tuple <Field, Field>(S.GetX(), S.GetY());
 }