示例#1
0
        private void button_StartToDecrypt_Click(object sender, EventArgs e)
        {
            try
            {
                string fileName       = textBox_FileNameToDecrypt.Text;
                string passwordPhrase = textBox_PasswordToDecrypt.Text;
                byte[] key            = Utils.GetKeyByPassPhrase(passwordPhrase);

                if (!FilterAPI.AESEncryptFile(fileName, (uint)key.Length, key, (uint)FilterAPI.DEFAULT_IV_TAG.Length, FilterAPI.DEFAULT_IV_TAG, false))
                {
                    string lastError = "Decrypt file " + fileName + " failed with error:" + FilterAPI.GetLastErrorMessage();
                    ShowMessage(lastError, MessageBoxIcon.Error);
                }
                else
                {
                    string lastError = "Decrypt file " + fileName + " succeeded.";
                    ShowMessage(lastError, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                string lastError = "Decrypt file failed with error:" + ex.Message;
                ShowMessage(lastError, MessageBoxIcon.Error);
            }
        }
示例#2
0
        private void button_Start_Click(object sender, EventArgs e)
        {
            string passPhrase = textBox_PassPhrase.Text.Trim();
            string fileName   = textBox_FileName.Text.Trim();
            string lastError  = string.Empty;
            bool   retVal     = false;

            if (passPhrase.Length == 0)
            {
                MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
                MessageBox.Show("Password phrase can't be empty.", "Encryption", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (fileName.Length == 0)
            {
                MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
                MessageBox.Show("File name can't be empty.", "Encryption", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            byte[] key = Utils.GetKeyByPassPhrase(passPhrase);
            byte[] iv  = Utils.GetRandomIV();

            if (checkBox_UseDefaultIV.Checked)
            {
                iv = FilterAPI.DEFAULT_IV_TAG;
            }


            if (encryptType == FilterAPI.EncryptType.Encryption)
            {
                retVal = FilterAPI.AESEncryptFile(fileName, (uint)key.Length, key, (uint)iv.Length, iv, true);
            }
            else
            {
                if (!checkBox_UseDefaultIV.Checked)
                {
                    uint accessFlags = 0;
                    retVal = FilterAPI.GetIVTag(fileName, ref iv, ref accessFlags, out lastError);

                    if (!retVal)
                    {
                        MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
                        MessageBox.Show(" Get file " + fileName + " IV tag failed with error:" + lastError, encryptType.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }

                retVal = FilterAPI.AESDecryptFile(fileName, (uint)key.Length, key, (uint)iv.Length, iv);
            }


            if (!retVal)
            {
                string errorMessage = FilterAPI.GetLastErrorMessage();

                MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
                MessageBox.Show(encryptType.ToString() + " file " + fileName + " failed with error:" + errorMessage, encryptType.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);

                return;
            }
            else
            {
                MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
                MessageBox.Show(encryptType.ToString() + " file " + fileName + " succeeded.", encryptType.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }