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; } }
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); }
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(); }