Exemplo n.º 1
0
        private void button_CreateShareEncryptedFile_Click(object sender, EventArgs e)
        {
            if (textBox_FileName.Text.Trim().Length == 0)
            {
                MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
                MessageBox.Show("The file name can't be empty.", "Create share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (textBox_PassPhrase.Text.Trim().Length == 0)
            {
                MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
                MessageBox.Show("The pass phrase can't be empty.", "Create share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            AESAccessPolicy policy = new AESAccessPolicy();

            if (textBox_IncludeProcessNames.Text.Trim().Length > 0 || textBox_ExcludeProcessNames.Text.Trim().Length > 0)
            {
                policy.AESFlags |= (uint)AESFlags.Flags_Enabled_Check_ProcessName;
            }

            if (textBox_IncludeUserNames.Text.Trim().Length > 0 || textBox_ExcludeUserNames.Text.Trim().Length > 0)
            {
                policy.AESFlags |= (uint)AESFlags.Flags_Enabled_Check_UserName;
            }

            policy.AESFlags |= (uint)AESFlags.Flags_Enabled_Check_AccessFlags | (uint)AESFlags.Flags_Enabled_Expire_Time;

            string encryptionPassPhrase = textBox_PassPhrase.Text;
            uint   accessFlags          = uint.Parse(textBox_FileAccessFlags.Text);

            policy.AccessFlags                 = accessFlags;
            policy.IncludeProcessNames         = textBox_IncludeProcessNames.Text.Trim();
            policy.LengthOfIncludeProcessNames = (uint)textBox_IncludeProcessNames.Text.Length * 2;
            policy.ExcludeProcessNames         = textBox_ExcludeProcessNames.Text.Trim();
            policy.LengthOfExcludeProcessNames = (uint)textBox_ExcludeProcessNames.Text.Length * 2;
            policy.IncludeUserNames            = textBox_IncludeUserNames.Text.Trim();
            policy.LengthOfIncludeUserNames    = (uint)textBox_IncludeUserNames.Text.Length * 2;
            policy.ExcludeUserNames            = textBox_ExcludeUserNames.Text.Trim();
            policy.LengthOfExcludeUserNames    = (uint)textBox_ExcludeUserNames.Text.Length * 2;
            policy.ExpireTime = dateTimePicker_ExpireTime.Value.ToUniversalTime().ToFileTimeUtc();

            string lastError = string.Empty;

            if (!EncryptionHandler.EncryptFileWithEmbeddedPolicy(textBox_FileName.Text, textBox_PassPhrase.Text, policy, out lastError))
            {
                MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
                MessageBox.Show("Create share encrypted file failed with error:" + lastError, "Process share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error);

                return;
            }
            else
            {
                MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
                MessageBox.Show("Create share encrypted file succeeded.", "Process share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }