DESCryptoServiceProvider加密解密问题,在线等
这是加密的方法
----------------------------------------------------------
这是我找了两个解密的不好使
求高手指点,怎么根据第一个加密方法解密出文本来!
在线等!
解密 加密
[解决办法]
哥给你贴个例子吧,呵呵
/// <summary>
/// 类型:方法
/// 方法名称:加密字符串
/// 方法说明:根据传入的字符串和密匙返回加密后的字串
/// </summary>
/// <param name="strText">加密前的原文</param>
/// <param name="strKey">密匙</param>
/// <returns>加密后的字串</returns>
public static string Encrypt(string strText, string strKey)
{
strKey += "etk579@#$am";
Byte[] byKey = { };
Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(strKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] inputByteArray = System.Text.Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception ex)
{
//。。。。
throw ex;
}
}
/// <summary>
/// 类型:方法
/// 方法名称:解密字符串
/// 方法说明:根据传入的字符串和密匙返回解密后的字串
/// </summary>
/// <param name="strText">加密后的密文</param>
/// <param name="strKey">密匙</param>
/// <returns>解密后的字串</returns>
public static string Decrypt(string strText, string strKey)
{
strKey += "etk579@#$am";
Byte[] byKey = { };
Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
Byte[] inputByteArray = new byte[strText.Length];
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(strKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (Exception ex)
{
//。。。。。。。
throw ex;
}
}
}
catch
{
return encryptString;
}
}
/**//**//**//// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
public static string DecryptDES(string decryptString, string decryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
byte[] rgbIV = Keys;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey,
rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return decryptString;
}
}
}
}