コード例 #1
0
ファイル: MainForm.cs プロジェクト: ThatSlyGuy/SecretSplitter
        private void CreateSecretFileShares()
        {
            if (!TryVerifyThresholdValues())
            {
                return;
            }

            if (!File.Exists(txtSecretFilePath.Text))
            {
                ShowError("Secret file does not exist", "File Not Found");
                return;
            }

            if (String.IsNullOrEmpty(txtMasterKey.Text))
            {
                ShowError("A valid key is required to encrypt a file.", "Invalid Key Length");
                return;
            }

            byte[] keyBytes;
            if (!SecretEncoder.TryParseHexString(txtMasterKey.Text, out keyBytes))
            {
                ShowError("The key must contain all hexadecimal characters.", "Invalid Key Character");
                return;
            }

            SplitSecret splitSecret = SecretSplitter.SplitFile(keyBytes, (int)nudThreshold.Value);

            saveFileDialog.Filter           = DialogFilter;
            saveFileDialog.Title            = "Save Encrypted File";
            saveFileDialog.InitialDirectory = Path.GetDirectoryName(txtSecretFilePath.Text);
            saveFileDialog.FileName         = Path.GetFileName(Path.ChangeExtension(txtSecretFilePath.Text, ".splitsecret"));
            saveFileDialog.AddExtension     = true;

            ShowInfo("You'll now have to specify where to save the encrypted file.", "Specify Encrypted File Path");
            if (saveFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            splitSecret.EncryptFile(txtSecretFilePath.Text, saveFileDialog.FileName);

            var sb = new StringBuilder();

            sb.AppendLine("Here are your secret pieces that form the decryption key for the encrypted file located at:");
            sb.AppendLine(saveFileDialog.FileName);
            sb.AppendLine();
            int shareWidth = saveFileDialog.FileName.Length;

            foreach (var currentShare in splitSecret.GetShares((int)nudShares.Value))
            {
                string currentShareText = currentShare.ToString();
                sb.AppendLine(currentShareText);
                shareWidth = Math.Max(shareWidth, currentShareText.Length);
                sb.AppendLine();
            }

            SetWidthFromShareLength(shareWidth);

            sb.AppendLine("When distributing the secret pieces, remember to also make sure that you distribute the encrypted file. It's safe to email the encrypted file, but you should securely distribute the secret pieces (i.e. in person).");
            sb.AppendLine();
            sb.AppendLine("Make sure that each person knows that exactly " + ((int)nudThreshold.Value) + " pieces are required to reconstruct the file.");

            txtShares.Text   = sb.ToString();
            tabs.SelectedTab = tabRecover;
        }