在本文中,我們将向您展示如何對本地文檔、電子表格和示範文稿進行加密。
工具
我們将使用 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 将被調用)。
- 如果停用加密,或者您在其他應用中打開此檔案,那麼系統就會要求您輸入密碼。