Guida introduttiva: Crittografare/decrittografare il testo usando SDK MIP (C#)Quickstart: Encrypt/Decrypt text using MIP SDK (C#)
Questo Avvio rapido illustra come sfruttare le API Protezione di MIP.This Quickstart shows you how to use more of the MIP Protection APIs. Con uno dei modelli di protezione elencati nell'Avvio rapido precedente, si usa un gestore di Protezione per crittografare il testo ad hoc.Using one of the protection templates you listed in the previous Quickstart, you use a Protection handler to Encrypt ad hoc text. La classe del gestore di Protezione espone diverse operazioni per applicare/rimuovere la protezione.The Protection handler class exposes various operations for applying/removing protection.
PrerequisitiPrerequisites
Se non è già stato fatto, completare i prerequisiti seguenti prima di continuare:If you haven't already, be sure to complete the following prerequisites before continuing:
- In primo luogo completare Avvio rapido: In primo luogo elencare i modelli di protezione (C#), che compilano una soluzione Visual Studio iniziale, per elencare i modelli di protezione disponibili per un utente autenticato.Complete Quickstart: List protection templates (C#) first, which builds a starter Visual Studio solution, to list the protection templates available to authenticated user. Questo Avvio rapido per crittografare/decrittografare il testo si basa su sull'avvio precedente.This "Encrypt/Decrypt text" Quickstart builds on the previous one.
- Facoltativamente: Rivedere i concetti in Gestori di Protezione nell'SDK MIP.Optionally: Review Protection handlers in the MIP SDK concepts.
Aggiungere codice per impostare e ottenere un'etichetta di riservatezzaAdd logic to set and get a sensitivity label
Aggiungere la logica per crittografare il testo ad hoc, usando l'oggetto motore di Protezione.Add logic to encrypt ad-hoc text, using the Protection engine object.
Usare Esplora soluzioni per aprire il file con estensione cs nel progetto che contiene l'implementazione del metodo Main()`.Using Solution Explorer, open the .cs file in your project that contains the implementation of the Main()` method. Per impostazione predefinita il file ha lo stesso nome del progetto che lo contiene, specificato durante la creazione del progetto.It defaults to the same name as the project containing it, which you specified during project creation.
Verso la fine del corpo
Main()
, nel punto in cui è stato interrotto l'Avvio rapido precedente inserire il codice seguente:Toward the end of theMain()
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();
Verso la fine di
Main()
trovare il blocco di arresto dell'applicazione creato nel primo avvio rapido e aggiungere le righe del gestore:Toward the end ofMain()
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;
Sostituire i valori segnaposto nel codice sorgente, usando i valori seguenti:Replace the placeholder values in the source code using the following values:
SegnapostoPlaceholder ValoreValue <sample-text> Testo di esempio che si vuole crittografare, ad esempio: My secure text
.The sample text you would like to encrypt, for example:My secure text
.<template-id> ID modello, copiato dall'output della console nell'Avvio rapido precedente, ad esempio: bb7ed207-046a-4caf-9826-647cff56b990
.A template ID, copied from the console output in the previous Quickstart, for example:bb7ed207-046a-4caf-9826-647cff56b990
.
Compilare e testare l'applicazioneBuild and test the application
Compilare e testare l'applicazione client.Build and test your client application.
Usare CTRL+MAIUSC+B (Compila soluzione) per compilare l'applicazione client.Use CTRL-SHIFT-B (Build Solution) to build your client application. Se non si registrano errori di compilazione, premere F5 (Avvia debug) per eseguire l'applicazione.If you have no build errors, use F5 (Start debugging) to run your application.
Se il progetto viene compilato ed eseguito correttamente, l'applicazione potrebbe richiedere l'autenticazione tramite ADAL ogni volta che il SDK chiama il metodo
AcquireToken()
.If your project builds and runs successfully, the application may prompt for authentication via ADAL each time the SDK calls yourAcquireToken()
method. Se esistono già credenziali memorizzate nella cache, non verrà richiesto di accedere e visualizzare l'elenco delle etichette e quindi le informazioni sull'etichetta applicata e sul file modificato.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.