下面是我写的加解密测试程序:
public class Security
{
public static string DesEncrypt(string plain, string des_key, string des_iv)
{
byte[] bytes_des_key = Encoding.UTF8.GetBytes(des_key);
byte[] bytes_des_iv = Encoding.UTF8.GetBytes(des_iv); DESCryptoServiceProvider des_encrypt = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des_encrypt.CreateEncryptor(bytes_des_key, bytes_des_iv),
CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cs); sw.WriteLine(plain); sw.Close();
cs.Close(); byte[] encrypted_bytes = ms.ToArray();
ms.Close(); //return Encoding.Unicode.GetString(encrypted_bytes);
return Encoding.UTF8.GetString(encrypted_bytes);
//return Convert.ToBase64String(encrypted_bytes);
} public static string DesDecrypt(string str_cipher, string des_key, string des_iv)
{
byte[] bytes_key = Encoding.UTF8.GetBytes(des_key);
byte[] bytes_iv = Encoding.UTF8.GetBytes(des_iv);
//byte[] bytes_clipher = Encoding.Unicode.GetBytes(str_cipher);
byte[] bytes_clipher = Encoding.UTF8.GetBytes(str_cipher);
//byte[] bytes_clipher = Convert.FromBase64String(str_cipher); DESCryptoServiceProvider des_decrypt = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(bytes_clipher);
CryptoStream cs = new CryptoStream(ms, des_decrypt.CreateDecryptor(bytes_key, bytes_iv),
CryptoStreamMode.Read); StreamReader sr = new StreamReader(cs);
string plain = sr.ReadLine(); sr.Close();
cs.Close();
ms.Close(); return plain;
} public static void Main()
{
string key = "mdm--mis";
string iv = "mis--mdm"; Console.Write("请输入要加密的字符:");
string plain = Console.ReadLine(); string str_encrypt = Security.DesEncrypt(plain, key, iv);
//string str_encrypt = Security2.EncryptDES(plain, key);
Console.WriteLine("加密后的密文是:{0}", str_encrypt); string str_decrypt = Security.DesDecrypt(str_encrypt, key, iv);
//string str_decrypt = Security2.DecryptDES(str_encrypt,key);
Console.WriteLine("解密后的密文是:{0}", str_decrypt); Console.ReadLine();
}
}
加密后如果用unicode或者base64输出加密后的字节流程序是没有问题的,但是如果换成用utf8在解密的时候就会报错——“要解密的数据的长度无效。”,请假这是什么原因呢?我用utf8可以输出加密后在字节流,但是在解密的时候就不行
public class Security
{
public static string DesEncrypt(string plain, string des_key, string des_iv)
{
byte[] bytes_des_key = Encoding.UTF8.GetBytes(des_key);
byte[] bytes_des_iv = Encoding.UTF8.GetBytes(des_iv); DESCryptoServiceProvider des_encrypt = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des_encrypt.CreateEncryptor(bytes_des_key, bytes_des_iv),
CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cs); sw.WriteLine(plain); sw.Close();
cs.Close(); byte[] encrypted_bytes = ms.ToArray();
ms.Close(); //return Encoding.Unicode.GetString(encrypted_bytes);
return Encoding.UTF8.GetString(encrypted_bytes);
//return Convert.ToBase64String(encrypted_bytes);
} public static string DesDecrypt(string str_cipher, string des_key, string des_iv)
{
byte[] bytes_key = Encoding.UTF8.GetBytes(des_key);
byte[] bytes_iv = Encoding.UTF8.GetBytes(des_iv);
//byte[] bytes_clipher = Encoding.Unicode.GetBytes(str_cipher);
byte[] bytes_clipher = Encoding.UTF8.GetBytes(str_cipher);
//byte[] bytes_clipher = Convert.FromBase64String(str_cipher); DESCryptoServiceProvider des_decrypt = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(bytes_clipher);
CryptoStream cs = new CryptoStream(ms, des_decrypt.CreateDecryptor(bytes_key, bytes_iv),
CryptoStreamMode.Read); StreamReader sr = new StreamReader(cs);
string plain = sr.ReadLine(); sr.Close();
cs.Close();
ms.Close(); return plain;
} public static void Main()
{
string key = "mdm--mis";
string iv = "mis--mdm"; Console.Write("请输入要加密的字符:");
string plain = Console.ReadLine(); string str_encrypt = Security.DesEncrypt(plain, key, iv);
//string str_encrypt = Security2.EncryptDES(plain, key);
Console.WriteLine("加密后的密文是:{0}", str_encrypt); string str_decrypt = Security.DesDecrypt(str_encrypt, key, iv);
//string str_decrypt = Security2.DecryptDES(str_encrypt,key);
Console.WriteLine("解密后的密文是:{0}", str_decrypt); Console.ReadLine();
}
}
加密后如果用unicode或者base64输出加密后的字节流程序是没有问题的,但是如果换成用utf8在解密的时候就会报错——“要解密的数据的长度无效。”,请假这是什么原因呢?我用utf8可以输出加密后在字节流,但是在解密的时候就不行
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货