示例#1
0
        public static void GetKeyByPassword(DoublePasswordForm pwdForm, out byte[] key, int regime)
        {
            byte[] bytes;

            bytes = Encoding.Unicode.GetBytes(pwdForm.resultText);
            pwdForm.clearResultText();
            pwdForm = null;

/*
 #warning INSECURE
 * File.AppendAllText("unsecure.log", "password: "******"-", "") + "\r\n");
 */
            GetKeyByPassword(regime, bytes, out key);
        }
示例#2
0
        private static void GetPasswordAndDecryptFile(FileInfo fi, out byte[] bt, DoublePasswordForm pwdForm1)
        {
            byte[] key;
            int    regime;

            using (var file = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                file.Position = 1;
                regime        = file.ReadByte();
            }

            GetKeyByPassword(pwdForm1, out key, regime);

/*
 #warning INSECURE
 * File.AppendAllText("unsecure.log", "key: " + BitConverter.ToString(key).Replace("-", "") + "\r\n");
 */
            pwdForm1.clearResultText();

            var sha = new SHA3(fi.Length);

            if (regime < 10)
            {
                sha.useOldDuplex = true;
            }

            var cbt = File.ReadAllBytes(fi.FullName);

            try
            {
                bt = sha.multiDecryptLZMA(cbt, key);
            }
            catch (Exception e)
            {
                MessageBox.Show("Расшифрование не удалось, возможно файл не является файлом программы rtbd. " + e.Message, "Расшифрование не удалось", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                bt = null;
                return;
            }
            finally
            {
                sha.Clear(true);
                BytesBuilder.ToNull(key);
                BytesBuilder.ToNull(cbt);
                cbt = null;
                key = null;
            }
        }
示例#3
0
        public FormCrypt(DoublePasswordForm pwdForm, string fileName) : this()
        {
            ThreadPool.QueueUserWorkItem
            (
                delegate
            {
                Form1.GetKeyByPassword(pwdForm, out key, 41);
                pwdForm.clearResultText();

                if (this.InvokeRequired)
                {
                    try
                    {
                        this.Invoke(new postFileEncryptFunc(endOfPasswordCrypt));
                    }
                    catch
                    {
                        endOfPasswordCrypt();
                    }
                }
                else
                {
                    endOfPasswordCrypt();
                }
            }
            );

            richTextBox1.Text = "Перед шифрованием запомните пароль/ключ (остальные опции запомнятся в зашифрованном файле)\r\n\r\nЖдите, выполняется предварительное криптографическое преобразование пароля в ключ";
            CryptButton.Text  = "Подождите";

            FileName         = fileName;
            FileNameBox.Text = FileName;

            CryptFileName   = FileName + "." + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bdc";
            PostfixBox.Text = CryptFileName;
        }
示例#4
0
        private void recryptFile()
        {
            DoublePasswordForm pwdForm1, pwdForm2 = null;
            bool isSuccess = false;

            do
            {
                pwdForm1 = new DoublePasswordForm(1);
                pwdForm1.ShowDialog();

                if (pwdForm1.resultText == null)
                {
                    return;
                }

                if (pwdForm1.resultText.Length < 6)
                {
                    MessageBox.Show("Извините, но введённый текст настолько мал, что не может являться паролем", "Шифрование", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    continue;
                }

                if (pwdForm1.fromFile)
                {
                    break;
                }

                pwdForm2 = new DoublePasswordForm(2);
                pwdForm2.ShowDialog();

                if (pwdForm2.cancel)
                {
                    pwdForm1.clearResultText();
                    return;
                }

                if (!String.IsNullOrEmpty(pwdForm2.resultText) && pwdForm1.resultText != pwdForm2.resultText)
                {
                    pwdForm1.clearResultText();
                    pwdForm2.clearResultText();
                    if (MessageBox.Show("Введённые пароли не равны друг другу\r\nХотите попробовать ещё раз?", "Шифрование", MessageBoxButtons.YesNo) != System.Windows.Forms.DialogResult.Yes)
                    {
                        return;
                    }
                }
                else
                {
                    isSuccess = true;
                }
            }while (!isSuccess);

            byte[] newKey1;
            Form1.GetKeyByPassword(pwdForm1, out newKey1, 10);
            pwdForm1.clearResultText();
            pwdForm2.clearResultText();

            var sha    = new SHA3(0);
            var newKey = new PasswordSecure(newKey1);

            foreach (var record in records)
            {
                record.Value.pwd   = crypt(decrypt(record.Value.pwd), newKey);
                record.Value.crypt = crypt(decrypt(record.Value.crypt), newKey);
            }

            ClearKey();
            key = newKey;

            recordsToFile();
        }
示例#5
0
        void cnt_closedEventDecrypt(Form1.OpenFileDialogContext context, bool isOK)
        {
            if (!isOK)
            {
                return;
            }

toStart:

            DoublePasswordForm pwdForm1, pwdForm2 = null;
            bool isSuccess = false;

            do
            {
                pwdForm1 = new DoublePasswordForm(1, Path.GetFileName(context.dialog.FileName));
                pwdForm1.ShowDialog();

                if (pwdForm1.resultText == null)
                {
                    return;
                }

                if (pwdForm1.resultText.Length < 6)
                {
                    MessageBox.Show("Извините, но введённый текст настолько мал, что не может являться паролем", "Шифрование", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    continue;
                }

                if (pwdForm1.fromFile)
                {
                    break;
                }

                pwdForm2 = new DoublePasswordForm(2, Path.GetFileName(context.dialog.FileName));
                pwdForm2.ShowDialog();

                if (pwdForm2.cancel)
                {
                    pwdForm1.clearResultText();
                    return;
                }

                if (!String.IsNullOrEmpty(pwdForm2.resultText) && pwdForm1.resultText != pwdForm2.resultText)
                {
                    pwdForm1.clearResultText();
                    pwdForm2.clearResultText();
                    if (MessageBox.Show("Введённые пароли не равны друг другу\r\nХотите попробовать ещё раз?", "Шифрование", MessageBoxButtons.YesNo) != System.Windows.Forms.DialogResult.Yes)
                    {
                        return;
                    }
                }
                else
                {
                    isSuccess = true;
                }
            }while (!isSuccess);

            ClearKey();
            ClearWindow();

            byte[] key1;
            Form1.GetKeyByPassword(pwdForm1, out key1, 22);
            pwdForm1.clearResultText();
            pwdForm2.clearResultText();

            var sha = new SHA3(0);

            var fi = new FileInfo(context.dialog.FileName);

            fileName = fi.FullName;
            if (!fi.Exists)
            {
                var newBytes = sha.multiCryptLZMA(new byte[0], key1, null, 12, false, 0, SHA3.getHashCountForMultiHash() - 8);
                File.WriteAllBytes(fileName, newBytes);
            }

            try
            {
                var openFile      = File.ReadAllBytes(fileName);
                var decryptedFile = sha.multiDecryptLZMA(openFile, key1);

                if (decryptedFile == null)
                {
                    MessageBox.Show("Файл расшифровать не удалось");
                    BytesBuilder.BytesToNull(key1);
                    goto toStart;
                }

                this.key = new PasswordSecure(key1);
                var str = Encoding.UTF32.GetString(decryptedFile);

                BytesBuilder.ToNull(openFile);
                BytesBuilder.ToNull(decryptedFile);

                GetRecordsFromFile(str);
                BytesBuilder.ClearString(str);
            }
            catch (Exception e)
            {
                MessageBox.Show("Расшифрование не удалось! " + e.Message, "Расшифрование не удалось", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            SetTagsAndMainTags();
        }
示例#6
0
        public static bool GetPasswordAndDecryptFile(Form1.OpenFileDialogContext context, out FileInfo fi, out byte[] bt, bool AcceptEmptyPassword = false, bool noMsg = false, bool keyFromFile = false, onEnd ended = null)
        {
            fi = new FileInfo(context.dialog.FileName);

            int countOfTryes = -1;

            bt = null;

            DoublePasswordForm pwdForm1, pwdForm2 = null;

            do
            {
                countOfTryes++;

                pwdForm1 = new DoublePasswordForm(1, fi.Name);
                pwdForm1.ShowDialog();

                if (pwdForm1.resultText == null)
                {
                    return(false);
                }

                if (pwdForm1.fromFile)
                {
                    break;
                }

                if (AcceptEmptyPassword && pwdForm1.resultText.Length == 0)
                {
                    return(true);
                }

                if (!AcceptEmptyPassword && pwdForm1.resultText.Length < 6)
                {
                    MessageBox.Show("Извините, но введённый текст настолько мал, что не может являться паролем", "Шифрование", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    continue;
                }

                if (fi.Length > 1024 * 1024 || countOfTryes > 1)
                {
                    pwdForm2 = new DoublePasswordForm(2, fi.Name, true);
                    pwdForm2.ShowDialog();

                    if (pwdForm2.cancel)
                    {
                        pwdForm1.clearResultText();
                        return(false);
                    }

                    if (!String.IsNullOrEmpty(pwdForm2.resultText) && pwdForm1.resultText != pwdForm2.resultText)
                    {
                        pwdForm1.clearResultText();
                        pwdForm2.clearResultText();
                        if (MessageBox.Show("Введённые пароли не равны друг другу\r\nХотите попробовать ещё раз?", "Шифрование", MessageBoxButtons.YesNo) != System.Windows.Forms.DialogResult.Yes)
                        {
                            return(false);
                        }

                        continue;
                    }

                    pwdForm2.clearResultText();
                }

                break;
            }while (true);

            GC.Collect();

            if (ended == null)
            {
                GetPasswordAndDecryptFile(fi, out bt, pwdForm1);

                if (bt == null)
                {
                    if (!noMsg)
                    {
                        if (MessageBox.Show("Расшифровка файла '" + fi.Name + "' не удалась - возможно, введён неверный пароль", "Расшифровка", MessageBoxButtons.OK, MessageBoxIcon.Stop) == System.Windows.Forms.DialogResult.Retry)
                        {
                            return(false);   // continue;
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }

                return(true);
            }
            else
            {
                var fif = fi;
                #if forLinux
                {
                    byte[] bd;
                    GetPasswordAndDecryptFile(fif, out bd, pwdForm1);

                    ended(fif, bd);

                    if (bd == null)
                    {
                        if (!noMsg)
                        {
                            MessageBox.Show("Расшифровка файла '" + fif.Name + "' не удалась - возможно, введён неверный пароль", "Расшифровка", MessageBoxButtons.OK, MessageBoxIcon.Stop); // == System.Windows.Forms.DialogResult.Retry
                        }
                    }
                }
                #else
                ThreadPool.QueueUserWorkItem
                    (delegate
                {
                    byte[] bd;
                    GetPasswordAndDecryptFile(fif, out bd, pwdForm1);

                    ended(fif, bd);

                    if (bd == null)
                    {
                        if (!noMsg)
                        {
                            MessageBox.Show("Расшифровка файла '" + fif.Name + "' не удалась - возможно, введён неверный пароль", "Расшифровка", MessageBoxButtons.OK, MessageBoxIcon.Stop); // == System.Windows.Forms.DialogResult.Retry
                        }
                    }
                }
                    );
                #endif
                return(true);
            }
        }
示例#7
0
        void cnt_closedEvent(Form1.OpenFileDialogContext context, bool isOk)
        {
            if (!isOk)
            {
                return;
            }

            DoublePasswordForm pwdForm1, pwdForm2 = null;
            bool isSuccess = false;

            do
            {
                pwdForm1 = new DoublePasswordForm(1, Path.GetFileName(context.dialog.FileName));
                pwdForm1.ShowDialog();

                if (pwdForm1.resultText == null)
                {
                    return;
                }

                if (pwdForm1.resultText.Length < 6)
                {
                    MessageBox.Show("Извините, но введённый текст настолько мал, что не может являться паролем", "Шифрование", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    continue;
                }

                if (pwdForm1.fromFile)
                {
                    break;
                }

                pwdForm2 = new DoublePasswordForm(2, Path.GetFileName(context.dialog.FileName), true);
                pwdForm2.ShowDialog();

                if (pwdForm2.cancel)
                {
                    pwdForm1.clearResultText();
                    return;
                }

                if (/*!String.IsNullOrEmpty(pwdForm2.resultText) && */ pwdForm1.resultText != pwdForm2.resultText)
                {
                    pwdForm1.clearResultText();
                    pwdForm2.clearResultText();
                    if (MessageBox.Show("Введённые пароли не равны друг другу\r\nХотите попробовать ещё раз?", "Шифрование", MessageBoxButtons.YesNo) != System.Windows.Forms.DialogResult.Yes)
                    {
                        return;
                    }
                }
                else
                {
                    isSuccess = true;
                }
            }while (!isSuccess);

            if (pwdForm2 != null)
            {
                pwdForm2.clearResultText();
            }

            new FormCrypt(pwdForm1, context.dialog.FileName).Show();
        }