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); }
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 static bool GetSharedFilePermission(string fileName, string processName, string userName, string tagStr, ref string iv, ref string key, ref uint accessFlags, ref string lastError) { bool retVal = false; try { lastError = string.Empty; ServiceReference1.ServiceSoapClient client = GetServiceClient(ref lastError); if (null == client) { return(false); } string ivStr = tagStr; string serverAccount = GlobalConfig.AccountName; int index = tagStr.IndexOf(";"); if (index > 0) { serverAccount = tagStr.Substring(0, index); ivStr = tagStr.Substring(index + 1); } UserInfo userInfo = new UserInfo(); string keyStr = string.Empty; userInfo.FileName = fileName; userInfo.AccountName = serverAccount; userInfo.ProcessName = processName; userInfo.UserName = userName; userInfo.UserPassword = password; userInfo.CreationTime = DateTime.Now.ToFileTime();; userInfo.EncryptionIV = ivStr; string userInfoStr = DigitalRightControl.EncryptObjectToStr <UserInfo>(userInfo); retVal = client.GetSharedFilePermission(userInfoStr, ref key, ref iv, ref accessFlags, ref lastError); } catch (Exception ex) { lastError = "Get file key failed with error:" + ex.Message; retVal = false; } return(retVal); }
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); }
private bool GetAccessPermissionFromServer(FilterAPI.MessageSendData messageSend, DRPolicyData drPolicyData, string userName, string processName, string userPassword, ref CacheUserAccessInfo cacheUserAccessInfo) { Boolean retVal = true; string fileName = messageSend.FileName; string lastError = string.Empty; try { UserInfo userInfo = new UserInfo(); string keyStr = string.Empty; string ivStr = string.Empty; userInfo.FileName = Path.GetFileName(messageSend.FileName) + DigitalRightControl.SECURE_SHARE_FILE_EXTENSION; userInfo.AccountName = drPolicyData.AccountName; userInfo.ProcessName = processName; userInfo.UserName = userName; userInfo.UserPassword = userPassword; userInfo.CreationTime = drPolicyData.CreationTime; byte[] computerId = new byte[52]; uint computerIdLength = (uint)computerId.Length; IntPtr computerIdPtr = Marshal.UnsafeAddrOfPinnedArrayElement(computerId, 0); retVal = FilterAPI.GetUniqueComputerId(computerIdPtr, ref computerIdLength); if (!retVal) { string message = "Get computerId failed,return error:" + FilterAPI.GetLastErrorMessage(); EventManager.WriteMessage(366, "GetAccessPermissionFromServer", EventLevel.Error, message); return(retVal); } Array.Resize(ref computerId, (int)computerIdLength); userInfo.ComputerId = UnicodeEncoding.Unicode.GetString(computerId); string userInfoStr = DigitalRightControl.EncryptObjectToStr <UserInfo>(userInfo); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); //retVal = WebFormServices.GetFileKey(userInfoStr, ref keyStr, ref ivStr, ref lastError); stopWatch.Stop(); if (!retVal) { string message = "Get file " + messageSend.FileName + " permission from server return error:" + lastError; EventManager.WriteMessage(293, "GetAccessPermissionFromServer", EventLevel.Error, message); return(retVal); } else { string message = "Get file " + messageSend.FileName + " permission frome server return succeed, spent " + stopWatch.ElapsedMilliseconds + " milliseconds."; EventManager.WriteMessage(208, "GetAccessPermissionFromServer", EventLevel.Verbose, message); } cacheUserAccessInfo.key = keyStr; cacheUserAccessInfo.iv = ivStr; } catch (Exception ex) { EventManager.WriteMessage(286, "GetAccessPermissionFromServer", EventLevel.Error, "Get file " + messageSend.FileName + "permission failed with exception:" + ex.Message); retVal = false; } return(retVal); }
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); } }