次の方法で共有


クイック スタート: MIP SDK を使用してテキストを暗号化/暗号化解除する (C#)

このクイック スタートでは、MIP Protection SDK をさらに使用する方法について説明します。 前のクイック スタートで示した保護テンプレートのいずれかを使用して、保護ハンドラーを使用してアドホック テキストを暗号化します。 Protection ハンドラー クラスは、保護を適用または削除するためのさまざまな操作を公開します。

前提条件

まだ行っていない場合は、続行する前に次の前提条件を満たしていることを確認してください。

保護テンプレートを設定して取得するロジックを追加する

保護エンジン オブジェクトを使用してアドホック テキストを暗号化するロジックを追加します。

  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. 次の値を使用して、ソース コード内のプレースホルダーの値を置き換えます。

    プレースホルダー 価値
    <サンプルテキスト> 暗号化するサンプル テキスト (例: My secure text)。
    <テンプレート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.