Decrypt TripleDES Bad Data

I am new to encryption / decryption. I am trying to decrypt an input string that is encrypted and outputs up to 44 characters.

This is what I have so far, but I keep getting "bad data" when it tries 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;
        }
    }
+3
source share
2 answers

If you are using IV, you should use CipherMode.CBC. ECB does not use any IV.

In addition, your data is not populated at all, it contains exactly 32 bytes. To verify the decryption, usually first try without filling. Thus, you can determine by eye what supplement is used to view the received plain text.

, .

+5

, , PaddingMode

My CipherMode ECB ( ).

+3

All Articles