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

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

先決條件

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

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

透過保護引擎物件加入邏輯來加密臨時文字。

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

  2. Main() body 的結尾附近,也就是你在上一個快速入門中停下的地方,插入下列程式碼:

    //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 要求認證。 如果已經有快取的憑證,MSAL 不會提示你登入,應用程式會顯示標籤清單,接著是套用標籤和修改後的檔案資訊。

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

下一步