天天看点

如何对本地办公文档进行加密

​在本文中,我们将向您展示如何对本地文档、电子表格和演示文稿进行加密。

如何对本地办公文档进行加密

工具

我们将使用 ONLYOFFICE 桌面编辑器这款可在 Linux、Windows 以及 MacOS 上使用的开源办公套件。其分发遵循 AGPLv3,并且提供有公开可用的 API。

本应用支持插件功能。这里我们也将使用插件来实现额外的安全性。

具有插件功能意味着您可以添加自己的功能,这在安全性方面应该会有较大的用处。您当然也没有必要完全按照开发人员所提供的线程说明,或是使用其所提供的现成工具来实现自己的目的。您完全可以按需独立开展相关工作。

插件的基本信息

插件中包含:

  • 包含界面的 HTML 文件。
  • 包含代码的 JS 文件。
  • 包含配置的 JSON 文件。

您可在此处了解插件结构。ONLYOFFICE GitHub 中提供了一些插件示例。只需在“Find a repository”字段中输入“Plugin”即可。

可用于加密所有文档的 2 个插件

为了对文档加密工作进行自动化,同时也为了降低此类工作的难度,我们在这里将创建 2 个插件:

  • 编辑器加密插件,与
  • 应用 UI 插件。

为什么需要这两者?通常而言,ONLYOFFICE 插件仅适用于其所运行的文档(即:应用的某个选项卡中)。在本例中,我们希望能够对 ONLYOFFICE 桌面版的不同选项卡中所打开或创建的所有文档进行处理。所以我们就需要上面提到的第二个插件。

请注意,下方所介绍的插件均为示例插件 —— 您可根据此处的信息自行进行创建工作。

编辑器加密插件

我们将创建一个简单加密插件。

首先来看看配置:

{
    "name" : "crypto",
    "guid" : "asc.{22222222-2222-2222-2222-222222222222}",
 
    "variations" : [
        {
            "url"         : "index.html",
 
            "icons"           : [],
            "isViewer"        : true,
            "EditorsSupport"  : ["word", "cell", "slide"],
 
            "isVisual"        : false,
            "isModal"         : false,
            "isInsideMode"    : false,
 
            "initDataType"    : "desktop",    // this should be the value for this type of plugin
            "initData"        : "encryption",    // this should be the value for this type of plugin
            "cryptoMode"      : "2",             // plugin ID as it’s possible to create several encryption plugins 
 
            "cryptoDisabledForInternalCloud" : "true",  // disables encryption for ONLYOFFICE clouds
            "cryptoDisabledForExternalCloud" : "true"  // disables encryption for 3rd party clouds
        }
    ]
}      

您可轻松地在文档中找到前面的字段。最后的五个值是新增的,相关文档也会很快推出。

插件代码还是比较简单的。

(function(window, undefined){
   const global_password = "{ my-super-long-password }";
   window.Asc.plugin.init = function(obj)
    {
      if (!obj)
        return;
 
      switch (obj.type)
    {
      case "generatePassword":
      {
        // password generation request
        this.executeMethod("OnEncryption", [{ type : "generatePassword", password : global_password }]);
        break;
      }
      case "getPasswordByFile":
      {
        // file password request
        this.executeMethod("OnEncryption", [{ type : "getPasswordByFile", password : global_password }]);
        break;
      }
      case "setPasswordByFile":
      {
         this.executeMethod("StartAction", ["Info", "Save"]);
         // saving password and file information
         this.executeMethod("EndAction", ["Info", "Save"]);
       
        break;
      }
      case "encryptData":
      {
        // changes encryption - not relevant for local files
        this.executeMethod("OnEncryption", [{ type : "encryptData", data : obj.data, check: true }]);
        break;
      }
      case "decryptData":
      {
        // changes decryption - not relevant for local files
        this.executeMethod("OnEncryption", [{ type : "decryptData", data : obj.data, check: true }]);
        break;
      }    
      default:
        break;
    }
    };
 
})(window, undefined);      

从上方的代码中我们可以看出,所有文件都将使用同一个密码来进行加密 - {my-super-long-password}。更多有关加密方法的信息可在​​官方 API 文档​​中找到。

这一加密插件并没有提供界面,同时也不会默认启动。但是,编辑器将知道应该在什么时候使用它 - 只要我们先创建一个适用于整个应用范围内的 UI 插件。

应用插件

此插件不能访问编辑功能,但其配置却几乎是相同的:

{
    "name" : "Encryption",
    "nameLocale" : { "ru" : "Шифрование",
                    "it" : "Crittografia",
                    "fr" : "Chiffrement",
                    "es" : "Encriptación",
                    "de" : "Verschlüsselung"                
                    },
    "guid" : "asc.{11111111-1111-1111-1111-111111111111}",
 
    "variations" : [
        {
            "url"           : "index.html",
      
      "initDataType"  : "desktop-external",  // plugins for the whole app type
            "initData"    : "encryption",              // indicates that the plugin is meant for encryption.
      
      "cryptoDisabledOnStart" : "true"         // reset the mode on restart              }
    ]
}      

下面我们就来使用 index.html 创建一下插件界面:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
    <title>Encrypt files</title> 
    <script type="text/javascript" src="code.js"></script>    
  </head>
  <body>  
    <p>Encryption enabled: <input id="check" type="checkbox"/></p>
  </body>
</html>      

这样,我们就能在主页上新增一个名为“Encryption”的标签页:

如何对本地办公文档进行加密

接下来我们勾选复选框以启用加密:

window.onload = function() {
 
  const ASC_DESKTOP_EDITOR_DEFAULT_MODE = 0;
  const ASC_DESKTOP_EDITOR_CRYPTO_MODE = 2; // cryptoMode in the plugin for the editor config
  
  document.getElementById("check").onchange = function() {
    
    let mode = this.checked ? ASC_DESKTOP_EDITOR_CRYPTO_MODE : ASC_DESKTOP_EDITOR_DEFAULT_MODE;
    AscDesktopEditor.SetCryptoMode("", mode, function(retCode) {                
      switch (retCode) {                
        case 0: // OK
          console.log("OK");
          break;
        case 1: 
          console.log("Please, close all openfiles!");
          break;                                
        default: 
          break;
      }
    });
    
  };
 
};      

将插件添加至桌面端应用

在最新的稳定版本(6.2.2)中,您需要将带有插件的文件夹复制到 sdkjs-plugins 目录中。以下是其具体路径:

  • Linux:/opt/onlyoffice/desktopeditors/editors/sdkjs-plugins/
  • Windows:%ProgramFiles%\ONLYOFFICE\DesktopEditors\sdkjs-plugins\

记住一定要使用插件 GUID 作为其文件夹名称。

详细的说明可在​​官方 API 文档​​中找到。

使用加密插件

  • 如果保持加密状态开启,则在打开受保护的文件时不会要求您输入密码(getPasswordByFile 将被调用)。
  • 如果停用加密,或者您在其他应用中打开此文件,那么系统就会要求您输入密码。

继续阅读