解密三重DES“错误数据” [英] Decrypt TripleDES "Bad Data"

查看:135
本文介绍了解密三重DES“错误数据”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是加密/解密的新手。我试图解密一个被加密并输出44个字符的输入字符串。

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

这是我到目前为止,但我不断得到坏数据它尝试执行TransformFinalBlock函数。

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;
        }
    }


推荐答案

你使用IV,那么你应该使用 CipherMode.CBC 。 ECB不使用任何IV。

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

此外,您的数据根本没有填充,它正好包含32个字节。要测试解密,通常首先尝试不填充。

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.

普通数据在这里打印过于粗糙,所以我赢得了 t。

这篇关于解密三重DES“错误数据”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆