DES 是最简单常规的加密方法,一直用没有发现过问题。 ////////////////////////////////////////////////////////////////////////////DES加密****************************************************************using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptography; using System.IO;namespace Component { public class Security { public Security() {
/// <summary>
/// 加密
/// </summary>
/// <param name="string_2">字符串</param>
/// <param name="string_3">密钥</param>
/// <returns></returns>
private static string smethod_1(string string_2, string string_3)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
byte[] bytes = Encoding.Unicode.GetBytes(string_3);
byte[] buffer = Encoding.Unicode.GetBytes(string_2);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
return Convert.ToBase64String(stream.ToArray());
}
/// 类型:方法
/// 方法名称:加密字符串
/// 方法说明:根据传入的字符串和密匙返回加密后的字串
/// </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;
}
}
////////////////////////////////////////////////////////////////////////////DES加密****************************************************************using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;namespace Component
{
public class Security
{
public Security()
{
} //默认密钥向量
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
/**//**//**//// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
public static string EncryptDES(string encryptString, string encryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
byte[] rgbIV = Keys;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
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;
}
}
}
}