Delen via


Quickstart: Tekst versleutelen/ontsleutelen met MIP SDK (C#)

In deze quickstart ziet u hoe u meer van de MIP Protection SDK's gebruikt. Met behulp van een van de beveiligingssjablonen die u in de vorige quickstart hebt vermeld, gebruikt u een beveiligingshandler om ad-hoctekst te versleutelen. De klasse Protection handler biedt verschillende bewerkingen voor het toepassen/verwijderen van beveiliging.

Vereisten

Als u dat nog niet hebt gedaan, moet u de volgende vereisten voltooien voordat u doorgaat:

Logica toevoegen om een beveiligingssjabloon in te stellen en op te halen

Voeg logica toe om ad-hoctekst te versleutelen met behulp van het object Protection Engine.

  1. Open met Solution Explorer het .cs-bestand in uw project dat de implementatie van de methode Main() bevat. Deze wordt standaard ingesteld op dezelfde naam als het project dat het bevat, die u hebt opgegeven tijdens het maken van het project.

  2. Voeg aan het einde van de Main() hoofdtekst, waar u was gebleven in de vorige quickstart, de volgende code in:

    //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. Voeg aan het einde van het afsluitblok van Main() de toepassing dat in de eerste quickstart is gemaakt, toe en voeg de handlerregels toe:

    // Application Shutdown
    publishingHandler = null;
    consumptionHandler = null;
    protectionEngine = null;
    protectionProfile = null;
    mipContext = null;
    
  4. Vervang de tijdelijke aanduidingen in de broncode met behulp van de volgende waarden:

    Tijdelijke aanduiding Waarde
    <voorbeeldtekst> De voorbeeldtekst die u wilt versleutelen, bijvoorbeeld: My secure text.
    <template-id> Een sjabloon-id, gekopieerd uit de console-uitvoer in de vorige quickstart, bijvoorbeeld: bb7ed207-046a-4caf-9826-647cff56b990.

De toepassing bouwen en testen

Bouw en test uw clienttoepassing.

  1. Gebruik Ctrl-Shift-B (build solution) om uw clienttoepassing te bouwen. Als u geen buildfouten hebt, gebruikt u F5 (Foutopsporing starten) om uw toepassing uit te voeren.

  2. Als uw project wordt gebouwd en uitgevoerd, kan de toepassing telkens wanneer de SDK uw AcquireToken() methode aanroept, om verificatie vragen via ADAL. Als er al referenties in de cache aanwezig zijn, wordt u niet gevraagd u aan te melden en de lijst met labels te zien, gevolgd door de informatie over het toegepaste label en het gewijzigde bestand.

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