0

The following string contains 20 bytes (160 bits). Do I have to make my keys and initialization vectors 128,192, or 256 or is there something I can do to make it 256 and keep the same key:

    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;

    }
Xaisoft
  • 45,655
  • 87
  • 279
  • 432

1 Answers1

0

It depends on which implementation of System.Security.Cryptography.SymmetricAlgorithm you're using. The legal key and block sizes are dependent upon the algorith. You can check the LegalKeySizes and LegalBlockSizes property to check what sizes are supported by your particular algorithm. You can also use the ValidKeySize() method to check if a particular key size is valid for your algorithm.

IIRC, the IV needs to be sized the same size as the block size in use.

Also, normally, you'd gen up an instance of your particular algorithm and let it create a random key and IV, which you can then save for later use.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135