Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This quickstart shows you how to use more of the MIP Protection SDKs. By 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 and removing protection.
Prerequisites
If you haven't already, be sure to complete the following prerequisites before continuing:
- Complete Quickstart: List protection templates (C#) first, which builds a starter Visual Studio solution to list the protection templates available to the authenticated user. This "Encrypt and 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 by using the Protection engine object.
In Solution Explorer, open the .cs file in your project that contains the implementation of the
Main()method. It defaults to the same name as the project containing it, which you specified during project creation.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 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();Toward the end of
Main(), find the application shutdown block that the first quickstart created and add the handler lines:// Application Shutdown publishingHandler = null; consumptionHandler = null; protectionEngine = null; protectionProfile = null; mipContext = null;Replace the placeholder values in the source code by using the following values:
Placeholder Value <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.
Build and test the application
Build and test your client application.
Use Ctrl+Shift+B (Build Solution) to build your client application. If you don't have build errors, use F5 (Start debugging) to run your application.
If your project builds and runs successfully, the application might prompt for authentication through MSAL each time the SDK calls your
AcquireToken()method. If cached credentials already exist, MSAL doesn't prompt you to sign in, and the application shows 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.
Next steps
- Explore the MIP Protection SDK .NET sample on GitHub.