Decrypt TripleDES “Bad Data”

我只是一个虾纸丫 提交于 2020-01-04 03:49:07

问题


I'm new to encryption/decryption. I'm trying to decrypt an input string that is encrypted and comes out to 44 characters.

This is what I have so far but I keep getting "bad data" when it attempts to execute the "TransformFinalBlock" function.

public static String Decrypt(String input)
    {
        try{
            byte[] inputArray = Convert.FromBase64String(input);
            TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
            tripleDES.KeySize = 128;
            tripleDES.Key = UTF8Encoding.UTF8.GetBytes("0123456789ABCDEF");
            tripleDES.IV = UTF8Encoding.UTF8.GetBytes("ABCDEFGH");
            tripleDES.Mode = CipherMode.ECB;
            tripleDES.Padding = PaddingMode.PKCS7;
            ICryptoTransform transform = tripleDES.CreateDecryptor();
            byte[] resultArray = transform.TransformFinalBlock(inputArray, 0, inputArray.Length);
            tripleDES.Clear();

            return UTF8Encoding.UTF8.GetString(resultArray);
        }
        catch(Exception except){
            Debug.WriteLine(except + "\n\n" + except.StackTrace);
            return null;
        }
    }

回答1:


If you use an IV, then you should use CipherMode.CBC. ECB does not use any IV.

In addition, your data is not padded at all, it contains exactly 32 bytes. To test decryption, it is common to try without padding first. That way you can determine by eye which padding is used by looking at the resulting plaintext.

The plain data is too corny to print here, so I won't.




回答2:


I had a very similar issue and i fixed it by changing the PaddingMode to None

My CipherMode is ECB (Electronic code book).



来源:https://stackoverflow.com/questions/10626126/decrypt-tripledes-bad-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!