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?
加密时Encoding.UTF8.GetString(memory.ToArray());会丢失无法转成UTF8的内容,所以解不开
这个怎么解决的呢?