Error decrypting string

2

I have a method for encrypting and decrypting data. I tested it in Console Application and it works perfectly. When I take this method to the controller, I get the following error:

  

Invalid length for an array or Base64 string.

This occurs when I put a large string to decrypt (above 30+ characters). However, I need to traffic a much larger (encrypted) string.

I do not understand why the Consolle Application works and in the MVC (controller), I get this

My method for encrypting and decrypting:

public class Criptografia : ICriptografia
    {

        public Criptografia()
        {
        }


        // Essa seqüência constante é usada como um valor "salt" para as chamadas de função PasswordDeriveBytes .
        // Este tamanho da IV (em bytes) devem = (KeySize / 8). KeySize padrão é 256, portanto, a IV deve ser
        // 32 bytes de comprimento. Usando uma seqüência de 16 caracteres aqui nos dá 32 bytes quando convertido para um array de bytes.
        private static readonly byte[] initVectorBytes = Encoding.ASCII.GetBytes("tu89geji340t89u2");

        // Esta constante é utilizado para determinar o tamanho da chave do algoritmo de encriptação.
        private const int keysize = 256;
        const string senha = "exemplo";


        public static string _Encrypt(string Message)
        {
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
            MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(senha));
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;
            byte[] DataToEncrypt = UTF8.GetBytes(Message);
            try
            {
                ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
                Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
            }
            finally
            {
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }
            return Convert.ToBase64String(Results);
        }


        public string Encrypt(string Message)
        {
            return Criptografia._Encrypt(Message);
        }


        public static string _Decrypt(string Message)
        {
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
            MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(senha));
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;
            byte[] DataToDecrypt = Convert.FromBase64String(Message);
            try
            {
                ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
                Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
            }
            finally
            {
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }
            return UTF8.GetString(Results);
        }


        public  string Decrypt(string Message)
        {
           return Criptografia._Decrypt(Message);
        }

    }
}

The form I use on my Controller:

 public ActionResult Dependente(string name)
    {
        Criptografia cr= new Criptografia();   
        string nome = Request.QueryString["name"];

        string nomeDescrypt = cr.Decrypt(nome);
        var dependente =
            dependenteRepository.Dependentes.Where(r => r.sLogin == nomeDescrypt).ToList();
        return View(dependente);

    }

By error, it tells me that the length is invalid for base64, however I do not understand why it works on the console and not on the controller.

    
asked by anonymous 21.01.2015 / 18:28

1 answer

3

As the error message says, a base-64 encoded string can not be any size. The size is always multiple of 4.

What I suspect may be happening is that when you create the URL for Dependente , you are passing the name parameter without escaping or URL-encode. Because strings in base64 usually end in = (which is a special character and needs to be handled) then the value received in Dependente is arriving corrupted, probably missing the last = .

    
22.01.2015 / 03:56