请高手明示,我在弄一个传输文件的实例,其中要对各种不同类型的文件进行加密,在接收方进行解码。遇到最多的就是编码方式的不同,导致的乱码。有没有比较完善的加密和解密的类啊,这个问题都困扰我一星期了,给点解决办法也好啊。尤其像rar文件,解密过程中就会出错,一般文本文件都可以加密解密的。请高手指教以下是我用的一个DES类,不知道这个类本身对解密rar文件有问题吗?//注意事项:sKey要为8位
/// <summary>
/// 加密原函数
/// </summary>
/// <param name="pToEncrypt">要加密的字符串</param>
/// <param name="sKey">加密密钥</param>
/// <returns>字符串</returns>
public static string DesEncrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
/// <summary>
/// 解密原函数
/// </summary>
/// <param name="pToDecrypt">解密字符串</param>
/// <param name="sKey">解密密钥</param>
/// <returns>字符串</returns>
public static string DesDecrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
给指条活路吧!在线等!
/// <summary>
/// 加密原函数
/// </summary>
/// <param name="pToEncrypt">要加密的字符串</param>
/// <param name="sKey">加密密钥</param>
/// <returns>字符串</returns>
public static string DesEncrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
/// <summary>
/// 解密原函数
/// </summary>
/// <param name="pToDecrypt">解密字符串</param>
/// <param name="sKey">解密密钥</param>
/// <returns>字符串</returns>
public static string DesDecrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
给指条活路吧!在线等!
解决方案 »
- 子窗体传值至父窗体
- 关于枚举(enum)
- 在Winform中,我使用一个事件把图片写到Sqlserver数据库里..遇到异常
- 100分紧急求救 DataGrid中DataKeys[]的用法
- DataGrid我用的是GridColumnStyles.Add(添加样式)后,设置RowHeaderWidth,AlternatingBackColor时却没有效果
- MSDN中的一句话,关于哈希的,有些困惑
- 各位好,请问如何获得字符A的ASCII,返过来将相应的ASCII转变为字符,谢谢!!
- 请问各位哥哥姐姐,如何一个阴历跟阳历的万年历。在线等。
- 谁有清华大学 《visual c#.net入门与提高》中 的数据库文件??太感谢了!
- 请问在c#.net中怎么调用条形码打印机打印
- combox下拉事件问题
- 有用过Summary DataGridView的吗?
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
Encoding.Default.GetString(Convert.FromBase64String("所需要解密的内容"));