记录新项目中遇到的技术及自己忘记的技术点【DES加密解密,MD5加密,字符串压缩、解压,字符串截取等操作】

一、DES加密、解密

#region DES加密解密
/// <summary>
/// 进行DES加密
/// </summary>
/// <param name="pToEncrypt">要加密的字符串</param>
/// <param name="sKey">密钥,必须为8位</param>
/// <returns>以Base64格式返回的加密字符串</returns>
public static string EncryptDES(string pToEncrypt, string sKey)
{
string key = MD5Encrypt(sKey);
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(key.Substring(, ));
des.IV = ASCIIEncoding.ASCII.GetBytes(key.Substring(, ));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
}
//byte[] b = ms.ToArray();
string str = Convert.ToBase64String(ms.ToArray());//System.Text.Encoding.UTF8.GetString(b, 0, b.Length);
ms.Close();
return str;
}
} /// <summary>
/// 解密
/// </summary>
/// <param name="pToDecrypt"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string DecryptDES(string pToDecrypt, string sKey)
{
byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);//Encoding.UTF8.GetBytes(pToDecrypt);
string key = MD5Encrypt(sKey);
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
des.Key = ASCIIEncoding.ASCII.GetBytes(key.Substring(, ));
des.IV = ASCIIEncoding.ASCII.GetBytes(key.Substring(, ));
MemoryStream ms = new MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
}
string str = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return str;
}
}
#endregion

二、MD5加密(16位)

#region MD5加密
/// <summary>
/// 给一个字符串进行MD5加密
/// </summary>
/// <param name="strText">待加密字符串</param>
/// <returns>加密后的16位字符串</returns>
private static string MD5Encrypt(string inputStr)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(inputStr)), , );
t2 = t2.Replace("-", "");
t2 = t2.ToLower();
return t2;
}
#endregion

三、字符串的压缩、解压

#region 字符串压缩
/// <summary>
/// 字符串压缩
/// </summary>
/// <returns>The string.</returns>
/// <param name="str">String.</param>
public static string CompressString(string str)
{
string compressString = string.Empty;
byte[] compressBeforeByte = Encoding.UTF8.GetBytes(str);
byte[] compressAfterByte = Compress(compressBeforeByte);
compressString = Convert.ToBase64String(compressAfterByte);
return compressString;
} /// <summary>
/// 字符串解压缩
/// </summary>
/// <returns>The string.</returns>
/// <param name="str">String.</param>
public static string DecompressString(string str)
{
string compressString = string.Empty;
byte[] compressBeforeByte = Convert.FromBase64String(str);
byte[] compressAfterByte = Decompress(compressBeforeByte);
compressString = Encoding.UTF8.GetString(compressAfterByte);
return compressString;
} /// <summary>
/// 字节数组压缩
/// </summary>
/// <param name="strSource"></param>
/// <returns></returns>
private static byte[] Compress(byte[] data)
{
try
{
MemoryStream ms = new MemoryStream();
GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
zip.Write(data, , data.Length);
zip.Close();
byte[] buffer = new byte[ms.Length];
ms.Position = ;
ms.Read(buffer, , buffer.Length);
ms.Close();
return buffer; }
catch (Exception e)
{
throw new Exception(e.Message);
}
}
/// <summary>
/// 字节数组解压缩
/// </summary>
/// <param name="strSource"></param>
/// <returns></returns>
private static byte[] Decompress(byte[] data)
{
try
{
MemoryStream ms = new MemoryStream(data);
GZipStream zip = new GZipStream(ms, CompressionMode.Decompress, true);
MemoryStream msreader = new MemoryStream();
byte[] buffer = new byte[0x1000];
while (true)
{
int reader = zip.Read(buffer, , buffer.Length);
if (reader <= )
{
break;
}
msreader.Write(buffer, , reader);
}
zip.Close();
ms.Close();
msreader.Position = ;
buffer = msreader.ToArray();
msreader.Close();
return buffer;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
#endregion

四、去掉\0

读取到的数据是“iAGAB8PHUpugMAAA==\0\0\0\0\0\0...\0\0”,在转成字符串以后不会看见\0,但是它还会存在,会影响后续的操作。因为我每个字符串的结尾肯定是“=”,只是不确定会有几个,所以只要从开始截取到最后一个“=”,就能得到我需要的数据。

string str = "2235=465876=68865845=2512436";
Console.WriteLine(str.Substring(,str.LastIndexOf('=')+));//注意+1
上一篇:Linux后台运行程序


下一篇:大数据开发-linux后台运行,关闭,查看后台任务