This Quickstart shows you how to use more of the MIP Protection SDKs. Using one of the protection templates you listed in the previous Quickstart, you use a Protection handler to Encrypt ad hoc text. The Protection handler class exposes various operations for applying/removing protection.
先決條件
如果您尚未完成,請務必先完成下列必要條件,再繼續進行:
- Complete Quickstart: List protection templates (C#) first, which builds a starter Visual Studio solution, to list the protection templates available to authenticated user. This "Encrypt/Decrypt text" Quickstart builds on the previous one.
- Optionally: Review Protection handlers in the MIP SDK concepts.
Add logic to set and get a protection template
Add logic to encrypt ad-hoc text, using the Protection engine object.
使用 方案總管,在您的專案中開啟包含 Main() 方法實作的 .cs 檔案。 它預設為與包含它的專案相同的名稱,此專案名稱是在您建立專案時指定的。
Toward the end of the
Main()
body, where you left off in the previous Quickstart, insert the following code://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();
Toward the end of
Main()
find the application shutdown block created in the first quickstart and add the handler lines:// Application Shutdown publishingHandler = null; consumptionHandler = null; protectionEngine = null; protectionProfile = null; mipContext = null;
使用下列值取代原始碼中的佔位元值:
Placeholder 價值 <sample-text> The sample text you would like to encrypt, for example: My secure text
.<template-id> A template ID, copied from the console output in the previous Quickstart, for example: bb7ed207-046a-4caf-9826-647cff56b990
.
建置及測試應用程式
建置及測試客戶端應用程式。
使用 CTRL-SHIFT-B (建置解決方案)來建置用戶端應用程式。 如果您沒有建置錯誤,請使用 F5 (開始偵錯) 來執行應用程式。
如果您的專案建置並成功執行,應用程式 可能會在 每次 SDK 呼叫您的
AcquireToken()
方法時,提示透過 ADAL 進行驗證。 If cached credentials already exist, you won't be prompted to sign in and see the list of labels, followed by the information on the applied label and modified file.
Original content: My secure text
Encrypted content: c?_hp???Q??+<?
Decrypted content: My secure text
Press a key to quit.