다음을 통해 공유


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

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

필수 조건

아직 완료하지 않은 경우 계속하기 전에 다음 필수 구성 요소를 완료해야 합니다.

  • 인증된 사용자가 사용할 수 있는 보호 템플릿을 나열하기 위해 시작 Visual Studio 솔루션을 빌드하는 빠른 시작: 보호 템플릿 나열(C#)을 먼저 완료합니다. 이 "텍스트 암호화/암호 해독" 빠른 시작은 이전 텍스트를 기반으로 합니다.
  • 선택 사항: 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. Main()의 끝부분을 향해 첫 번째 빠른 시작에서 생성된 애플리케이션 종료 블록을 찾고 처리기 줄을 추가합니다.

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

    자리 표시자
    <sample-text> 암호화할 샘플 텍스트(예: My secure text).
    <template-id> 이전 빠른 시작의 콘솔 출력에서 복사한 템플릿 ID(예: bb7ed207-046a-4caf-9826-647cff56b990).

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

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

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

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

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