这个程序怎么修改???调了很多还是出错,求指教
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;
namespace DESTool
{
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);
}
}
}
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;
namespace DESTool
{
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);
}
}
}
...全文
相关推荐
不懂的工程代码,求指教
【标题】:“不懂的工程代码,求指教”在编程领域,遇到不理解或出错的工程代码是很常见的。这通常需要我们具备分析问题、调试代码以及查阅文档的能力。
求debug源程序C程序,输出无内容,可能是逻辑错误
可编译,可链接,可运行,但运行出错。一个求1000以内完数的程序,程序很简单,就是两个循环,但是逻辑出错?
解决explorer.exe 应用程序出错 问题
当用户遇到“explorer.exe 应用程序出错”的提示时,通常意味着该进程在执行过程中遇到了异常情况,导致无法正常运行。#### 二、常见原因分析1.
c++程序运行出错问题处理
### c++程序运行出错问题处理在C++编程中,遇到程序运行时出现错误的情况十分常见。这类问题可能源于多种因素,包括但不限于编译器配置不当、代码逻辑错误、库文件缺失等。
dll调用时总是出错,高手指教
解决方法是确保所有依赖项都与应用程序兼容,并避免混用不同版本的DLL。3. **API调用错误**:如果应用程序在调用DLL函数时参数传递错误,也会导致错误。


