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

本快速入门介绍如何使用更多 MIP 保护 SDK。 使用在上一个快速入门中列出的保护模板之一,可以使用保护处理程序来加密临时文本。 保护处理程序类公开用于应用/删除保护的各种操作。

先决条件

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

  • 首先完成快速入门:列出保护模板 (C#),它会构建一个入门级 Visual Studio 解决方案,以列出经过身份验证的用户可用的保护模板。 此“加密/解密文本”快速入门建立在上一个快速入门的基础之上。
  • 可选:查看 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 licence
    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. 将源代码中的占位符值替换为以下值:

    占位符
    <sample-text> 要加密的示例文本,例如:My secure text
    <template-id> 从上一个快速入门中的控制台输出复制的模板 ID,例如:bb7ed207-046a-4caf-9826-647cff56b990

生成并测试应用

生成和测试客户端应用程序。

  1. 使用 CTRL-SHIFT-B(生成解决方案)生成客户端应用程序。 如果没有生成错误,请使用 F5(启动调试)运行应用程序。

  2. 如果项目成功生成并运行,则应用程序可能在 SDK 每次调用 AcquireToken() 方法时都会提示通过 ADAL 进行身份验证。 如果缓存的凭据已存在,系统不会提示你登录并查看标签列表,以及应用的标签和已修改文件的信息。

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