示例#1
0
        private void button_StartToDecrypt_Click(object sender, EventArgs e)
        {
            try
            {
                string fileName       = textBox_DecryptSourceName.Text;
                string targetFileName = textBox_DecryptTargetName.Text;

                byte[] key = Utils.GetKeyByPassPhrase(GlobalConfig.MasterPassword, 32);

                bool retVal = false;
                if (fileName.Equals(targetFileName, StringComparison.CurrentCulture))
                {
                    retVal = FilterAPI.AESDecryptFile(fileName, (uint)key.Length, key, (uint)FilterAPI.DEFAULT_IV_TAG.Length, FilterAPI.DEFAULT_IV_TAG);
                }
                else
                {
                    retVal = FilterAPI.AESDecryptFileToFile(fileName, targetFileName, (uint)key.Length, key, (uint)FilterAPI.DEFAULT_IV_TAG.Length, FilterAPI.DEFAULT_IV_TAG);
                }

                if (!retVal)
                {
                    string lastError = "Decrypt file " + fileName + " failed with error:" + FilterAPI.GetLastErrorMessage();
                    ShowMessage(lastError, MessageBoxIcon.Error);
                }
                else
                {
                    string lastError = "Decrypt file " + fileName + " to " + targetFileName + " 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 targetFileName = textBox_TargetFileName.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, 32);
            byte[] iv  = Utils.GetRandomIV();

            if (encryptType == FilterAPI.EncryptType.Encryption)
            {
                if (fileName.Equals(targetFileName, StringComparison.CurrentCulture))
                {
                    retVal = FilterAPI.AESEncryptFileWithTag(fileName, (uint)key.Length, key, (uint)iv.Length, iv, (uint)iv.Length, iv);
                }
                else
                {
                    retVal = FilterAPI.AESEncryptFileToFileWithTag(fileName, targetFileName, (uint)key.Length, key, (uint)iv.Length, iv, (uint)iv.Length, iv);
                }
            }
            else
            {
                uint ivLength = (uint)iv.Length;
                retVal = FilterAPI.GetAESTagData(fileName, ref ivLength, iv);

                if (!retVal)
                {
                    MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
                    MessageBox.Show("Decrypt file failed with error " + FilterAPI.GetLastErrorMessage(), "Decryption", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (fileName.Equals(targetFileName, StringComparison.CurrentCulture))
                {
                    retVal = FilterAPI.AESDecryptFile(fileName, (uint)key.Length, key, (uint)iv.Length, iv);
                }
                else
                {
                    retVal = FilterAPI.AESDecryptFileToFile(fileName, targetFileName, (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);
            }
        }