다음을 통해 공유


빠른 시작: MIP SDK를 사용하여 텍스트 암호화/암호 해독(C#)

이 빠른 시작에서는 MIP Protection SDK를 더 많이 사용하는 방법을 보여 줍니다. 이전 빠른 시작에서 나열한 보호 템플릿 중 하나를 사용하여 보호 처리기를 사용하여 임시 텍스트를 암호화합니다. 보호 처리기 클래스는 보호를 적용/제거하기 위한 다양한 작업을 노출합니다.

필수 조건

아직 완료하지 않은 경우 계속하기 전에 다음 필수 조건을 완료해야 합니다.

  • 빠른 시작 완료: 먼저 시작 Visual Studio 솔루션을 빌드하는 보호 템플릿(C#)을 나열 하여 인증된 사용자가 사용할 수 있는 보호 템플릿을 나열합니다. 이 "텍스트 암호화/암호 해독" Quickstart는 이전 Quickstart를 기반으로 합니다.
  • 선택 사항: MIP SDK 개념에서 보호 처리기를 검토합니다.

보호 템플릿에 대한 논리를 설정 및 가져오도록 추가합니다.

보호 엔진 개체를 사용하여 임시 텍스트를 암호화하는 논리를 추가합니다.

  1. 솔루션 탐색기를 사용하여 Main()' 메서드의 구현이 포함된 프로젝트에서 .cs 파일을 엽니다. 기본 이름은 프로젝트 생성 시 지정한 이름을 포함한 프로젝트와 동일합니다.

  2. 이전 빠른 시작에서 중단한 본문의 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();
    
    
  3. 마지막 부분에 있는 첫 번째 빠른 시작에서 만든 애플리케이션 종료 블록을 찾고, 처리기 줄을 추가합니다.

    // Application Shutdown
    publishingHandler = null;
    consumptionHandler = null;
    protectionEngine = null;
    protectionProfile = null;
    mipContext = null;
    
  4. 다음 값을 사용하여 소스 코드의 자리 표시자 값을 바꿉니다.

    플레이스홀더 가치
    <샘플-텍스트> 암호화하려는 샘플 텍스트(예: My secure text.
    <template-id> 이전 빠른 시작의 콘솔 출력에서 복사한 템플릿 ID입니다. 예를 들면 다음과 bb7ed207-046a-4caf-9826-647cff56b990같습니다.

응용 프로그램 구축 및 테스트

클라이언트 애플리케이션을 빌드하고 테스트합니다.

  1. Ctrl-SHIFT-B(솔루션 빌드)를 사용하여 클라이언트 애플리케이션을 빌드합니다. 빌드 오류가 없는 경우 F5(디버깅 시작)를 사용하여 애플리케이션을 실행합니다.

  2. 프로젝트가 성공적으로 빌드되고 실행되는 경우 애플리케이션은 SDK가 메서드를 호출할 때마다 ADAL을 통해 인증을 요청하는 메시지를 표시할 수 있습니다AcquireToken(). 캐시된 자격 증명이 이미 있는 경우 로그인하고 레이블 목록을 확인하고 적용된 레이블 및 수정된 파일에 대한 정보를 확인하라는 메시지가 표시되지 않습니다.

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