Пример #1
0
        public string decrypt(string base64str, PasswordSecure pwd = null)
        {
            if (pwd == null)
            {
                pwd = key;
            }

            var sha     = new SHA3(base64str.Length);
            var openKey = pwd.getObjectValue();
            var crypted = sha.multiDecryptLZMA(Convert.FromBase64String(base64str), openKey);

            BytesBuilder.BytesToNull(openKey);

            var result = new UTF32Encoding().GetString(crypted); //Convert.ToBase64String(crypted);

            BytesBuilder.ToNull(crypted);
            return(result);
        }
Пример #2
0
        public string crypt(string str, PasswordSecure pwd = null)
        {
            if (pwd == null)
            {
                pwd = key;
            }

            var sha = new SHA3(str.Length);

            if (random == null)
            {
                var inits = str + DateTime.Now.ToString("r");
                var t     = new UTF32Encoding().GetBytes(inits);
                var bbi   = new BytesBuilder();
                bbi.add(t);
                bbi.add(sha.CreateInitVector(0, 64, 40));
                var init = bbi.getBytes();

                random = new SHA3.SHA3Random(init);

                bbi.clear();
                BytesBuilder.ClearString(inits);
                BytesBuilder.ToNull(init);
            }

            var bytes = new UTF32Encoding().GetBytes(str);

            var openKey = pwd.getObjectValue();
            var crypted = sha.multiCryptLZMA(bytes, openKey, null, 22, false);

            BytesBuilder.BytesToNull(openKey);
            BytesBuilder.BytesToNull(bytes);
            BytesBuilder.ClearString(str);

            return(Convert.ToBase64String(crypted));
        }
Пример #3
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();
        }