DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法。但该算法一旦密钥固定,加密的字符串也就随之固定,这不利于数据存储安全。而且用该方法加密后有一个显著的标记,末位几位为‘=’。所有本次优化对密钥和去等进行了一些优化。
首先我们生成8位随机字符串作为加密的key值。
private static string so = "1234567890abcdefghijklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM"; /// <summary>
/// 生成内容随机key
/// </summary>
/// <returns></returns>
public static string GetContent()
{
Random rand = new Random();
string str = null;
for (int i = ; i < ; i++)
{
str += so.Substring(rand.Next(), );
}
return str;
}
然后将key值进行拆分为两部分。
Random rand = new Random();
int r = rand.Next();
string encryptKey = GetContent();
string first = encryptKey.Substring(, r);
string last = encryptKey.Substring(r);
最后对需加密的字符串进行DES加密,并记录等号的个数并截取去除等号字符串,然后与key值左右部分拼接。
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
public static string Encrypt(string encryptString)
{
try
{
Random rand = new Random();
int r = rand.Next();
string encryptKey = GetContent();
string first = encryptKey.Substring(, r);
string last = encryptKey.Substring(r); byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(, ));
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, , inputByteArray.Length);
cStream.FlushFinalBlock();
string str = Convert.ToBase64String(mStream.ToArray());
int i = ;
for (int j = str.Length - ; j > ; j--)
{
if (str[j] != '=')
{
break;
}
else
{
i++;
}
}
str = str.Substring(, str.Length - i);
return i.ToString()+r.ToString()+first + str +last;
}
catch
{
return encryptString;
}
}
对加密的字符串进行解密。
/// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
public static string Decrypt(string decryptString)
{
try
{
int code = Int32.Parse(decryptString.Substring(, ));
int i = Int32.Parse(decryptString.Substring(, )); string first = decryptString.Substring(, code);
string last = decryptString.Substring(decryptString.Length - + code);
string encryptKey =first+last;
decryptString = decryptString.Substring(code + , decryptString.Length - );
for (int j = ; j < i; j++)
{
decryptString += "=";
}
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey);
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, , inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return decryptString;
}
}
应用及演示:
public static void Main(string[] args)
{
string str = "xuhang";
for (int i = ; i < ; i++)
{
var m = DesNew.Encrypt(str);
Console.WriteLine(m);
Console.WriteLine(DesNew.Decrypt(m));
Console.WriteLine("------------------------------");
Thread.Sleep(); //0.5s后唤醒线程,避免由于时间短生成的数据一致
}
}