59

私はこのコードを持っています:

RijndaelManaged rijndaelCipher = new RijndaelManaged();

            // Set key and IV
            rijndaelCipher.Key = Convert.FromBase64String("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678912");
            rijndaelCipher.IV = Convert.FromBase64String("1234567890123456789012345678901234567890123456789012345678901234");

私は投げます:

Specified key is not a valid size for this algorithm.

Specified initialization vector (IV) does not match the block size for this algorithm.

この文字列の何が問題になっていますか? You からの文字列の例を数えてもいいですか?

4

5 に答える 5

95

文字列 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678912" を base64 でデコードすると 48 バイト (384 ビット) になります。RijndaelManaged は、128、192、および 256 ビット キーをサポートします。

有効な 128 ビット キーはnew byte[]{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }、または base64 から取得する必要がある場合:Convert.FromBase64String("AAECAwQFBgcICQoLDA0ODw==")です。

デフォルトのブロックサイズは 128 ビットなので、同じバイト配列が IV として機能します。

于 2010-05-27T08:32:47.593 に答える
5

次のように、乱数ジェネレーター クラス (RNGCryptoServiceProvider) を使用して、指定したバッファーにランダムなバイトを入力します。

var numberOfBits = 256; // or 192 or 128, however using a larger bit size renders the encrypted data harder to decipher

var ivBytes = new byte[numberOfBits / 8]; // 8 bits per byte

new RNGCryptoServiceProvider().GetBytes(ivBytes);

var rijndaelManagedCipher = new RijndaelManaged();

//Don't forget to set the explicitly set the block size for the IV if you're not using the default of 128

rijndaelManagedCipher.BlockSize = 256;

rijndaelManagedCipher.IV = ivBytes;

同じプロセスを使用して鍵を導出できることに注意してください。お役に立てれば。

于 2013-10-27T16:05:22.153 に答える
2

RijndaelManaged アルゴリズムは、128、192、または 256 ビットのキー長をサポートします。あなたのキーはこれらのサイズのいずれかですか?

于 2010-05-27T07:32:08.153 に答える
-6

rijndaelCipher.Key の長さが 24 だとわからないので、rijndaelCipher.Key = s.SubString(0, 24); とします。

とても簡単。

于 2014-11-21T08:19:13.453 に答える