// @file FormMySecret.cs
// @version 4.0.1a (2026-03-16T09:17Z)
// @author David Ireland <https://cryptosys.net/contact>
// @copyright 2007-26 DI Management Services Pty Ltd
// @license Apache-2.0

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;

namespace MySecretForm
{
    public partial class FormMySecret : Form
    {
        public FormMySecret()
        {
            InitializeComponent();
            lblVersion.Text = lblVersion.Text + MySecret.CrSysVersion();
            // Test data...
            txtPlain.Text = @"KING RICHARD. A horse! a horse! my kingdom for a horse!
CATESBY. Withdraw, my lord! I'll help you to a horse.
KING RICHARD. Slave, I have set my life upon a cast
And I Will stand the hazard of the die.
I think there be six Richmonds in the field;
Five have I slain to-day instead of him.
A horse! a horse! my kingdom for a horse!             Exeunt";
            txtPassword.Text = "password";
        }

        private void FormMySecret_Load(object sender, EventArgs e)
        {
            // Add a link to the LinkLabel.
            LinkLabel.Link link = new LinkLabel.Link();
            link.LinkData = "https://di-mgt.com.au/";
            linkLabel_dimgt.Links.Add(link);
            LinkLabel.Link link1 = new LinkLabel.Link();
            link1.LinkData = "https://cryptosys.net/";
            linkLabel_cryptosys.Links.Add(link1);
        }

        private void linkLabel_dimgt_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            // Send the URL to the operating system.
            System.Diagnostics.Process.Start(e.Link.LinkData as string);
        }
        private void linkLabel_cryptosys_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            // Send the URL to the operating system.
            System.Diagnostics.Process.Start(e.Link.LinkData as string);

        }

        private void cmdClearAll_Click(object sender, EventArgs e)
        {
            txtPlain.Text = "";
            txtCipher.Text = "";
            txtPassword.Text = "";
            chkUseVer3.Checked = false;
        }

        private void cmdEncrypt_Click(object sender, EventArgs e)
        {
            // Use a mutable StringBuilder for sensitive data
            StringBuilder sbPassword = new StringBuilder(txtPassword.Text ?? "");
            StringBuilder sbPlain = new StringBuilder(txtPlain.Text ?? "");
            string strCipher;

            if (sbPassword.Length == 0) {
                MessageBox.Show("Password is not set!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtPassword.Focus();
                return;
            }

            if (sbPlain.Length == 0) {
                MessageBox.Show("Nothing to encrypt!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtPlain.Focus();
                return;
            }

            if (chkUseVer3.Checked) {
                strCipher = MySecret.MySecretEncryptV3(sbPlain.ToString(), sbPassword.ToString());
            } else {
                strCipher = MySecret.MySecretEncryptV4(sbPlain.ToString(), sbPassword.ToString());
            }
            txtCipher.Text = strCipher;

            // Clean up local variables
            CryptoSysAPI.Wipe.String(sbPassword);
            CryptoSysAPI.Wipe.String(sbPlain);

            if (strCipher.Length == 0) {
                MessageBox.Show("Encryption error.", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void cmdDecrypt_Click(object sender, EventArgs e)
        {
            // Use a mutable StringBuilder for sensitive data
            StringBuilder sbPassword = new StringBuilder(txtPassword.Text ?? "");
            StringBuilder sbCipher = new StringBuilder(txtCipher.Text ?? "");
            string strPlain;

            if (sbPassword.Length == 0) {
                MessageBox.Show("Password is not set!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtPassword.Focus();
                return;
            }

            if (sbCipher.Length == 0) {
                MessageBox.Show("Nothing to decrypt!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtCipher.Focus();
                return;
            }

            strPlain = MySecret.MySecretDecrypt(sbCipher.ToString(), sbPassword.ToString());
            txtPlain.Text = strPlain;

            if (strPlain.Length == 0) {
                MessageBox.Show("Decryption error.", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            // Clean up local variables
            CryptoSysAPI.Wipe.String(sbPassword);
            CryptoSysAPI.Wipe.String(sbCipher);
        }

    }
}