Beispiel #1
0
        private void newToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            saveConfirm(false);
            SaveFile.FileName      = "Unnamed.cnp";
            PublicVar.openFileName = "Crypto Notepad";
            EnterKeyForm f2 = new EnterKeyForm();

            f2.ShowDialog();
            PublicVar.encryptionKey.Set(TypedPassword.Value);
            if (PublicVar.okPressed == false)
            {
                TypedPassword.Value = null;
                return;
            }

            if (PublicVar.okPressed == true)
            {
                PublicVar.okPressed = false;
                if (SaveFile.ShowDialog() != DialogResult.OK)
                {
                    TypedPassword.Value = null;
                    return;
                }

                customRTB.Clear();
                StreamWriter sw             = new StreamWriter(SaveFile.FileName);
                string       NameWithotPath = Path.GetFileName(SaveFile.FileName);
                this.Text       = appName + NameWithotPath;
                filePath        = SaveFile.FileName;
                currentFilename = Path.GetFileName(SaveFile.FileName);
                sw.Close();
            }
            TypedPassword.Value = null;
        }
Beispiel #2
0
        private void openAsotiations()
        {
            EnterKeyForm Form2 = new EnterKeyForm();

            Form2.StartPosition = FormStartPosition.CenterScreen;
            string fileExtension = Path.GetExtension(args[1]);

            if (fileExtension == ".cnp")
            {
                try
                {
                    string NameWithotPath = Path.GetFileName(args[1]);
                    string opnfile        = File.ReadAllText(args[1]);

                    Form2.ShowDialog();
                    if (PublicVar.okPressed == false)
                    {
                        OpenFile.FileName = "";
                        return;
                    }
                    PublicVar.okPressed = false;

                    string de = AES.Decrypt(opnfile, TypedPassword.Value, null, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize);
                    customRTB.Text = de;

                    this.Text = appName + NameWithotPath;

                    filePath = args[1];
                    string cc = customRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
                    PublicVar.encryptionKey.Set(TypedPassword.Value);
                    customRTB.Select(Convert.ToInt32(cc), 0);
                    TypedPassword.Value = null;
                }
                catch (CryptographicException)
                {
                    TypedPassword.Value = null;
                    DialogResult dialogResult = MessageBox.Show("Invalid key!", "Crypto Notepad", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                    if (dialogResult == DialogResult.Retry)
                    {
                        openAsotiations();
                    }
                }
            }
            else
            {
                string opnfile        = File.ReadAllText(args[1]);
                string NameWithotPath = Path.GetFileName(args[1]);
                customRTB.Text = opnfile;
                this.Text      = appName + NameWithotPath;
                string cc2 = customRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
                customRTB.Select(Convert.ToInt32(cc2), 0);
            }

            currentFilename = Path.GetFileName(args[1]);
        }
Beispiel #3
0
        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int    saveCaret      = customRTB.SelectionStart;
            string NameWithotPath = Path.GetFileName(OpenFile.FileName);

            SaveFile.FileName = currentFilename;
            EnterKeyForm f2 = new EnterKeyForm();

            if (string.IsNullOrEmpty(PublicVar.encryptionKey.Get()))
            {
                f2.ShowDialog();
                if (PublicVar.okPressed == false)
                {
                    return;
                }
                PublicVar.okPressed = false;
            }

            if (SaveFile.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            if (TypedPassword.Value == null)
            {
                TypedPassword.Value = PublicVar.encryptionKey.Get();
            }

            filePath = SaveFile.FileName;
            string noenc = customRTB.Text;
            string en;

            en             = AES.Encrypt(customRTB.Text, TypedPassword.Value, null, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize);
            customRTB.Text = en;
            StreamWriter sw = new StreamWriter(filePath);
            int          i  = customRTB.Lines.Count();
            int          j  = 0;

            i = i - 1;
            while (j <= i)
            {
                sw.WriteLine(customRTB.Lines.GetValue(j).ToString());
                j = j + 1;
            }
            sw.Close();
            customRTB.Text = noenc;
            this.Text      = appName + Path.GetFileName(filePath);
            customRTB.Select(Convert.ToInt32(saveCaret), 0);
            PublicVar.encryptionKey.Set(TypedPassword.Value);
            TypedPassword.Value = null;
            currentFilename     = Path.GetFileName(SaveFile.FileName);
        }
Beispiel #4
0
        void DecryptAES()
        {
            EnterKeyForm f2 = new EnterKeyForm();

            f2.ShowDialog();
            if (PublicVar.okPressed == false)
            {
                return;
            }
            if (panel1.Visible == true)
            {
                findToolStripMenuItem2_Click(this, new EventArgs());
            }
            try
            {
                string opnfile        = File.ReadAllText(OpenFile.FileName);
                string NameWithotPath = Path.GetFileName(OpenFile.FileName);

                string de = AES.Decrypt(opnfile, TypedPassword.Value, null, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize);
                customRTB.Text = de;

                this.Text = appName + NameWithotPath;
                filePath  = OpenFile.FileName;
                string cc2 = customRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
                customRTB.Select(Convert.ToInt32(cc2), 0);

                PublicVar.openFileName = Path.GetFileName(OpenFile.FileName);
                currentFilename        = Path.GetFileName(OpenFile.FileName);
                PublicVar.encryptionKey.Set(TypedPassword.Value);
                TypedPassword.Value = null;
            }
            catch (CryptographicException)
            {
                using (new CenterWinDialog(this))
                {
                    TypedPassword.Value = null;
                    DialogResult dialogResult = MessageBox.Show("Invalid key!", "Crypto Notepad", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                    if (dialogResult == DialogResult.Retry)
                    {
                        DecryptAES();
                    }
                    if (dialogResult == DialogResult.Cancel)
                    {
                        return;
                    }
                }
            }
        }
Beispiel #5
0
        public void ContextMenuEncryptReplace()
        {
            PublicVar.openFileName = Path.GetFileName(args[1]);
            DialogResult res = new DialogResult();

            if (args[1].Contains(".cnp"))
            {
                MessageBox.Show("Looks like this file is already encrypted", "Crypto Notepad", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            res = MessageBox.Show("This action will delete the source file and replace it with encrypted version", "Crypto Notepad", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            if (res == DialogResult.Cancel)
            {
                Environment.Exit(0);
            }

            if (!args[1].Contains(".cnp"))
            {
                string opnfile = File.ReadAllText(args[1]);
                customRTB.Text = opnfile;
                string cc2 = customRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
                customRTB.Select(Convert.ToInt32(cc2), 0);
                currentFilename = Path.GetFileName(args[1]);
                string newFile = Path.GetDirectoryName(args[1]) + @"\" + Path.GetFileNameWithoutExtension(args[1]) + ".cnp";

                EnterKeyForm f2 = new EnterKeyForm();
                f2.ShowDialog();
                if (PublicVar.okPressed == false)
                {
                    Application.Exit();
                }
                PublicVar.okPressed = false;

                File.Delete(args[1]);

                string noenc = customRTB.Text;
                string en;
                en             = AES.Encrypt(customRTB.Text, TypedPassword.Value, null, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize);
                customRTB.Text = en;
                StreamWriter sw = new StreamWriter(newFile);
                int          i  = customRTB.Lines.Count();
                int          j  = 0;
                i = i - 1;
                while (j <= i)
                {
                    sw.WriteLine(customRTB.Lines.GetValue(j).ToString());
                    j = j + 1;
                }
                sw.Close();
                PublicVar.encryptionKey.Set(TypedPassword.Value);
                TypedPassword.Value = null;
                filePath            = newFile;
                currentFilename     = Path.GetFileName(newFile);
                this.Text           = appName + currentFilename;
                customRTB.Text      = noenc;
            }

            #region workaround, strange behavior with the cursor in customRTB fix
            customRTB.DetectUrls = false;
            customRTB.DetectUrls = true;
            customRTB.Modified   = false;
            #endregion

            if (PublicVar.okPressed == true)
            {
                PublicVar.okPressed = false;
            }
        }
Beispiel #6
0
        void AutoLock(bool minimize)
        {
            EnterKeyForm f2 = new EnterKeyForm();

            PublicVar.encryptionKey.Set(null);
            caretPos       = customRTB.SelectionStart;
            f2.MinimizeBox = true;
            this.Hide();

            if (minimize == true)
            {
                f2.WindowState = FormWindowState.Minimized;
            }
            f2.ShowDialog();

            if (PublicVar.okPressed == false)
            {
                PublicVar.encryptionKey.Set(null);
                customRTB.Clear();
                this.Text       = appName.Remove(14);
                currentFilename = "";
                filePath        = "";
                this.Show();
                return;
            }
            PublicVar.okPressed = false;

            try
            {
                customRTB.Clear();
                string opnfile = File.ReadAllText(filePath);
                string de      = AES.Decrypt(opnfile, TypedPassword.Value, null, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize);
                customRTB.Text = de;
                this.Text      = appName + currentFilename;
                string cc2 = customRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
                customRTB.Select(Convert.ToInt32(cc2), 0);
                customRTB.SelectionStart = caretPos;
                PublicVar.encryptionKey.Set(TypedPassword.Value);
                TypedPassword.Value = null;
                this.Show();
            }
            catch (Exception ex)
            {
                if (ex is CryptographicException)
                {
                    TypedPassword.Value = null;
                    DialogResult dialogResult = MessageBox.Show("Invalid key!", "Crypto Notepad", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                    if (dialogResult == DialogResult.Retry)
                    {
                        AutoLock(false);
                    }
                    if (dialogResult == DialogResult.Cancel)
                    {
                        PublicVar.encryptionKey.Set(null);
                        customRTB.Clear();
                        this.Text       = appName.Remove(14);
                        filePath        = "";
                        currentFilename = "";
                        this.Show();
                        return;
                    }
                }
            }
        }