Share via


CryptographicException "Specified key is not a valid size for this algorithm"

Question

Friday, July 29, 2011 4:57 PM

I'm no expert in C #, but I'm trying to make a program to decrypt data with a key.

This is the code used:

private void button1_Click(object sender, EventArgs e)
        {
            byte i;
            char[] Cadena = new char[11];
            char[] Clave = new char[17];
            string A;
            string B;
            Key[0] = 40;                                                                   
            Key[1] = 174;                                                                  
            Key[2] = 49;                                                                   
            Key[3] = 169;                                                                  
            Key[4] = 241;                                                                  
            Key[5] = 12;                                                                   
            Key[6] = 46;                                                                   
            Key[7] = 250;                                                                  
            Key[8] = 243;                                                                  
            Key[9] = 88;                                                                   
            Key[10] = 170;                                                                 
            Key[11] = 4;                                                                   
            Key[12] = 150;                                                                 
            Key[13] = 152;                                                                 
            Key[14] = 233;                                                                 
            Key[15] = 62;
            Key[16] = 0;                                                   
             
            Datos[0] = 175;                                                                
            Datos[1] = 217;                                                                
            Datos[2] = 5;                                                                  
            Datos[3] = 72;                                                                 
            Datos[4] = 113;                                                                
            Datos[5] = 11;                                                                 
            Datos[6] = 254;                                                                
            Datos[7] = 117;                                                                
            Datos[8] = 159;                                                                
            Datos[9] = 95;
            Datos[10] = 0;                                                    
            //Datos[10] = 0;
          
            for (i = 0; i < 15; i++)
                Clave[i] = (char) Key[i];//.ToString;
            for (i = 0; i < 11; i++)
                Cadena[i] = (char) Datos[i];//.ToString;

            textBox1.Text = System.Convert.ToString(Cadena);
            A = "";
            B = "";
            for (i = 0; i < 11; i++)
                A += (char)(Cadena[i]);
          
            for (i = 0; i < 16; i++)
                B += (char)(Key[i]);
            textBox3.Text = desencriptar(A,B);
        }
static string desencriptar(string cadena, string clave) 
        { 
          
            byte[] cadenaBytes = Convert.FromBase64String(cadena); 
            byte[] claveBytes = Encoding.UTF8.GetBytes(clave); 
         
            RijndaelManaged rij = new RijndaelManaged(); 
        
            rij.Mode = CipherMode.ECB; 
         
            rij.BlockSize = 128; 
          
            rij.Padding = PaddingMode.Zeros; 
        
            ICryptoTransform desencriptador; 
            desencriptador = rij.CreateDecryptor(claveBytes, rij.IV); 
        
            MemoryStream memStream = new MemoryStream(cadenaBytes); 
         
            CryptoStream cifradoStream; 
            cifradoStream = new CryptoStream(memStream, desencriptador, CryptoStreamMode.Read); 
          
            StreamReader lectorStream = new StreamReader(cifradoStream); 
 
            string resultado = lectorStream.ReadToEnd(); 
        
            memStream.Close(); 
            cifradoStream.Close(); 
        
            return resultado;  
        }

And I have an error on line:

desencriptador = rij.CreateDecryptor(claveBytes, rij.IV);

 Says: "Specified key is not a valid size for this algorithm."

 What am I doing wrong?

Thanks

All replies (4)

Friday, July 29, 2011 7:22 PM ✅Answered

You probably expected to use 16 bytes as a key (128 bits), but there are two problems with what you are doing:

1) You are using 17 chars, not 16, (even '\0' chars count).

2) You are using UTF-8 to convert the string into bytes. While it is guaranteed that chars in the range 0-128 will always map to just one byte, charcters with larger values may require 2 through 4 bytes for their encoding. This means that even if you had used the correct number of characters, you wouldn't have ended up with 16 bytes.

If you really need to use a string for your key (instead of an array of bytes), you can either use an encoding that guarantees a 1 to 1 conversion between characters in the 0-255 range to bytes (such as Windows-1252) or you could hash the string and use the bytes as a key.

HTH
--mc


Friday, July 29, 2011 8:16 PM ✅Answered

Yes, of course. I was enumerating the valid key lengths, decided against it and deleted the wrong ones. Sorry for the mixup. Fixed.

--mc


Friday, July 29, 2011 7:06 PM

Keys are a set size. Here's a good example on symmetric encryption:

http://www.obviex.com/samples/Encryption.aspx


Friday, July 29, 2011 7:26 PM

"You probably expected to use 16 bytes as a key (192 bits), but there are two problems with what you are doing:"

????

 

I thing 16 bytes as a key of 128 bits.

 

Am I right?