对称密钥和初始化向量必须始终为128,192或256位?

时间:2013-11-19 20:11:17

标签: c# .net encryption-symmetric rijndael

以下字符串包含20个字节(160位)。我是否必须制作我的密钥和初始化向量128,192或256,或者我可以做些什么来使其成为256并保持相同的密钥:

    byte[] bbb = Encoding.ASCII.GetBytes("abcdefghijklmnopqrst");


  // Define other methods and classes here
  static string EncryptStringToBytes(string plainText, string Key, string 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");
        string encrypted;
        // Create an RijndaelManaged object 
        // with the specified key and IV. 
        using (RijndaelManaged rijAlg = new RijndaelManaged())
        {
            rijAlg.Mode = CipherMode.CBC;
            rijAlg.BlockSize = 256;
            rijAlg.KeySize = 256;
            rijAlg.Key = Encoding.ASCII.GetBytes(Key);
            rijAlg.IV = Encoding.ASCII.GetBytes(IV);

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

            // Create the streams used for encryption. 
            using (MemoryStream 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);
                    }
                    encrypted = Convert.ToBase64String(msEncrypt.ToArray());
                }
            }
        }


        // Return the encrypted bytes from the memory stream. 
        return encrypted;

    }

    static string DecryptStringFromBytes(string cipherText,string Key, string IV)
    {
        // Check arguments. 
        if (cipherText == null || cipherText.Length <= 0)
            throw new ArgumentNullException("cipherText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("Key");

        // Declare the string used to hold 
        // the decrypted text.
        string plaintext = null;

        // Create an RijndaelManaged object 
        // with the specified key and IV. 
        using (RijndaelManaged rijAlg = new RijndaelManaged())
        {
            rijAlg.Mode = CipherMode.CBC;
            rijAlg.BlockSize = 256;
            rijAlg.KeySize = 256;
            rijAlg.Key = Encoding.ASCII.GetBytes(Key);
            rijAlg.IV = Encoding.ASCII.GetBytes(IV);

            // Create a decrytor to perform the stream transform.
            ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

            // Create the streams used for decryption. 
            using (MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(cipherText)))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {

                        // Read the decrypted bytes from the decrypting stream 
                        // and place them in a string.
                        plaintext = srDecrypt.ReadToEnd();

                    }
                }
            }

        }

        return plaintext;

    }

1 个答案:

答案 0 :(得分:0)

这取决于您使用的System.Security.Cryptography.SymmetricAlgorithm的实施方式。合法密钥和块大小取决于算法。您可以查看LegalKeySizesLegalBlockSizes属性,以查看特定算法支持的尺寸。您还可以使用ValidKeySize()方法检查特定密钥大小是否对您的算法有效。

IIRC,IV的大小必须与使用的块大小相同。

另外,通常情况下,你会创建一个特定算法的实例,并让它创建一个随机密钥和IV,然后你可以保存以供以后使用。