使用RMS API 自定义Office(Word、Excel、PPT)加密策略

什么是RMS:

Microsoft Windows Rights Management 服务 (RMS),是一种与应用程序协作来保护数字内容(不论其何去何从)的安全技术,专为那些需要保护敏感的 Web 内容、文档和电子邮件的用户而设计。用户可以严格规定哪些用户可以打开、读取、修改和重新分发特定内容。组织可以创建权限策略模板,以实施用户应用于内容的策略。

准备工作1:

下载并添加引用以下3个DLL:

Microsoft.Office.Interop.Excel.dll

Microsoft.Office.Interop.PowerPoint.dll

Microsoft.Office.Interop.Word.dll

并在dll的属性中,把“嵌入互操作类型”设为False

 

添加以下引用:

在添加引用-程序集-扩展中,引用office       ,(或引用Microsoft Office 14.0 Object Library)

在添加引用-程序集-框架中,引用PresentationCore和PresentationFramework

准备工作2:

打开word或者Excel等office文件,在信息-保护文档-限制访问-选择连接到权限管理服务器并获取模版,登陆域账号,后续在RMS加密工作将会使用这个域账号进行:
 

代码:

EncryptFile.cs

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.Office.Core;  
  7.   
  8. namespace WindowsFormsApplication1  
  9. {  
  10.     class EncryptFile  
  11.     {  
  12.   
  13.   
  14.   
  15.         /// <summary>  
  16.         /// 加密Word  
  17.         /// </summary>  
  18.         /// <param name="filemodel">文件属性</param>  
  19.         /// <param name="path">文件路径</param>  
  20.         /// <returns>加密结果</returns>  
  21.         public static bool EncryptWord(RMSFileModel filemodel, string path)  
  22.         {  
  23.             var wordClass = new Microsoft.Office.Interop.Word.ApplicationClass();  
  24.             Microsoft.Office.Interop.Word.Document mydoc = wordClass.Documents.Open(filemodel.FSourcePath);  
  25.             try  
  26.             {  
  27.                 DateTime exprieDateTime = new DateTime(2027, 05, 19);  
  28.                 if (mydoc != null)  
  29.                 {  
  30.                     mydoc.Activate();  
  31.                     var irmPremission = mydoc.Permission;  
  32.                     if (irmPremission.Enabled == true)  
  33.                     {  
  34.                         filemodel.FErrorMessage = "加密已加密的Word时错误";  
  35.                         filemodel.FContent = "当前文档已经加密,操作失败!";  
  36.                         return false;  
  37.                     }  
  38.                     mydoc.Application.Visible = false;  
  39.                     irmPremission.Enabled = true;  
  40.                     if (filemodel.Users != null && filemodel.Users.Length > 0)  
  41.                     {  
  42.                         foreach (var item in filemodel.Users)  
  43.                         {  
  44.                             if (!String.IsNullOrEmpty(ObjIsEmail(item)))  
  45.                             {  
  46.                                 if (filemodel.FIsFullControl)  
  47.                                 {  
  48.                                     irmPremission.Add(item, MsoPermission.msoPermissionFullControl, exprieDateTime);  
  49.                                 }  
  50.                                 if (filemodel.FIsPrint)  
  51.                                 {  
  52.                                     irmPremission.Add(item, MsoPermission.msoPermissionPrint, exprieDateTime);  
  53.                                 }  
  54.                                 if (filemodel.FIsRead)  
  55.                                 {  
  56.                                     irmPremission.Add(item, MsoPermission.msoPermissionRead, exprieDateTime);  
  57.                                 }  
  58.                                 if (filemodel.FIsSave)  
  59.                                 {  
  60.                                     irmPremission.Add(item, MsoPermission.msoPermissionSave, exprieDateTime);  
  61.                                 }  
  62.                                 if (filemodel.FIsEdit)  
  63.                                 {  
  64.                                     irmPremission.Add(item, MsoPermission.msoPermissionEdit, exprieDateTime);  
  65.                                 }  
  66.                             }  
  67.                         }  
  68.                     }  
  69.                     else  
  70.                     {  
  71.                         irmPremission.Add("Everyone", MsoPermission.msoPermissionEdit, exprieDateTime);  
  72.                     }  
  73.                 }  
  74.   
  75.                 string tempPath = path + filemodel.FTitle;  
  76.                 mydoc.SaveAs(tempPath);  
  77.                 filemodel.FRMSPath = tempPath;  
  78.                 filemodel.FContent = "加密成功";  
  79.                 return true;  
  80.             }  
  81.             catch (Exception ex)  
  82.             {  
  83.                 filemodel.FErrorMessage = "加密Word时错误";  
  84.                 filemodel.FContent = ex.ToString();  
  85.                 return false;  
  86.             }  
  87.             finally  
  88.             {  
  89.                 mydoc.Close();  
  90.                 wordClass.Quit();  
  91.             }  
  92.         }  
  93.   
  94.         /// <summary>  
  95.         /// 加密Excel  
  96.         /// </summary>  
  97.         /// <param name="filemodel">文件属性</param>  
  98.         /// <param name="path">文件路径</param>  
  99.         /// <returns>加密结果</returns>  
  100.         public static bool EncryptExcel(RMSFileModel filemodel, string path)  
  101.         {  
  102.             var excelClass = new Microsoft.Office.Interop.Excel.ApplicationClass();  
  103.             var myExecl = excelClass.Workbooks.Open(filemodel.FSourcePath);  
  104.             try  
  105.             {  
  106.                 DateTime exprieDateTime = new DateTime(2027, 05, 19);  
  107.                 if (myExecl != null)  
  108.                 {  
  109.                     myExecl.Activate();  
  110.                     var irmPremission = myExecl.Permission;  
  111.                     if (irmPremission.Enabled == true)  
  112.                     {  
  113.                         filemodel.FErrorMessage = "加密已加密的Excel时错误";  
  114.                         filemodel.FContent = "当前文档已经加密,操作失败!";  
  115.                         return false;  
  116.                     }  
  117.                     myExecl.Application.Visible = false;  
  118.                     irmPremission.Enabled = true;  
  119.                     if (filemodel.Users != null && filemodel.Users.Length > 0)  
  120.                     {  
  121.                         foreach (var item in filemodel.Users)  
  122.                         {  
  123.                             if (!String.IsNullOrEmpty(ObjIsEmail(item)))  
  124.                             {  
  125.                                 if (filemodel.FIsFullControl)  
  126.                                 {  
  127.                                     irmPremission.Add(item, MsoPermission.msoPermissionFullControl, exprieDateTime);  
  128.                                 }  
  129.                                 if (filemodel.FIsPrint)  
  130.                                 {  
  131.                                     irmPremission.Add(item, MsoPermission.msoPermissionPrint, exprieDateTime);  
  132.                                 }  
  133.                                 if (filemodel.FIsRead)  
  134.                                 {  
  135.                                     irmPremission.Add(item, MsoPermission.msoPermissionRead, exprieDateTime);  
  136.                                 }  
  137.                                 if (filemodel.FIsSave)  
  138.                                 {  
  139.                                     irmPremission.Add(item, MsoPermission.msoPermissionSave, exprieDateTime);  
  140.                                 }  
  141.                                 if (filemodel.FIsEdit)  
  142.                                 {  
  143.                                     irmPremission.Add(item, MsoPermission.msoPermissionEdit, exprieDateTime);  
  144.                                 }  
  145.                             }  
  146.                         }  
  147.                     }  
  148.                     else  
  149.                     {  
  150.                         irmPremission.Add("Everyone", MsoPermission.msoPermissionEdit, exprieDateTime);  
  151.                     }  
  152.                 }  
  153.                 string tempPath = path + filemodel.FTitle;  
  154.                 myExecl.SaveAs(tempPath);  
  155.                 filemodel.FRMSPath = tempPath;  
  156.                 filemodel.FContent = "加密成功";  
  157.                 return true;  
  158.             }  
  159.             catch (Exception ex)  
  160.             {  
  161.                 filemodel.FErrorMessage = "加密Excel时错误";  
  162.                 filemodel.FContent = ex.ToString();  
  163.                 return false;  
  164.             }  
  165.             finally  
  166.             {  
  167.                 myExecl.Close();  
  168.                 excelClass.Quit();  
  169.             }  
  170.         }  
  171.   
  172.         /// <summary>  
  173.         /// 加密PPT  
  174.         /// </summary>  
  175.         /// <param name="filemodel">文件属性</param>  
  176.         /// <param name="path">文件路径</param>  
  177.         /// <returns>加密结果</returns>  
  178.         public static bool EncryptPPT(RMSFileModel filemodel, string path)  
  179.         {  
  180.             var pptClass = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();  
  181.             var myppt = pptClass.Presentations.Open(filemodel.FSourcePath, MsoTriState.msoCTrue, MsoTriState.msoCTrue, MsoTriState.msoFalse);  
  182.             try  
  183.             {  
  184.                 DateTime exprieDateTime = new DateTime(2027, 05, 19);  
  185.                 if (myppt != null)  
  186.                 {  
  187.                     //myppt.Activate();  
  188.                     var irmPremission = myppt.Permission;  
  189.                     if (irmPremission.Enabled == true)  
  190.                     {  
  191.                         filemodel.FErrorMessage = "加密已加密的Excel时错误";  
  192.                         filemodel.FContent = "当前文档已经加密,操作失败!";  
  193.                         return false;  
  194.                     }  
  195.                     //myppt.Application.Visible = false;  
  196.                     irmPremission.Enabled = true;  
  197.                     if (filemodel.Users != null && filemodel.Users.Length > 0)  
  198.                     {  
  199.                         foreach (var item in filemodel.Users)  
  200.                         {  
  201.                             if (!String.IsNullOrEmpty(ObjIsEmail(item)))  
  202.                             {  
  203.                                 if (filemodel.FIsFullControl)  
  204.                                 {  
  205.                                     irmPremission.Add(item, MsoPermission.msoPermissionFullControl, exprieDateTime);  
  206.                                 }  
  207.                                 if (filemodel.FIsPrint)  
  208.                                 {  
  209.                                     irmPremission.Add(item, MsoPermission.msoPermissionPrint, exprieDateTime);  
  210.                                 }  
  211.                                 if (filemodel.FIsRead)  
  212.                                 {  
  213.                                     irmPremission.Add(item, MsoPermission.msoPermissionRead, exprieDateTime);  
  214.                                 }  
  215.                                 if (filemodel.FIsSave)  
  216.                                 {  
  217.                                     irmPremission.Add(item, MsoPermission.msoPermissionSave, exprieDateTime);  
  218.                                 }  
  219.                                 if (filemodel.FIsEdit)  
  220.                                 {  
  221.                                     irmPremission.Add(item, MsoPermission.msoPermissionEdit, exprieDateTime);  
  222.                                 }  
  223.                             }  
  224.                         }  
  225.                     }  
  226.                     else  
  227.                     {  
  228.                         irmPremission.Add("Everyone", MsoPermission.msoPermissionEdit, exprieDateTime);  
  229.                     }  
  230.                 }  
  231.                 string tempPath = path + filemodel.FTitle;  
  232.                 myppt.SaveAs(tempPath);  
  233.                 filemodel.FRMSPath = tempPath;  
  234.                 filemodel.FContent = "加密成功";  
  235.                 return true;  
  236.             }  
  237.             catch (Exception ex)  
  238.             {  
  239.                 filemodel.FErrorMessage = "加密PPT时错误";  
  240.                 filemodel.FContent = ex.ToString();  
  241.                 return false;  
  242.             }  
  243.             finally  
  244.             {  
  245.                 myppt.Close();  
  246.                 pptClass.Quit();  
  247.             }  
  248.         }  
  249.   
  250.         /// <summary>  
  251.         /// 清理Office进程  
  252.         /// </summary>  
  253.         public static void KillOfficeProcess()  
  254.         {  
  255.             try  
  256.             {  
  257.                 var proWord = System.Diagnostics.Process.GetProcessesByName("WinWord");  
  258.                 foreach (var word in proWord)  
  259.                 {  
  260.                     word.Kill();  
  261.                 }  
  262.                 var proExcel = System.Diagnostics.Process.GetProcessesByName("Excel");  
  263.                 foreach (var excel in proExcel)  
  264.                 {  
  265.                     excel.Kill();  
  266.                 }  
  267.                 var proPPT = System.Diagnostics.Process.GetProcessesByName("POWERPNT");  
  268.                 foreach (var ppt in proPPT)  
  269.                 {  
  270.                     ppt.Kill();  
  271.                 }  
  272.             }  
  273.             catch (Exception)  
  274.             {  
  275.   
  276.                 throw;  
  277.             }  
  278.         }  
  279.   
  280.         /// <summary>  
  281.         /// Object判断并转为Email(String格式)  
  282.         /// </summary>  
  283.         /// <param name="o">Object</param>  
  284.         /// <returns>Email</returns>  
  285.         public static string ObjIsEmail(object o)  
  286.         {  
  287.             string email = ObjToStr(o);  
  288.             if (!String.IsNullOrEmpty(email))  
  289.             {  
  290.                 if (email.IndexOf("@") > -1)  
  291.                 {  
  292.                     return email;  
  293.                 }  
  294.                 else  
  295.                 {  
  296.                     return "";  
  297.                 }  
  298.             }  
  299.             else  
  300.             {  
  301.                 return "";  
  302.             }  
  303.         }  
  304.   
  305.         /// <summary>  
  306.         /// Object转String  
  307.         /// </summary>  
  308.         /// <param name="o">Object</param>  
  309.         /// <returns>String</returns>  
  310.         public static string ObjToStr(object o)  
  311.         {  
  312.             if (o == null)  
  313.             {  
  314.                 return "";  
  315.             }  
  316.             else  
  317.             {  
  318.                 return o.ToString();  
  319.             }  
  320.         }  
  321.     }  
  322. }  
 

RMSFileModel.cs

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace WindowsFormsApplication1  
  8. {  
  9.     public class RMSFileModel  
  10.     {  
  11.         /// <summary>  
  12.         /// 文件ID  
  13.         /// </summary>  
  14.         public int FID { get; set; }  
  15.         /// <summary>  
  16.         /// 完全控制  
  17.         /// </summary>  
  18.         public bool FIsFullControl { get; set; }  
  19.         /// <summary>  
  20.         /// 只读  
  21.         /// </summary>  
  22.         public bool FIsRead { get; set; }  
  23.         /// <summary>  
  24.         /// 打印  
  25.         /// </summary>  
  26.         public bool FIsPrint { get; set; }  
  27.         /// <summary>  
  28.         /// 另存  
  29.         /// </summary>  
  30.         public bool FIsSave { get; set; }  
  31.         /// <summary>  
  32.         /// 编辑  
  33.         /// </summary>  
  34.         public bool FIsEdit { get; set; }  
  35.         /// <summary>  
  36.         /// 文件名  
  37.         /// </summary>  
  38.         public string FTitle { get; set; }  
  39.         /// <summary>  
  40.         /// 在线URL路径(源文件路径)  
  41.         /// </summary>  
  42.         public string FUrlSourcePath { get; set; }  
  43.         /// <summary>  
  44.         /// 在线URL路径(加密后的文件路径)  
  45.         /// </summary>  
  46.         public string FUrlRMSPath { get; set; }  
  47.         /// <summary>  
  48.         /// 应用到用户  
  49.         /// </summary>  
  50.         public string[] Users { get; set; }  
  51.         /// <summary>  
  52.         /// 加密状态  
  53.         /// </summary>  
  54.         public string FState { get; set; }  
  55.         /// <summary>  
  56.         /// 加密错误分类  
  57.         /// </summary>  
  58.         public string FErrorMessage { get; set; }  
  59.         /// <summary>  
  60.         /// 加密详情  
  61.         /// </summary>  
  62.         public string FContent { get; set; }  
  63.         /// <summary>  
  64.         /// 本地物理路径(源文件)  
  65.         /// </summary>  
  66.         public string FSourcePath { get; set; }  
  67.         /// <summary>  
  68.         /// 本地物理路径(加密后的文件)  
  69.         /// </summary>  
  70.         public string FRMSPath { get; set; }  
  71.     }  
  72. }  


 

调用:

 
[csharp] view plain copy
 
  1. /// <summary>  
  2.         /// 执行加密操作  
  3.         /// </summary>  
  4.         /// <param name="fileModel">文件属性</param>  
  5.         /// <returns>加密结果</returns>  
  6.         private bool HandleRMS(RMSFileModel fileModel)  
  7.         {  
  8.             try  
  9.             {  
  10.                 //确认路径及创建文件夹  
  11.                 string path = System.Windows.Forms.Application.StartupPath + "\\App_Data\\RMSFile\\";  
  12.                 if (!Directory.Exists(path))  
  13.                 {  
  14.                     Directory.CreateDirectory(path);  
  15.                 }  
  16.                 //清理Office进程  
  17.                 EncryptFile.KillOfficeProcess();  
  18.                 string extension = Path.GetExtension(fileModel.FSourcePath).ToLower();//获取扩展名  
  19.                 if (extension.IndexOf("doc") > -1)  
  20.                 {  
  21.                     return Common.EncryptFile.EncryptWord(fileModel, path);  
  22.                 }  
  23.                 if (extension.IndexOf("xls") > -1)  
  24.                 {  
  25.                     return Common.EncryptFile.EncryptExcel(fileModel, path);  
  26.                 }  
  27.                 if (extension.IndexOf("ppt") > -1)  
  28.                 {  
  29.                     return Common.EncryptFile.EncryptPPT(fileModel, path);  
  30.                 }  
  31.                 fileModel.FErrorMessage = "格式错误";  
  32.                 fileModel.FContent = "无法加密该格式的文件";  
  33.                 return false;  
  34.             }  
  35.             catch (Exception ex)  
  36.             {  
  37.                 fileModel.FErrorMessage = "获取文件扩展名错误";  
  38.                 fileModel.FContent = ex.ToString();  
  39.                 return false;  
  40.             }  
  41.         }  


谢谢!

posted on 2018-04-04 14:15  秋天  阅读(1606)  评论(0编辑  收藏  举报

导航