private bool AddNewFileDRInfoToServer(ref string iv, ref string key, ref long creationTime) { bool retVal = false; string lastError = string.Empty; try { iv = string.Empty; key = string.Empty; creationTime = 0; DRPolicy drPolicy = GetDRSetting(); string encryptedDRPolicy = DigitalRightControl.EncryptObjectToStr <DRPolicy>(drPolicy); retVal = WebAPIServices.AddShareFile(encryptedDRPolicy, ref creationTime, ref key, ref iv, ref lastError); if (!retVal) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Create share encrypted file failed with error:" + lastError, "Process share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error); return(retVal); } } catch (Exception ex) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Get encryption key info failed with error:" + ex.Message, "GetEncryptionKeyAndIVFromServer", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(retVal); }
public ShareFileForm(DRPolicy _selectedDRPolicy) { InitializeComponent(); this.selectedDRPolicy = _selectedDRPolicy; InitializeDRSetting(); }
private DRPolicy GetDRSetting() { DRPolicy drPolicy = new DRPolicy(); try { drPolicy.AuthorizedProcessNames = textBox_authorizedProcessNames.Text.Trim().ToLower(); drPolicy.UnauthorizedProcessNames = textBox_UnauthorizedProcessNames.Text.Trim().ToLower(); drPolicy.AuthorizedUserNames = textBox_AuthorizedUserNames.Text.Trim().ToLower(); drPolicy.UnauthorizedUserNames = textBox_UnauthorizedUserNames.Text.Trim().ToLower(); DateTime expireDate = dateTimePicker_ExpireDate.Value.Date + dateTimePicker_ExpireTime.Value.TimeOfDay; drPolicy.FileName = Path.GetFileName(textBox_TargetName.Text); drPolicy.ExpireTime = expireDate.ToFileTime(); if (null != selectedDRPolicy) { drPolicy.EncryptionIV = selectedDRPolicy.EncryptionIV; } } catch (Exception ex) { throw new Exception("Apply digital right failed with error:" + ex.Message); } return(drPolicy); }
private void button_Delete_Click(object sender, EventArgs e) { button_Delete.Enabled = false; try { if (listView_SharedFiles.SelectedItems.Count != 1) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Please select a file.", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string creationTimeStr = (string)listView_SharedFiles.SelectedItems[0].Tag; if (sharedFileList.ContainsKey(creationTimeStr)) { DRPolicy drPolicy = sharedFileList[creationTimeStr]; MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); if (MessageBox.Show("Are you sure you want to delete the file " + drPolicy.FileName + " ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != System.Windows.Forms.DialogResult.Yes) { return; } string fileName = drPolicy.FileName + "." + creationTimeStr; string lastError = string.Empty; //if (!WebFormServices.DeleteShareFile(AccountForm.accountName, AccountForm.password, fileName, ref lastError)) //{ // MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); // MessageBox.Show("Delete shared file " + selectedDRPolicy.FileName + " failed with error:" + lastError, "DeleteSharedFile", MessageBoxButtons.OK, MessageBoxIcon.Error); // return; //} sharedFileList.Remove(creationTimeStr); } InitListView(); } catch (Exception ex) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Delete shared file " + selectedDRPolicy.FileName + " failed with error " + ex.Message, "DeleteSharedFile", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { button_Delete.Enabled = true; } }
private void button_Apply_Click(object sender, EventArgs e) { button_Apply.Enabled = false; try { string lastError = string.Empty; if (isNewFile) { if (CreateShareEncryptFile()) { this.Close(); } } else { DRPolicy newDRPolicy = GetDRSetting(); newDRPolicy.CreationTime = selectedDRPolicy.CreationTime; string encryptedDRPolicy = string.Empty; encryptedDRPolicy = DigitalRightControl.EncryptObjectToStr <DRPolicy>(newDRPolicy); //if (!WebFormServices.SetFileDRInfo(AccountForm.accountName, AccountForm.password, encryptedDRPolicy, ref lastError)) //{ // MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); // MessageBox.Show("Apply digital right information for file " + selectedDRPolicy.FileName + " failed with error " + lastError, "Apply", MessageBoxButtons.OK, MessageBoxIcon.Error); // return; //} //else { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Apply digital right information for file " + selectedDRPolicy.FileName + " succeeded.", "Apply", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); return; } } } catch (Exception ex) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Apply digital right information for file " + selectedDRPolicy.FileName + " failed with error " + ex.Message, "Apply", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { button_Apply.Enabled = true; } }
public bool GetSharedFileList() { string lastError = string.Empty; string encryptFileList = string.Empty; Dictionary <string, DRPolicy> shareList = new Dictionary <string, DRPolicy>(); //bool retVal = WebFormServices.GetFileList(AccountForm.accountName, AccountForm.password, ref encryptFileList, ref lastError); //if (!retVal) //{ // MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); // MessageBox.Show(lastError, "GetFileList", MessageBoxButtons.OK, MessageBoxIcon.Error); // return false; //} //else { if (encryptFileList.Length > 0) { List <string> decrypFileList = DigitalRightControl.DecryptStrToObject <List <string> >(encryptFileList); foreach (string name in decrypFileList) { if (name.Length > 0) { //the extension of the file is the creation time. string creationTimeStr = Path.GetExtension(name).Substring(1); string fileName = Path.GetFileNameWithoutExtension(name); DRPolicy drPolicy = new DRPolicy(); drPolicy.FileName = fileName; drPolicy.CreationTime = long.Parse(creationTimeStr); drPolicy.ExpireTime = 0; shareList.Add(creationTimeStr, drPolicy); } } } sharedFileList = shareList; InitListView(); } return(true); }
private bool AddNewFileDRInfoToServer(ref string iv, ref string key, ref long creationTime) { bool retVal = false; string lastError = string.Empty; try { iv = string.Empty; key = string.Empty; creationTime = 0; if (AccountForm.accountName.Length == 0 || AccountForm.password.Length == 0) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Can't enable revoke access feature for guest user!", "Create share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error); return(retVal); } selectedDRPolicy = GetDRSetting(); string encryptedDRPolicy = DigitalRightControl.EncryptObjectToStr <DRPolicy>(selectedDRPolicy); //retVal = WebFormServices.AddNewFile(AccountForm.accountName, AccountForm.password, encryptedDRPolicy, ref creationTime, ref key, ref iv, ref lastError); //if (!retVal) //{ // MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); // MessageBox.Show("Create share encrypted file failed with error:" + lastError, "Process share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error); // return retVal; //} //else { selectedDRPolicy.CreationTime = creationTime; isNewFileAddedToServer = true; } } catch (Exception ex) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Get encryption key info failed with error:" + ex.Message, "GetEncryptionKeyAndIVFromServer", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(retVal); }
public ShareFileForm(DRPolicy drPolicy) { InitializeComponent(); textBox_FileName.Text = drPolicy.FileName; selectedDRPolicy = drPolicy; textBox_FileName.Enabled = false; textBox_TargetName.Enabled = false; button_OpenFile.Enabled = false; dateTimePicker_ExpireDate.Value = DateTime.FromFileTime(drPolicy.ExpireTime); dateTimePicker_ExpireTime.Value = DateTime.FromFileTime(drPolicy.ExpireTime); textBox_authorizedProcessNames.Text = drPolicy.AuthorizedProcessNames; textBox_UnauthorizedProcessNames.Text = drPolicy.UnauthorizedProcessNames; textBox_AuthorizedUserNames.Text = drPolicy.AuthorizedUserNames; textBox_UnauthorizedUserNames.Text = drPolicy.UnauthorizedUserNames; button_CreateFile.Text = "Apply change"; }
private void toolStripButton_ModifyShareFile_Click(object sender, EventArgs e) { if (listView_SharedFiles.SelectedItems.Count != 1) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Please select a file.", "Edit", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } DRPolicy drPolicy = (DRPolicy)listView_SharedFiles.SelectedItems[0].Tag; ShareFileForm shareFileForm = new ShareFileForm(drPolicy); shareFileForm.Text = "EaseFilter Shared File Modification"; if (shareFileForm.ShowDialog() == System.Windows.Forms.DialogResult.OK) { GetSharedFileList(); InitShareFileListView(); } }
private void button_EditSharedFile_Click(object sender, EventArgs e) { if (listView_SharedFiles.SelectedItems.Count != 1) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Please select a file.", "Edit", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string creationTimeStr = (string)listView_SharedFiles.SelectedItems[0].Tag; if (sharedFileList.ContainsKey(creationTimeStr)) { DRPolicy drPolicy = sharedFileList[creationTimeStr]; if (drPolicy.ExpireTime == 0) { string fileName = drPolicy.FileName; long creationTime = drPolicy.CreationTime; string lastError = string.Empty; string encryptedDRPolicy = string.Empty; //bool retVal = WebFormServices.GetFileDRInfo(AccountForm.accountName, AccountForm.password, fileName, creationTime, ref encryptedDRPolicy, ref lastError); //if (!retVal) //{ // MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); // MessageBox.Show("Get digital right information for file " + fileName + " failed with error:" + lastError, "GetFileDRInfo", MessageBoxButtons.OK, MessageBoxIcon.Error); // return; //} drPolicy = DigitalRightControl.DecryptStrToObject <DRPolicy>(encryptedDRPolicy); drPolicy.CreationTime = creationTime; sharedFileList[creationTimeStr] = drPolicy; } ShareFileForm shareFileForm = new ShareFileForm(drPolicy); shareFileForm.ShowDialog(); } }
private void toolStripButton_RemoveShareFile_Click(object sender, EventArgs e) { if (listView_SharedFiles.SelectedItems.Count != 1) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Please select a file.", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } DRPolicy drPolicy = (DRPolicy)listView_SharedFiles.SelectedItems[0].Tag; string lastError = string.Empty; if (!WebAPIServices.DeleteShareFile(drPolicy.EncryptionIV, ref lastError)) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Delete shared file " + selectedDRPolicy.FileName + " failed with error:" + lastError, "DeleteSharedFile", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } GetSharedFileList(); }
private DRPolicy GetDRSetting() { DRPolicy drPolicy = new DRPolicy(); try { drPolicy.IncludeProcessNames = textBox_IncludeProcessNames.Text.Trim().ToLower(); drPolicy.ExcludeProcessNames = textBox_ExcludeProcessNames.Text.Trim().ToLower(); drPolicy.IncludeUserNames = textBox_IncludeUserNames.Text.Trim().ToLower(); drPolicy.ExcludeUserNames = textBox_ExcludeUserNames.Text.Trim().ToLower(); drPolicy.IncludeComputerIds = textBox_IncludeComputerIds.Text.Trim(); DateTime expireDate = dateTimePicker_ExpireDate.Value.Date + dateTimePicker_ExpireTime.Value.TimeOfDay; drPolicy.ExpireTime = expireDate.ToUniversalTime().ToFileTime(); drPolicy.FileName = Path.GetFileName(textBox_OutputFilePath.Text); drPolicy.UserPassword = textBox_UserPassword.Text.Trim(); } catch (Exception ex) { throw new Exception("Apply digital right failed with error:" + ex.Message); } return(drPolicy); }
private bool CreateOrModifyShareEncryptFile() { string lastError = string.Empty; string authorizedProcessNames = textBox_authorizedProcessNames.Text.Trim(); string unauthorizedProcessNames = textBox_UnauthorizedProcessNames.Text.Trim(); string authorizedUserNames = textBox_AuthorizedUserNames.Text.Trim(); string unauthorizedUserNames = textBox_UnauthorizedUserNames.Text.Trim(); string fileName = textBox_FileName.Text.Trim(); string targetFileName = textBox_TargetName.Text; try { if (fileName.Length == 0) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("The file name can't be empty.", "Create share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } DateTime expireDateTime = dateTimePicker_ExpireDate.Value.Date + dateTimePicker_ExpireTime.Value.TimeOfDay; if (expireDateTime <= DateTime.Now) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("The expire time can't be less than current time.", "Create share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } if (selectedDRPolicy != null) { DRPolicy drPolicy = GetDRSetting(); string encryptedDRPolicy = DigitalRightControl.EncryptObjectToStr <DRPolicy>(drPolicy); if (WebAPIServices.ModifySharedFileDRInfo(encryptedDRPolicy, ref lastError)) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Modify shared file " + textBox_FileName.Text + " policy succeeded.", "Modify shared file", MessageBoxButtons.OK, MessageBoxIcon.Information); return(true); } else { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Modify shared file " + textBox_FileName.Text + " policy failed with error:" + lastError, "Modify shared file", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } } //here we generate the random unique IV and key, you can use your own key and iv byte[] encryptionIV = Utils.GetRandomIV(); byte[] encryptionKey = Utils.GetRandomKey(); string keyStr = string.Empty; string ivStr = string.Empty; if (GlobalConfig.StoreSharedFileMetaDataInServer) { long creationTime = DateTime.Now.ToFileTime(); //send the encrypted file digital right information to the server and get back the iv and key. if (!AddNewFileDRInfoToServer(ref ivStr, ref keyStr, ref creationTime)) { return(false); } if (ivStr.Length > 0 && keyStr.Length > 0) { encryptionIV = Utils.ConvertHexStrToByteArray(ivStr); encryptionKey = Utils.ConvertHexStrToByteArray(keyStr); } } //for this example, we add the encryptIV and account name as the tag data to the encrypted file //you can add your own custom tag data to the encyrpted file, so when someone open the encrypted file, you will get the tag data. string tagStr = GlobalConfig.AccountName + ";" + ivStr; byte[] tagData = UnicodeEncoding.Unicode.GetBytes(tagStr); bool retVal = false; if (fileName.Equals(targetFileName, StringComparison.CurrentCulture)) { retVal = FilterAPI.AESEncryptFileWithTag(fileName, (uint)encryptionKey.Length, encryptionKey, (uint)encryptionIV.Length, encryptionIV, (uint)tagData.Length, tagData); } else { retVal = FilterAPI.AESEncryptFileToFileWithTag(fileName, targetFileName, (uint)encryptionKey.Length, encryptionKey, (uint)encryptionIV.Length, encryptionIV, (uint)tagData.Length, tagData); } if (!retVal) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Create encrypted file " + targetFileName + " failed with error:" + FilterAPI.GetLastErrorMessage(), "Create share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error); if (GlobalConfig.StoreSharedFileMetaDataInServer) { WebAPIServices.DeleteShareFile(ivStr, ref lastError); } if (!fileName.Equals(targetFileName, StringComparison.CurrentCulture)) { File.Delete(targetFileName); } return(false); } else { //set this flag to the encrypted file, require to get permission from user mode when the file open if (!FilterAPI.SetHeaderFlags(targetFileName, (uint)AESFlags.Flags_Request_IV_And_Key_From_User, FilterAPI.ALLOW_MAX_RIGHT_ACCESS)) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("SetHeaderFlags for file " + targetFileName + " failed with error:" + FilterAPI.GetLastErrorMessage(), "SetHeaderFlags", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } if (!GlobalConfig.StoreSharedFileMetaDataInServer) { //add the permission meta data to a file and store it in the server, it will be used when the file open. if (!DRServer.AddDRInfoToFile(targetFileName, authorizedProcessNames, unauthorizedProcessNames, authorizedUserNames, unauthorizedUserNames , expireDateTime, encryptionIV, encryptionKey, FilterAPI.ALLOW_MAX_RIGHT_ACCESS.ToString())) { return(false); } } MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); string message = "Create encrypted file " + targetFileName + " succeeded, you can distribute this encrypted file to your client.\r\n\r\nDownload this file to the share file drop folder in the client,"; message += " then start the filter service there, now you can open the encrypted file if the process in client has the permission."; MessageBox.Show(message, "Share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Information); } return(true); } catch (Exception ex) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Create share file failed with error " + ex.Message, "Create share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } }
public void InitShareFileListView() { textBox_SharedFileDropFolder.Text = GlobalConfig.ShareFolder; listView_SharedFiles.Clear(); //create column header for ListView listView_SharedFiles.Columns.Add("FileName", 150, System.Windows.Forms.HorizontalAlignment.Left); listView_SharedFiles.Columns.Add("CreationTime", 100, System.Windows.Forms.HorizontalAlignment.Left); listView_SharedFiles.Columns.Add("ExpireTime", 100, System.Windows.Forms.HorizontalAlignment.Left); listView_SharedFiles.Columns.Add("AuthorizedProcessNames", 100, System.Windows.Forms.HorizontalAlignment.Left); listView_SharedFiles.Columns.Add("UnauthorizedProcessNames", 100, System.Windows.Forms.HorizontalAlignment.Left); listView_SharedFiles.Columns.Add("AuthorizedUserNames", 100, System.Windows.Forms.HorizontalAlignment.Left); listView_SharedFiles.Columns.Add("UnauthorizedUserNames", 100, System.Windows.Forms.HorizontalAlignment.Left); try { foreach (DRPolicy sharedFile in sharedFileList.Values) { int index = 0; ListViewItem lvItem = new ListViewItem(); string[] listEntry = new string[listView_SharedFiles.Columns.Count]; string fileName = sharedFile.FileName; if (fileName.EndsWith(GlobalConfig.ShareFileExt)) { fileName = fileName.Replace(GlobalConfig.ShareFileExt, ""); } listEntry[index++] = fileName; long dateTimeN = sharedFile.CreationTime; DateTime dateTimeD = DateTime.FromFileTime(dateTimeN); listEntry[index++] = dateTimeD.ToShortDateString();// String.Format("{0:F}", dateTimeD); dateTimeN = sharedFile.ExpireTime; dateTimeD = DateTime.FromFileTime(dateTimeN); listEntry[index++] = dateTimeD.ToShortDateString();//String.Format("{0:F}", dateTimeD); listEntry[index++] = sharedFile.AuthorizedProcessNames; listEntry[index++] = sharedFile.UnauthorizedProcessNames; listEntry[index++] = sharedFile.AuthorizedUserNames; listEntry[index++] = sharedFile.UnauthorizedUserNames; lvItem = new ListViewItem(listEntry, 0); lvItem.Tag = sharedFile; int insertIndex = 0; if (listView_SharedFiles.Items.Count > 0) { for (insertIndex = 0; insertIndex < listView_SharedFiles.Items.Count; insertIndex++) { DRPolicy sharedFileInList = (DRPolicy)(listView_SharedFiles.Items[insertIndex]).Tag; if (sharedFile.CreationTime < sharedFileInList.CreationTime) { break; } } } listView_SharedFiles.Items.Insert(insertIndex, lvItem); } } catch { } }