快速入門:使用 MIP SDK(C#)加密/解密文字

這個快速入門工具會教你如何使用更多 MIP 保護 SDK。 使用你在前述快速入門中列出的保護範本之一,你可以使用保護處理程式來加密臨時文字。 保護處理類別會揭露各種用於套用/移除保護的操作。

先決條件

如果您尚未完成,請務必先完成下列必要條件,再繼續進行:

添加邏輯以設置和獲取保護範本

加入邏輯來加密臨時文字,使用保護引擎物件。

  1. 使用 方案總管,在您的專案中開啟包含 Main() 方法實作的 .cs 檔案。 它預設為與包含它的專案相同的名稱,此專案名稱是在您建立專案時指定的。

  2. Main() 內容接近末尾的地方,也就是你在前一篇快速入門中停下的地方,插入以下程式碼:

    //Set text to encrypt and template ID
    string inputText = "<Sample-text>";
    string templateId = "<template-id>";
    //Create a template based publishing descriptor
    ProtectionDescriptor protectionDescriptor = new ProtectionDescriptor(templateId);
    
    //Create publishing settings using protection descriptor
    PublishingSettings publishingSettings = new PublishingSettings(protectionDescriptor);
    
    //Generate Protection Handler for publishing
    var publishingHandler = Task.Run(async() => await protectionEngine.CreateProtectionHandlerForPublishingAsync(publishingSettings)).Result;
    
    //Encrypt text using Publishing handler
    long bufferSize = publishingHandler.GetProtectedContentLength(inputText.Length, true);
    byte[] inputTextBuffer = Encoding.ASCII.GetBytes(inputText);
    byte[] encryptedTextBuffer = new byte[bufferSize];
    publishingHandler.EncryptBuffer(0, inputTextBuffer, encryptedTextBuffer, true);
    Console.WriteLine("Original text: {0}", inputText);
    Console.WriteLine("Encrypted text: {0}", Encoding.UTF8.GetString(encryptedTextBuffer));
    
    //Create a Protection handler for consumption using the same publishing license
    var serializedPublishingLicense = publishingHandler.GetSerializedPublishingLicense();
    PublishingLicenseInfo plInfo = PublishingLicenseInfo.GetPublishingLicenseInfo(serializedPublishingLicense);
    ConsumptionSettings consumptionSettings = new ConsumptionSettings(plInfo);
    var consumptionHandler = protectionEngine.CreateProtectionHandlerForConsumption(consumptionSettings);
    
    //Use the handler to decrypt the encrypted text
    long buffersize = encryptedTextBuffer.Length;
    byte[] decryptedBuffer = new byte[bufferSize];
    var bytesDecrypted = consumptionHandler.DecryptBuffer(0, encryptedTextBuffer, decryptedBuffer, true);
    byte[] OutputBuffer = new byte[bytesDecrypted];
    for (int i = 0; i < bytesDecrypted; i++){
       OutputBuffer[i] = decryptedBuffer[i];
    }
    
    Console.WriteLine("Decrypted content: {0}", Encoding.UTF8.GetString(OutputBuffer));
    Console.WriteLine("Press a key to quit.");
    Console.ReadKey();
    
    
  3. Main() 結尾處,找到在第一個快速入門中建立的應用程式關閉區塊,並加入處理程序行:

    // Application Shutdown
    publishingHandler = null;
    consumptionHandler = null;
    protectionEngine = null;
    protectionProfile = null;
    mipContext = null;
    
  4. 使用下列值取代原始碼中的佔位元值:

    Placeholder 價值
    <範例文字> 你想加密的範例文字,例如: My secure text
    <模板識別碼> 例如,從前一快速入門指南的控制台輸出中複製來的模板 ID:bb7ed207-046a-4caf-9826-647cff56b990

建置及測試應用程式

建置及測試客戶端應用程式。

  1. 使用 CTRL-SHIFT-B (建置解決方案)來建置用戶端應用程式。 如果您沒有建置錯誤,請使用 F5 (開始偵錯) 來執行應用程式。

  2. 如果你的專案成功建置並執行,應用程式 可能會 在每次 SDK 呼叫你的 AcquireToken() 方法時,透過 MSAL 要求驗證。 如果已經存在快取的憑證,你不需要登入即可查看標籤清單,接著會看到套用標籤和修改後的檔案資訊。

 Original content: My secure text
 Encrypted content: c?_hp???Q??+<?
 Decrypted content: My secure text
 Press a key to quit.