本快速入门介绍如何使用更多 MIP 防护 SDK。 使用您在上一快速入门中列出的保护模板之一,通过保护处理程序加密临时文本。 保护处理程序类公开用于应用/删除保护的各种操作。
先决条件
如果尚未完成,请确保先完成以下先决条件,然后再继续:
- 完整 快速入门:首先列出保护模板(C#), 它构建了一个初学者 Visual Studio 解决方案,以列出可供经过身份验证的用户使用的保护模板。 本“加密/解密文本”快速入门指南基于上一个快速入门指南。
- (可选):查看 MIP SDK 概念中的保护处理程序 。
添加逻辑以设置和获取保护模板
使用保护引擎对象添加逻辑以加密即席文本。
使用 解决方案资源管理器,打开项目中包含 Main()方法实现的.cs文件。 它默认与其所在项目同名,这是您在创建项目时指定的。
在
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();
在
Main()
末尾找到在第一个快速入门中创建的应用程序关闭块并添加处理程序行:// Application Shutdown publishingHandler = null; consumptionHandler = null; protectionEngine = null; protectionProfile = null; mipContext = null;
使用以下值替换源代码中的占位符值:
占位符 价值 <示例文本> 要加密的示例文本,例如: My secure text
<模板ID> 从上一个快速入门中的控制台输出复制的模板 ID,例如: bb7ed207-046a-4caf-9826-647cff56b990
。
生成并测试应用
生成并测试客户端应用程序。
使用 CTRL-SHIFT-B(生成解决方案)生成客户端应用程序。 如果没有生成错误,请使用 F5(开始调试)来运行应用程序。
如果项目成功生成并运行,则每次 SDK 调用
AcquireToken()
方法时,应用程序可能会提示通过 ADAL 进行身份验证。 如果缓存的凭据已存在,系统不会提示你登录并查看标签列表,以及应用的标签和已修改文件的信息。
Original content: My secure text
Encrypted content: c?_hp???Q??+<?
Decrypted content: My secure text
Press a key to quit.