3

ハウディ仲間のプログラマー!私のソフトウェアのユーザーが、Windows Phoneデバイスからいくつかの異なるサービスに、可能な限り最も便利で、一般的で、プラットフォームに依存しない方法で情報を安全に転送できるようにしたいと考えています。できれば、Microsoftがすでに提供しているツールを使用してください。このため、Windows PhoneSDK7.1の「System.Security.Cryptography」名前空間から利用できるAesManagedクラスを調べました。ただし、これまでのところ、このクラスでNISTの例を1つだけ再現することはできません。とりわけ、私は以下を試しました:

/* http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf page 35 and 36 */
var passwordBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
var unencryptedBytes = new byte[]{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };        

using (var aes = new AesManaged())
{
    aes.BlockSize = 128; /* size in bits */
    aes.KeySize = 128; /* size in bits */
    aes.Key = passwordBytes; 

    using (var memoryStream = new MemoryStream())
    {
        using (var cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
        {
            cryptoStream.Write(unencryptedBytes, 0, unencryptedBytes.Length);
            cryptoStream.FlushFinalBlock();

            var stream = memoryStream.ToArray();
            /* 
            should be 
            69-c4-e0-d8-6a-7b-04-30-d8-cd-b7-80-70-b4-c5-5a = 128 bit
            according to http://testprotect.com/appendix/AEScalc and fips-197.pdf
            */
            var output = BitConverter.ToString(stream); 
            /* 
            86-DB-4D-44-72-C0-16-E6-80-B9-D2-B2-3C-6D-00-40-98-4C-59-76-CF-41-DF-4E-A6-46-BB-DE-4C-13-E6-12 
            256 bit? 
            */
        }
    }  
}

IVとRfc2898DeriveBytes(http:// msdnなど)を使用するコード例(http://msdn.microsoft.com/de-de/library/system.security.cryptography.aesmanaged.aspxなど)をいくつか見ました。 microsoft.com/en-us/library/system.security.cryptography.rfc2898derivebytes.aspx)、しかし、デフォルトですべてのシステムによって理解される事実上の標準は何ですか?デフォルトの「キーパディング」アルゴリズムとは何ですか。WindowsPhoneでの使用方法(https://www.ietf.org/rfc/rfc3394.txtまたはhttps://www.ietf.org/rfc/rfc5649など)。 txt?)。そして最後に:デフォルト( http://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged.mode.aspxによるCBC )をWindows Phoneの他のモードに変更できますか?

4

1 に答える 1

2

ドキュメントが指摘していないのは、初期化ベクトルにすべてゼロを使用していることです。したがって、IVをすべてゼロの16バイトに設定すると、機能します。

/* http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf page 35 and 36 */
var passwordBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
var unencryptedBytes = new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
   0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
var initvectorBytes = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

using (var aes = new AesManaged())
{
   aes.BlockSize = 128; /* size in bits */
   aes.KeySize = 128; /* size in bits */
   aes.Key = passwordBytes;
   aes.IV = initvectorBytes;

   using (var memoryStream = new MemoryStream())
   {
      using (var cryptoStream = new CryptoStream(
         memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
      {
         cryptoStream.Write(unencryptedBytes, 0, unencryptedBytes.Length);
         cryptoStream.FlushFinalBlock();

         var stream = memoryStream.ToArray();
         /* now you get: 
            69-c4-e0-d8-6a-7b-04-30-d8-cd-b7-80-70-b4-c5-5a = 128 bit
            according to http://testprotect.com/appendix/AEScalc and fips-197.pdf
         */
         var output = BitConverter.ToString(stream);
      }
   }
}

アプリケーションでは、IVをすべてゼロ以外のものにする必要があります。AesManagedAESはCBCモード(またはECB)を使用して実装されているため、を使用してモードを変更することはできません。それ以外に設定しようとすると、例外が発生します。パディングは好きなように変更できます。PKCS7(デフォルト)のままにしておきます。

于 2012-05-15T13:49:11.537 に答える