5

ここでAESメソッドを使用しています:http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.aspx

バイト配列に変換してAES暗号化メソッドに渡す文字列値が必要です。メソッドが期待する正しいバイト配列サイズを生成するには、文字列は何文字である必要がありますか?

static byte[] encryptStringToBytes_AES(string plainText, byte[] Key, byte[] IV)
    {
        // Check arguments.
        if (plainText == null || plainText.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("Key");

        // Declare the stream used to encrypt to an in memory
        // array of bytes.
        MemoryStream msEncrypt = null;

        // Declare the RijndaelManaged object
        // used to encrypt the data.
        RijndaelManaged aesAlg = null;

        try
        {
            // Create a RijndaelManaged object
            // with the specified key and IV.
            aesAlg = new RijndaelManaged();
            aesAlg.Key = Key;
            aesAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

            // Create the streams used for encryption.
            msEncrypt = new MemoryStream();
            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                {

                    //Write all data to the stream.
                    swEncrypt.Write(plainText);
                }
            }

        }
        finally
        {

            // Clear the RijndaelManaged object.
            if (aesAlg != null)
                aesAlg.Clear();
        }

        // Return the encrypted bytes from the memory stream.
        return msEncrypt.ToArray();

    }
4

3 に答える 3

6

プレーンテキストのサイズは問題ではありません。decryptStringFromBytes_AES(byte[] cipherText, byte[] Key, byte[] IV) メソッドで、暗号化されたバイトとまったく同じ IV とキーを使用していることを確認してください。入力したプレーンテキストが返されます。

例えば:


string plain_text = "Cool this works";
byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                                           0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
byte[] key = new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
                                           0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
byte[] encrytped_text = encryptStringToBytes_AES(plain_text, key, iv);
string plain_text_again = decryptStringFromBytes_AES(encrypted_text, key, iv);

ここで、plain-text と plain-text-again が同じであることがわかります。次に、plain_text を任意のものに変更して、これが正常に機能することを確認します。

RijndaelManaged のデフォルト値は次のとおりです。
BlockSize: 128
KeySize: 256
Mode: CipherMode.CBC
Padding: PaddingMode.PKCS7

有効な IV サイズは次のとおりです:
128、192、256 ビット (これは BlockSize です。使用しているサイズ IV に設定してください)
有効なキー サイズは次のとおりです:
128、192、256 ビット (これは KeySize です。使用しているサイズキーに設定します)

これは、byte[] iv が 16、24、または 32 バイト (上記の例では 16 バイト) であり、byte[] キーも 16、24、または 32 バイト (上記の例では 16 バイト) である可能性があることを意味します。 )。

それが役立つことを願っています。

于 2009-06-29T03:44:49.413 に答える
0

文字列をUnicodeバイト表現に変換しないでください。適切な長さをチェックするのは非常に難しく、十分なランダム化を提供しません。

次のことができます。鍵導出関数を使用します。関数の入力に固定長のバイト配列が必要です。これがRfc2898が得意とするところです。

したがって、新しいRfc2898オブジェクトを作成します。

using PBKDF2 = System.Security.Cryptography.Rfc2898DeriveBytes;

class Example {
    byte[] mySalt = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };

    void Initialize( string password ) {
        PBKDF2 kdf = new PBKDF2( password, mySalt );
        // Then you have your algorithm
        // When you need a key: use:
        byte[] key = kdf.GetBytes( 16 ); // for a 128-bit key (16*8=128)

        // You can specify how many bytes you need. Same for IV.
        byte[] iv = kdf.GetBytes( 16 ); // 128 bits again.

        // And then call your constructor, etc.
        // ...
    }
}

私がこれをどのように使用したかの例については、Rijndaelを使用して私のプロジェクトをチェックしてください。文字列を取得し、上記の方法を使用してキーとivバイトの配列を取得するパスワードステップがあります。

于 2009-06-29T14:19:38.653 に答える
0

そのためにはパディングが必要です。実際、リンクしたページには(C ++での)パディングの例があります。

パディングを使用すると、非標準のブロックサイズを暗号化できます。

于 2009-06-29T02:29:29.177 に答える