Windows Phone 7 Mango リリースで IsolatedStorage に DPAPI を使用する

原文の記事の投稿日: 2011 年 7 月 3 日 (日曜日)

ここしばらく、Windows Phone 7 の Mango リリースの開発に取り組んでいました。このリリースに含まれる 1 つの素晴らしい機能は (それ以外にも素晴らしい機能はたくさんありますが)、DPAPI サポートが追加されたことです。DPAPI を使用するシナリオの 1 つとして、コンテンツをローカルに格納する前にそれを DPAPI で暗号化するというものがあります。WP7 では、アプリケーションからデータをローカルに格納するために IsolatedStorage と呼ばれるものを使用します。IsolatedStorage システムには、アプリケーションからのデータの読み書きに使用できる便利なクラスが用意されています。ところが、少なくとも現時点では、DPAPI で暗号化されたコンテンツがこの方法で正しく扱えないことが判明しました。以下に詳しい事情を説明します。

DPAPI を使ってコンテンツを暗号化してから、ディスクにそのコンテンツを書き込んだと仮定しましょう。その後、この暗号化データを読み取って、暗号化を解除し、データをなんらかの目的に使用することになったとします。これは、IsolatedStorage の一般的な使用例に従って、次のように記述できます。

//get into isolated storage for the credentials
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
 //stream in our registration file
 using (var stream = new
  IsolatedStorageFileStream(REG_INFO_FILE, FileMode.Open, FileAccess.Read, store))
 {
  //read the contents into a variable
  using (var reader = new StreamReader(stream))
  {
   //create and fill the byte array with the raw data so you can use it
   byte[] rawData = new byte[reader.Length];
   reader.Read(rawData, 0, Convert.ToInt16(byteStream.Length));

   //now decrypt it
   byte[] safeData = ProtectedData.Unprotect(rawData, null);
  }
 }
}

問題は、Unprotect の呼び出しで発生します。エラーが返され、複数行のパディングが追加されます。原因は、既定の IsolatedStorageFileStream リーダーがコンテンツを読む込む際に余分な文字を追加することにあります。この問題を回避するには、操作の基盤となるストリームへの参照を取得し、そこから読み取りを直接行う必要があります。たとえば、前記のコードでは次のように記述しました。

//create and fill the byte array with the raw data so you can use it
byte[] rawData = new byte[reader.Length];
reader.Read(rawData, 0, Convert.ToInt16(byteStream.Length));

これを次のように変更します。

Stream byteStream = reader.BaseStream;                               

//create and fill the byte array with the raw data so you can use it
byte[] rawData = new byte[byteStream.Length];
byteStream.Read(rawData, 0, Convert.ToInt16(byteStream.Length));

//now decrypt it
byte[] safeData = ProtectedData.Unprotect(rawData, null);

このように BaseStream を使用すると、パディングのエラーは回避されます。

 

これはローカライズされたブログ投稿です。原文の記事は、「Using DPAPI with IsolatedStorage In Windows Phone 7 Mango Release」をご覧ください。