クイックスタート: MIP SDK を使用してテキストの暗号化と暗号化の解除を行う (C#)
このクイック スタートでは、MIP Protection SDK をさらに利用する方法について説明します。 先行するクイックスタートで一覧表示した保護テンプレートのいずれかを用い、保護ハンドラーを使用して、アドホック テキストを暗号化します。 保護ハンドラー クラスには、保護の適用と削除に関するさまざまな操作が公開されています。
前提条件
先に進む前に、次の前提条件をまだ実行していない場合は完了してください。
- まず「クイックスタート: 保護テンプレートを一覧表示する (C#)」を完了し、Visual Studio のスターター ソリューションを作成し、認証済みユーザーが利用できる保護テンプレートを一覧表示します。 "テキストの暗号化と暗号化の解除" に関するこのクイックスタートは、先行するクイック スタートをベースにしています。
- 省略可能: MIP SDK の保護ハンドラーの概念を確認します。
保護テンプレートを設定して取得するロジックを追加する
保護エンジン オブジェクトを使用して、アドホック テキストを暗号化するロジックを追加します。
ソリューション エクスプローラーを使用して、Main() メソッドの実装が含まれるプロジェクトの .cs ファイルを開きます。 既定の名前は、それが含まれるプロジェクトと同じであり、プロジェクトの作成時に指定したものです。
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();
Main()
の最後の方で、最初のクイックスタートで作成したアプリケーションのシャットダウン ブロックを見つけ、ハンドラーの行を追加します。// Application Shutdown publishingHandler = null; consumptionHandler = null; protectionEngine = null; protectionProfile = null; mipContext = null;
次の値を使用して、ソース コードのプレースホルダー値を置き換えます。
プレースホルダー Value <sample-text> 暗号化するサンプル テキスト (例: My secure text
)。<template-id> 前のクイック スタートのコンソール出力からコピーされたテンプレート ID (例: bb7ed207-046a-4caf-9826-647cff56b990
)。
アプリケーションのビルドとテスト
クライアント アプリケーションをビルドしてテストします。
Ctrl + Shift + B キー ([ソリューションのビルド]) を使用して、クライアント アプリケーションをビルドします。 ビルド エラーがない場合は、F5 キー ([デバッグの開始]) を使用してアプリケーションを実行します。
プロジェクトがビルドされ、正常に実行された場合、SDK から
AcquireToken()
メソッドを呼び出すたびに、アプリケーションで ADAL による認証を求めるメッセージが表示される "場合があります"。 キャッシュされた資格情報が既に存在する場合は、サインインしてラベルの一覧を表示するように、そして、適用されたラベルと変更されたファイルに関する情報を表示するように求められることはありません。
Original content: My secure text
Encrypted content: c?_hp???Q??+<?
Decrypted content: My secure text
Press a key to quit.