首页新闻找找看学习计划

DES解密出错:要解密的数据的长度无效

0
悬赏园豆:10 [待解决问题]
public class EncryptHelpr
{
    private static readonly string key = "a1234567";
    private static readonly string iv = "a1234567";

    public static string Encrypt(string data)
    {
        string result = string.Empty;
        byte[] bytes = Encoding.UTF8.GetBytes(data);
        DESCryptoServiceProvider provider = new DESCryptoServiceProvider() 
        { 
            Key = Encoding.ASCII.GetBytes(key), 
            IV = Encoding.ASCII.GetBytes(iv)
        };
        using (MemoryStream memory = new MemoryStream())
        {
            using (CryptoStream crypto = new CryptoStream(memory, provider.CreateEncryptor(), CryptoStreamMode.Write))
            {
                crypto.Write(bytes, 0, bytes.Length);                    
            }
            //result = Convert.ToBase64String(memory.ToArray());
            result = Encoding.UTF8.GetString(memory.ToArray());
                
        }
        return result;
    }

    public static string Decrypt(string data)
    {
        string result = string.Empty;
        //byte[] bytes = Convert.FromBase64String(data);
        byte[] bytes = Encoding.UTF8.GetBytes(data);
        DESCryptoServiceProvider provider = new DESCryptoServiceProvider()
        {
            Key = Encoding.ASCII.GetBytes(key),
            IV = Encoding.ASCII.GetBytes(iv)
        };
        using (MemoryStream memory = new MemoryStream())
        {
            using (CryptoStream crypto = new CryptoStream(memory, provider.CreateDecryptor(), CryptoStreamMode.Write))
            {
                crypto.Write(bytes, 0, bytes.Length);
            }
            result = Encoding.UTF8.GetString(memory.ToArray());
        }
        return result;
    }
}

将注释的两行改为红色代码后,解密时总出错,提示"要解密的数据的长度无效",why?

des
张远强的主页 张远强 | 初学一级 | 园豆:153
提问于:2013-04-21 09:24
< >
分享
所有回答(2)
1

加密时Encoding.UTF8.GetString(memory.ToArray());会丢失无法转成UTF8的内容,所以解不开

Lionheart Zhang | 园豆:334 (菜鸟二级) | 2013-11-15 15:07
0

这个怎么解决的呢?

韩梦芫 | 园豆:242 (菜鸟二级) | 2018-01-12 17:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册