سؤال

وأنا باستخدام أساليب 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();

    }
هل كانت مفيدة؟

المحلول

وحجم نص عادي لا يهم. فقط تأكد من استخدام نفس IV الدقيق ومفتاح جنبا إلى جنب مع بايت المشفرة في decryptStringFromBytes_AES (البايت [] النص المشفر، بايت [] مفتاح، [] بايت 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
KeySize: 256
الوضع: CipherMode.CBC
الحشو: PaddingMode.PKCS7

وأحجام IV الصحيحة هي:
128، 192، 256 بت (وهذا هو BlockSize، للتأكد من تعيينها إلى حجم IV الذي تستخدمه)
أحجام الرئيسية الصحيحة هي:
128، 192، 256 بت (وهذا هو KeySize، للتأكد من تعيينها إلى مفتاح الحجم الذي تستخدمه)

وهذا يعني أن البايت [] رابعا يمكن أن يكون 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.
        // ...
    }
}

لمثال عن كيفية لقد استعملت هذا، تحقق من مشروعي باستخدام ريجنديل . لدي خطوة كلمة المرور حيث كنت تأخذ سلسلة والحصول على المصفوفات الرئيسية والرابع بايت باستخدام الأسلوب أعلاه.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top