質問

私はここに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();

    }
役に立ちましたか?

解決

プレーンテキストの大きさは重要ではありません。ちょうどあなたが暗号化されたdecryptStringFromBytes_AESのバイト(バイト[]暗号文、バイト[]キー、バイト[] 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を変更し、これが正常に動作していることがわかります。

RijndaelManagedのデフォルト値は次のとおりです。
BlockSizeは:128
キーサイズ:256
モード:CipherMode.CBC
パディング:PaddingMode.PKCS7

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

これは、バイト[] IV 16、24、または32バイト(私の上記の例では、その16バイト)であることができ、バイト[]キーまた、私の上記の例では16、24、または32バイト(得ることを意味その16バイト)。

希望に役立ちます。

他のヒント

あなたはそのためのパディングを必要とします。実際には、あなたがリンクされたページは、(C ++で)パディングの例を持っています。

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

その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.
        // ...
    }
}

私はこれを使ってきた方法の例については、ラインダールを使用して私のプロジェクトをチェックアウトに。私は、文字列を取り、上記の方法を使用してキーおよびIVバイト配列を取得し、パスワードのステップを持っています。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top