Microsoft Windows Rights Management 服務 (RMS),是一種與應用程序協做來保護數字內容(不論其何去何從)的安全技術,專爲那些須要保護敏感的 Web 內容、文檔和電子郵件的用戶而設計。用戶能夠嚴格規定哪些用戶能夠打開、讀取、修改和從新分發特定內容。組織能夠建立權限策略模板,以實施用戶應用於內容的策略。安全
下載並添加引用如下3個DLL:
Microsoft.Office.Interop.Excel.dll服務器
Microsoft.Office.Interop.PowerPoint.dll框架
Microsoft.Office.Interop.Word.dllui
並在dll的屬性中,把「嵌入互操做類型」設爲False加密
添加如下引用:
在添加引用-程序集-擴展中,引用office ,(或引用Microsoft Office 14.0 Object Library)spa
在添加引用-程序集-框架中,引用PresentationCore和PresentationFramework.net
打開word或者Excel等office文件,在信息-保護文檔-限制訪問-選擇鏈接到權限管理服務器並獲取模版,登錄域帳號,後續在RMS加密工做將會使用這個域帳號進行:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Office.Core;
- namespace WindowsFormsApplication1
- {
- class EncryptFile
- {
- /// <summary>
- /// 加密Word
- /// </summary>
- /// <param name="filemodel">文件屬性</param>
- /// <param name="path">文件路徑</param>
- /// <returns>加密結果</returns>
- public static bool EncryptWord(RMSFileModel filemodel, string path)
- {
- var wordClass = new Microsoft.Office.Interop.Word.ApplicationClass();
- Microsoft.Office.Interop.Word.Document mydoc = wordClass.Documents.Open(filemodel.FSourcePath);
- try
- {
- DateTime exprieDateTime = new DateTime(2027, 05, 19);
- if (mydoc != null)
- {
- mydoc.Activate();
- var irmPremission = mydoc.Permission;
- if (irmPremission.Enabled == true)
- {
- filemodel.FErrorMessage = "加密已加密的Word時錯誤";
- filemodel.FContent = "當前文檔已經加密,操做失敗!";
- return false;
- }
- mydoc.Application.Visible = false;
- irmPremission.Enabled = true;
- if (filemodel.Users != null && filemodel.Users.Length > 0)
- {
- foreach (var item in filemodel.Users)
- {
- if (!String.IsNullOrEmpty(ObjIsEmail(item)))
- {
- if (filemodel.FIsFullControl)
- {
- irmPremission.Add(item, MsoPermission.msoPermissionFullControl, exprieDateTime);
- }
- if (filemodel.FIsPrint)
- {
- irmPremission.Add(item, MsoPermission.msoPermissionPrint, exprieDateTime);
- }
- if (filemodel.FIsRead)
- {
- irmPremission.Add(item, MsoPermission.msoPermissionRead, exprieDateTime);
- }
- if (filemodel.FIsSave)
- {
- irmPremission.Add(item, MsoPermission.msoPermissionSave, exprieDateTime);
- }
- if (filemodel.FIsEdit)
- {
- irmPremission.Add(item, MsoPermission.msoPermissionEdit, exprieDateTime);
- }
- }
- }
- }
- else
- {
- irmPremission.Add("Everyone", MsoPermission.msoPermissionEdit, exprieDateTime);
- }
- }
- string tempPath = path + filemodel.FTitle;
- mydoc.SaveAs(tempPath);
- filemodel.FRMSPath = tempPath;
- filemodel.FContent = "加密成功";
- return true;
- }
- catch (Exception ex)
- {
- filemodel.FErrorMessage = "加密Word時錯誤";
- filemodel.FContent = ex.ToString();
- return false;
- }
- finally
- {
- mydoc.Close();
- wordClass.Quit();
- }
- }
- /// <summary>
- /// 加密Excel
- /// </summary>
- /// <param name="filemodel">文件屬性</param>
- /// <param name="path">文件路徑</param>
- /// <returns>加密結果</returns>
- public static bool EncryptExcel(RMSFileModel filemodel, string path)
- {
- var excelClass = new Microsoft.Office.Interop.Excel.ApplicationClass();
- var myExecl = excelClass.Workbooks.Open(filemodel.FSourcePath);
- try
- {
- DateTime exprieDateTime = new DateTime(2027, 05, 19);
- if (myExecl != null)
- {
- myExecl.Activate();
- var irmPremission = myExecl.Permission;
- if (irmPremission.Enabled == true)
- {
- filemodel.FErrorMessage = "加密已加密的Excel時錯誤";
- filemodel.FContent = "當前文檔已經加密,操做失敗!";
- return false;
- }
- myExecl.Application.Visible = false;
- irmPremission.Enabled = true;
- if (filemodel.Users != null && filemodel.Users.Length > 0)
- {
- foreach (var item in filemodel.Users)
- {
- if (!String.IsNullOrEmpty(ObjIsEmail(item)))
- {
- if (filemodel.FIsFullControl)
- {
- irmPremission.Add(item, MsoPermission.msoPermissionFullControl, exprieDateTime);
- }
- if (filemodel.FIsPrint)
- {
- irmPremission.Add(item, MsoPermission.msoPermissionPrint, exprieDateTime);
- }
- if (filemodel.FIsRead)
- {
- irmPremission.Add(item, MsoPermission.msoPermissionRead, exprieDateTime);
- }
- if (filemodel.FIsSave)
- {
- irmPremission.Add(item, MsoPermission.msoPermissionSave, exprieDateTime);
- }
- if (filemodel.FIsEdit)
- {
- irmPremission.Add(item, MsoPermission.msoPermissionEdit, exprieDateTime);
- }
- }
- }
- }
- else
- {
- irmPremission.Add("Everyone", MsoPermission.msoPermissionEdit, exprieDateTime);
- }
- }
- string tempPath = path + filemodel.FTitle;
- myExecl.SaveAs(tempPath);
- filemodel.FRMSPath = tempPath;
- filemodel.FContent = "加密成功";
- return true;
- }
- catch (Exception ex)
- {
- filemodel.FErrorMessage = "加密Excel時錯誤";
- filemodel.FContent = ex.ToString();
- return false;
- }
- finally
- {
- myExecl.Close();
- excelClass.Quit();
- }
- }
- /// <summary>
- /// 加密PPT
- /// </summary>
- /// <param name="filemodel">文件屬性</param>
- /// <param name="path">文件路徑</param>
- /// <returns>加密結果</returns>
- public static bool EncryptPPT(RMSFileModel filemodel, string path)
- {
- var pptClass = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
- var myppt = pptClass.Presentations.Open(filemodel.FSourcePath, MsoTriState.msoCTrue, MsoTriState.msoCTrue, MsoTriState.msoFalse);
- try
- {
- DateTime exprieDateTime = new DateTime(2027, 05, 19);
- if (myppt != null)
- {
- //myppt.Activate();
- var irmPremission = myppt.Permission;
- if (irmPremission.Enabled == true)
- {
- filemodel.FErrorMessage = "加密已加密的Excel時錯誤";
- filemodel.FContent = "當前文檔已經加密,操做失敗!";
- return false;
- }
- //myppt.Application.Visible = false;
- irmPremission.Enabled = true;
- if (filemodel.Users != null && filemodel.Users.Length > 0)
- {
- foreach (var item in filemodel.Users)
- {
- if (!String.IsNullOrEmpty(ObjIsEmail(item)))
- {
- if (filemodel.FIsFullControl)
- {
- irmPremission.Add(item, MsoPermission.msoPermissionFullControl, exprieDateTime);
- }
- if (filemodel.FIsPrint)
- {
- irmPremission.Add(item, MsoPermission.msoPermissionPrint, exprieDateTime);
- }
- if (filemodel.FIsRead)
- {
- irmPremission.Add(item, MsoPermission.msoPermissionRead, exprieDateTime);
- }
- if (filemodel.FIsSave)
- {
- irmPremission.Add(item, MsoPermission.msoPermissionSave, exprieDateTime);
- }
- if (filemodel.FIsEdit)
- {
- irmPremission.Add(item, MsoPermission.msoPermissionEdit, exprieDateTime);
- }
- }
- }
- }
- else
- {
- irmPremission.Add("Everyone", MsoPermission.msoPermissionEdit, exprieDateTime);
- }
- }
- string tempPath = path + filemodel.FTitle;
- myppt.SaveAs(tempPath);
- filemodel.FRMSPath = tempPath;
- filemodel.FContent = "加密成功";
- return true;
- }
- catch (Exception ex)
- {
- filemodel.FErrorMessage = "加密PPT時錯誤";
- filemodel.FContent = ex.ToString();
- return false;
- }
- finally
- {
- myppt.Close();
- pptClass.Quit();
- }
- }
- /// <summary>
- /// 清理Office進程
- /// </summary>
- public static void KillOfficeProcess()
- {
- try
- {
- var proWord = System.Diagnostics.Process.GetProcessesByName("WinWord");
- foreach (var word in proWord)
- {
- word.Kill();
- }
- var proExcel = System.Diagnostics.Process.GetProcessesByName("Excel");
- foreach (var excel in proExcel)
- {
- excel.Kill();
- }
- var proPPT = System.Diagnostics.Process.GetProcessesByName("POWERPNT");
- foreach (var ppt in proPPT)
- {
- ppt.Kill();
- }
- }
- catch (Exception)
- {
- throw;
- }
- }
- /// <summary>
- /// Object判斷並轉爲Email(String格式)
- /// </summary>
- /// <param name="o">Object</param>
- /// <returns>Email</returns>
- public static string ObjIsEmail(object o)
- {
- string email = ObjToStr(o);
- if (!String.IsNullOrEmpty(email))
- {
- if (email.IndexOf("@") > -1)
- {
- return email;
- }
- else
- {
- return "";
- }
- }
- else
- {
- return "";
- }
- }
- /// <summary>
- /// Object轉String
- /// </summary>
- /// <param name="o">Object</param>
- /// <returns>String</returns>
- public static string ObjToStr(object o)
- {
- if (o == null)
- {
- return "";
- }
- else
- {
- return o.ToString();
- }
- }
- }
- }
RMSFileModel.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace WindowsFormsApplication1
- {
- public class RMSFileModel
- {
- /// <summary>
- /// 文件ID
- /// </summary>
- public int FID { get; set; }
- /// <summary>
- /// 徹底控制
- /// </summary>
- public bool FIsFullControl { get; set; }
- /// <summary>
- /// 只讀
- /// </summary>
- public bool FIsRead { get; set; }
- /// <summary>
- /// 打印
- /// </summary>
- public bool FIsPrint { get; set; }
- /// <summary>
- /// 另存
- /// </summary>
- public bool FIsSave { get; set; }
- /// <summary>
- /// 編輯
- /// </summary>
- public bool FIsEdit { get; set; }
- /// <summary>
- /// 文件名
- /// </summary>
- public string FTitle { get; set; }
- /// <summary>
- /// 在線URL路徑(源文件路徑)
- /// </summary>
- public string FUrlSourcePath { get; set; }
- /// <summary>
- /// 在線URL路徑(加密後的文件路徑)
- /// </summary>
- public string FUrlRMSPath { get; set; }
- /// <summary>
- /// 應用到用戶
- /// </summary>
- public string[] Users { get; set; }
- /// <summary>
- /// 加密狀態
- /// </summary>
- public string FState { get; set; }
- /// <summary>
- /// 加密錯誤分類
- /// </summary>
- public string FErrorMessage { get; set; }
- /// <summary>
- /// 加密詳情
- /// </summary>
- public string FContent { get; set; }
- /// <summary>
- /// 本地物理路徑(源文件)
- /// </summary>
- public string FSourcePath { get; set; }
- /// <summary>
- /// 本地物理路徑(加密後的文件)
- /// </summary>
- public string FRMSPath { get; set; }
- }
- }
調用:
- /// <summary>
- /// 執行加密操做
- /// </summary>
- /// <param name="fileModel">文件屬性</param>
- /// <returns>加密結果</returns>
- private bool HandleRMS(RMSFileModel fileModel)
- {
- try
- {
- //確認路徑及建立文件夾
- string path = System.Windows.Forms.Application.StartupPath + "\\App_Data\\RMSFile\\";
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- //清理Office進程
- EncryptFile.KillOfficeProcess();
- string extension = Path.GetExtension(fileModel.FSourcePath).ToLower();//獲取擴展名
- if (extension.IndexOf("doc") > -1)
- {
- return Common.EncryptFile.EncryptWord(fileModel, path);
- }
- if (extension.IndexOf("xls") > -1)
- {
- return Common.EncryptFile.EncryptExcel(fileModel, path);
- }
- if (extension.IndexOf("ppt") > -1)
- {
- return Common.EncryptFile.EncryptPPT(fileModel, path);
- }
- fileModel.FErrorMessage = "格式錯誤";
- fileModel.FContent = "沒法加密該格式的文件";
- return false;
- }
- catch (Exception ex)
- {
- fileModel.FErrorMessage = "獲取文件擴展名錯誤";
- fileModel.FContent = ex.ToString();
- return false;
- }
- }
謝謝!