大家帮忙看看我错在哪里啦
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
public class MYDES{
public static System.Security.Cryptography.DES mydes = new DESCryptoServiceProvider();
private static string m_key = "";
private static string m_iv = "";
#region 解密文件 返回解密出的字符串 string Decode(string inFileName, string key, string iv)
public static string Decode(string inFileName, string key, string iv)
{
m_key = key;
m_iv = iv;
string result = "";
try
{
mydes.IV = MYDES.GetLegalIV();
mydes.Key = MYDES.GetLegalKey();
MemoryStream mStream = new MemoryStream();
ICryptoTransform encrypto = mydes.CreateDecryptor();
CryptoStream encStream = new CryptoStream(mStream, encrypto, CryptoStreamMode.Write);
byte[] btFile = File.ReadAllBytes(inFileName);
encStream.Write(btFile, 0, btFile.Length);
encStream.FlushFinalBlock();
result = Encoding.Default.GetString(mStream.ToArray());
encStream.Close();
mStream.Close();
}catch(Exception ex)
{
return "解密失败";
}
return result;
}
#endregion
#region 解密文件 解密后生成新的文件 返回bool指示解密成功失败情况 bool Decode(string inFileName, string outFileName, string key, string iv)
public static bool Decode(string inFileName, string outFileName, string key, string iv)
{
m_key = key;
m_iv = iv;
try
{
if (outFileName.Trim().Length == 0)
outFileName = inFileName + ".decry";
FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
mydes.IV = MYDES.GetLegalIV();
mydes.Key = MYDES.GetLegalKey();
ICryptoTransform encrypto = mydes.CreateDecryptor();
CryptoStream encStream = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
byte[] btFile = File.ReadAllBytes(inFileName);
encStream.Write(btFile, 0, btFile.Length);
encStream.FlushFinalBlock();
encStream.Close();
fout.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
#endregion
#region 加密文件 返回加密后的字符串 string Encode(string inFileName, string key, string iv)
public static string Encode(string inFileName, string key, string iv)
{
m_key = key;
m_iv = iv;
string result = "";
try
{
mydes.IV = MYDES.GetLegalIV();
mydes.Key = MYDES.GetLegalKey();
MemoryStream mStream = new MemoryStream();
ICryptoTransform encrypto = mydes.CreateEncryptor();
CryptoStream encStream = new CryptoStream(mStream, encrypto, CryptoStreamMode.Write);
byte[] btFile = File.ReadAllBytes(inFileName);
encStream.Write(btFile, 0, btFile.Length);
encStream.FlushFinalBlock();
result = Convert.ToBase64String(mStream.ToArray());
encStream.Close();
mStream.Close();
}catch(Exception ex)
{
return "加密失败!";
}
return result;
}
#endregion
#region 加密文件 加密后生成新的文件 返回bool指示加密成功失败情况 bool Encode(string inFileName, string outFileName, string key, string iv)
public static bool Encode(string inFileName, string outFileName, string key, string iv)
{
m_key = key;
m_iv = iv;
try
{
if (outFileName.Trim().Length == 0)
outFileName = inFileName + ".enc";
FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
mydes.IV = MYDES.GetLegalIV();
mydes.Key = MYDES.GetLegalKey();
ICryptoTransform encrypto = mydes.CreateEncryptor();
CryptoStream encStream = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
byte[] btFile = File.ReadAllBytes(inFileName);
encStream.Write(btFile, 0, btFile.Length);
encStream.FlushFinalBlock();
encStream.Close();
fout.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
#endregion
#region 解密字符串 返回解密后的字符串 string Decrypt(string src, string key, string iv)
public static string Decrypt(string src, string key, string iv)
{
m_key = key;
m_iv = iv;
string result ="";
try
{
mydes.IV = MYDES.GetLegalIV();
mydes.Key = MYDES.GetLegalKey();
byte[] btFile = Convert.FromBase64String(src);
MemoryStream mStream = new MemoryStream();
ICryptoTransform encrypto = mydes.CreateDecryptor();
CryptoStream encStream = new CryptoStream(mStream, encrypto, CryptoStreamMode.Write);
encStream.Write(btFile, 0, btFile.Length);
encStream.FlushFinalBlock();
result = Encoding.Default.GetString(mStream.ToArray());
encStream.Close();
mStream.Close();
}catch(Exception ex)
{
return "解密失败";
}
return result;
}
#endregion
#region 解密字符串 生成解密文件 返回bool值 bool Decrypt(string src, string outFileName, string key, string iv)
public static bool Decrypt(string src, string outFileName, string key, string iv)
{
m_key = key;
m_iv = iv;
try
{
FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
mydes.IV = MYDES.GetLegalIV();
mydes.Key = MYDES.GetLegalKey();
ICryptoTransform encrypto = mydes.CreateDecryptor();
CryptoStream encStream = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
byte[] btFile = Convert.FromBase64String(src);
encStream.Write(btFile, 0, btFile.Length);
encStream.FlushFinalBlock();
encStream.Close();
fout.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
#endregion
#region 加密字符串 返回加密后的字符串 string Encrypt(string src, string key, string iv)
public static string Encrypt(string src, string key, string iv)
{
m_key = key;
m_iv = iv;
string result ="";
try
{
mydes.IV = MYDES.GetLegalIV();
mydes.Key = MYDES.GetLegalKey();
byte[] btFile = Encoding.Default.GetBytes(src);
MemoryStream mStream = new MemoryStream();
ICryptoTransform encrypto = mydes.CreateEncryptor();
CryptoStream encStream = new CryptoStream(mStream, encrypto, CryptoStreamMode.Write);
encStream.Write(btFile, 0, btFile.Length);
encStream.FlushFinalBlock();
result = Convert.ToBase64String(mStream.ToArray());
encStream.Close();
mStream.Close();
}
catch (Exception ex)
{
return "";
}
return result;
}
#endregion
#region 加密字符串 生成加密文件 返回bool值 bool Encrypt(string src, string outFileName, string key, string iv)
public static bool Encrypt(string src, string outFileName, string key, string iv)
{
m_key = key;
m_iv = iv;
try
{
FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
mydes.IV = MYDES.GetLegalIV();
mydes.Key = MYDES.GetLegalKey();
ICryptoTransform encrypto = mydes.CreateEncryptor();
CryptoStream encStream = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
byte[] btFile = Encoding.Default.GetBytes(src);
encStream.Write(btFile, 0, btFile.Length);
encStream.FlushFinalBlock();
encStream.Close();
fout.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
#endregion
private static byte[] GetLegalKey()
{
string sTemp = m_key;
mydes.GenerateKey();
byte[] bytTemp = mydes.Key;
int KeyLength = bytTemp.Length;
if (sTemp.Length > KeyLength)
sTemp = sTemp.Substring(0, KeyLength);
else if (sTemp.Length < KeyLength)
sTemp = sTemp.PadRight(KeyLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
private static byte[] GetLegalIV()
{
string sTemp = m_iv;
mydes.GenerateIV();
byte[] bytTemp = mydes.IV;
int IVLength = bytTemp.Length;
if (sTemp.Length > IVLength)
sTemp = sTemp.Substring(0, IVLength);
else if (sTemp.Length < IVLength)
sTemp = sTemp.PadRight(IVLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
}
}
}