快速入门:使用 MIP SDK 加密和解密文本 (C#)

本快速入门介绍如何使用更多 MIP 保护 SDK。 通过使用你在上一个快速入门中列出的保护模板之一,你可以使用保护处理程序加密临时文本。 Protection 处理程序类提供用于应用和移除保护的各种操作。

先决条件

如果尚未完成,请确保先完成以下先决条件,然后再继续:

添加逻辑以设置和获取保护模板

添加使用保护引擎对象加密临时文本的逻辑。

  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, mipContext);
    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. 使用以下值替换源代码中的占位符值:

    占位符 价值
    <示例文本> 要加密的示例文本,例如: My secure text
    <模板ID> 从上一个快速入门中的控制台输出复制的模板 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.

后续步骤