這個快速入門工具會教你如何使用更多 MIP 保護 SDK。 使用你在前述快速入門中列出的保護範本之一,你可以使用保護處理程式來加密臨時文字。 保護處理類別會揭露各種用於套用/移除保護的操作。
先決條件
如果你還沒完成,請務必在繼續前完成以下先決條件:
- 完整快速入門:先列出保護範本(C++),建立一個入門的 Visual Studio 解決方案,列出已認證使用者可用的保護範本。 這個「加密/解密文字」快速入門是建立在前一個基礎上的。
- 可選地:檢視 MIP SDK 概念中的保護處理程序 。
實作一個觀察者類別來監控保護處理物件
類似你在應用程式初始化快速啟動中實作的觀察者(保護設定檔和引擎),現在你為保護處理物件實作了一個觀察者類別。
透過 mip::ProtectionHandler::Observer 擴充 SDK 類別,建立一個保護處理觀察器的基本實作。 觀察者會被實例化,之後再用來監控保護處理器的操作。
打開你在之前「快速入門:清單保護範本(C++)」文章中參與的 Visual Studio 解決方案。
將新類別新增至您的專案,這會為您產生標頭/.h 和實作/.cpp檔案:
- 在 方案總管中,再次以滑鼠右鍵按兩下項目節點,選取 [ 新增],然後選取 [ 類別]。
- 在 [ 新增類別 ] 對話框中:
- 在 班級名稱 欄位輸入「handler_observer」。 請注意,根據您輸入的名稱,會自動填入 .h 檔案 和 .cpp檔案 欄位。
- 完成後,按兩下 [ 確定] 按鈕。
產生 類別的 .h 和 .cpp 檔案之後,這兩個檔案都會在 [編輯器群組] 索引卷標中開啟。 現在更新每個檔案以實作新的觀察者類別:
透過選取或刪除產生
handler_observer的類別來更新「handler_observer.h」。 請勿 移除上一個步驟所產生的預處理器指示詞(#pragma、#include)。 然後在任何現有的預處理器指令之後,將下列原始碼複製/貼到檔案中。#include <memory> #include "mip/protection/protection_engine.h" using std::shared_ptr; using std::exception_ptr; class ProtectionHandlerObserver final : public mip::ProtectionHandler::Observer { public: ProtectionHandlerObserver() { } void OnCreateProtectionHandlerSuccess(const shared_ptr<mip::ProtectionHandler>& protectionHandler, const shared_ptr<void>& context) override; void OnCreateProtectionHandlerFailure(const exception_ptr& Failure, const shared_ptr<void>& context) override; };更新「handler_observer.cpp」,方法是選擇或刪除產生
handler_observer的類別實作。 請勿 移除上一個步驟所產生的預處理器指示詞(#pragma、#include)。 然後在任何現有的預處理器指令之後,將下列原始碼複製/貼到檔案中。#include "handler_observer.h" using std::shared_ptr; using std::promise; using std::exception_ptr; void ProtectionHandlerObserver::OnCreateProtectionHandlerSuccess( const shared_ptr<mip::ProtectionHandler>& protectionHandler,const shared_ptr<void>& context) { auto createProtectionHandlerPromise = static_cast<promise<shared_ptr<mip::ProtectionHandler>>*>(context.get()); createProtectionHandlerPromise->set_value(protectionHandler); }; void ProtectionHandlerObserver::OnCreateProtectionHandlerFailure( const exception_ptr& Failure, const shared_ptr<void>& context) { auto createProtectionHandlerPromise = static_cast<promise<shared_ptr<mip::ProtectionHandler>>*>(context.get()) createProtectionHandlerPromise->set_exception(Failure); };
您可以選擇性地使用 Ctrl+Shift+B (建置方案)來執行解決方案的測試編譯/連結,以確保它會成功建置,然後再繼續。
新增邏輯來加密與解密臨時文字
利用保護引擎物件加入邏輯來加密與解密臨時文字。
使用 方案總管,在您的項目中開啟包含 方法實作
main()的 .cpp 檔案。在檔案頂端的對應現有指示詞下方,新增下列 #include 和 using 指示詞:
#include "mip/protection/protection_descriptor_builder.h" #include "mip/protection_descriptor.h" #include "handler_observer.h" using mip::ProtectionDescriptor; using mip::ProtectionDescriptorBuilder; using mip::ProtectionHandler;在
Main()內容接近末尾的地方,也就是你在前一篇快速入門中停下的地方,插入以下程式碼://Encrypt/Decrypt text: string templateId = "<Template-ID>";//Template ID from previous QuickStart e.g. "bb7ed207-046a-4caf-9826-647cff56b990" string inputText = "<Sample-Text>";//Sample Text //Refer to ProtectionDescriptor docs for details on creating the descriptor auto descriptorBuilder = mip::ProtectionDescriptorBuilder::CreateFromTemplate(templateId); const std::shared_ptr<mip::ProtectionDescriptor>& descriptor = descriptorBuilder->Build(); //Create Publishing settings using a descriptor mip::ProtectionHandler::PublishingSettings publishingSettings = mip::ProtectionHandler::PublishingSettings(descriptor); //Create a publishing protection handler using Protection Descriptor auto handlerObserver = std::make_shared<ProtectionHandlerObserver>(); engine->CreateProtectionHandlerForPublishingAsync(publishingSettings, handlerObserver, pHandlerPromise); auto publishingHandler = pHandlerFuture.get(); std::vector<uint8_t> inputBuffer(inputText.begin(), inputText.end()); //Show action plan cout << "Applying Template ID " + templateId + " to: " << endl << inputText << endl; //Encrypt buffer using Publishing Handler std::vector<uint8_t> encryptedBuffer; encryptedBuffer.resize(static_cast<size_t>(publishingHandler->GetProtectedContentLength(inputText.size(), true))); publishingHandler->EncryptBuffer(0, &inputBuffer[0], static_cast<int64_t>(inputBuffer.size()), &encryptedBuffer[0], static_cast<int64_t>(encryptedBuffer.size()), true); std::string encryptedText(encryptedBuffer.begin(), encryptedBuffer.end()); cout << "Encrypted Text :" + encryptedText; //Show action plan cout << endl << "Decrypting string: " << endl << endl; //Generate publishing license, so it can be used later to decrypt text. auto serializedPublishingLicense = publishingHandler->GetSerializedPublishingLicense(); //Use same PL to decrypt the encryptedText. auto cHandlerPromise = std::make_shared<std::promise<std::shared_ptr<ProtectionHandler>>>(); auto cHandlerFuture = cHandlerPromise->get_future(); shared_ptr<ProtectionHandlerObserver> cHandlerObserver = std::make_shared<ProtectionHandlerObserver>(); //Create consumption settings using serialized publishing license. mip::ProtectionHandler::ConsumptionSettings consumptionSettings = mip::ProtectionHandler::ConsumptionSettings(serializedPublishingLicense); engine->CreateProtectionHandlerForConsumptionAsync(consumptionSettings, cHandlerObserver, cHandlerPromise); auto consumptionHandler = cHandlerFuture.get(); //Use consumption handler to decrypt the text. std::vector<uint8_t> decryptedBuffer(static_cast<size_t>(encryptedText.size())); int64_t decryptedSize = consumptionHandler->DecryptBuffer( 0, &encryptedBuffer[0], static_cast<int64_t>(encryptedBuffer.size()), &decryptedBuffer[0], static_cast<int64_t>(decryptedBuffer.size()), true); decryptedBuffer.resize(static_cast<size_t>(decryptedSize)); std::string decryptedText(decryptedBuffer.begin(), decryptedBuffer.end()); // Output decrypted content. Should match original input text. cout << "Decrypted Text :" + decryptedText << endl;在快
main()結束時,找到第一個快速啟動中建立的應用程式關閉區塊,並新增以下行以釋放處理資源:publishingHandler = nullptr; consumptionHandler = nullptr;使用字串常數來取代如下所示的原始碼中的佔位元值:
Placeholder 值 <範例文字> 你想保護的範例文字,例如: "cipher text"<模板-ID> 你想用來保護文字的範本 ID。 例如: "bb7ed207-046a-4caf-9826-647cff56b990"
組建和測試應用程式
建立並測試你的客戶應用程式。
使用 Ctrl+Shift+B(建置解決方案)來建立你的客戶端應用程式。 如果您沒有建置錯誤,請使用 F5 (開始偵錯) 來執行應用程式。
如果您的專案建置並成功執行,應用程式會在每次 SDK 呼叫您的
AcquireOAuth2Token()方法時,提示輸入存取令牌。 就像你之前在「清單保護範本」快速入門中做的一樣,執行你的 PowerShell 腳本,每次都用 $authority 和 $resourceUrl 提供的值來取得權杖。*** Template List: Name: Confidential \ All Employees : a74f5027-f3e3-4c55-abcd-74c2ee41b607 Name: Highly Confidential \ All Employees : bb7ed207-046a-4caf-9826-647cff56b990 Name: Confidential : 174bc02a-6e22-4cf2-9309-cb3d47142b05 Name: Contoso Employees Only : 667466bf-a01b-4b0a-8bbf-a79a3d96f720 Applying Template ID bb7ed207-046a-4caf-9826-647cff56b990 to: <Sample-Text> Encrypted Text :y¬╩$Ops7Γ╢╖¢t Decrypting string: Run the PowerShell script to generate an access token using the following values, then copy/paste it below: Set $authority to: https://login.microsoftonline.com/common/oauth2/authorize Set $resourceUrl to: https://aadrm.com Sign in with user account: user1@tenant.onmicrosoft.com Enter access token: <paste-access-token-here> Press any key to continue . . . Run the PowerShell script to generate an access token using the following values, then copy/paste it below: Set $authority to: https://login.microsoftonline.com/94f69844-8d34-4794-bde4-3ac89ad2b664/oauth2/authorize Set $resourceUrl to: https://aadrm.com Sign in with user account: user1@tenant.onmicrosoft.com Enter access token: <paste-access-token-here> Press any key to continue . . . Decrypted Text :<Sample-Text> C:\MIP Sample Apps\ProtectionQS\Debug\ProtectionQS.exe (process 8252) exited with code 0. To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops. Press any key to close this window . . .
下一步
- 請在 GitHub 上探索 MIP Protection SDK C++ 範例。