自我整理
/// <summary>
/// DES加密(输出Hex)
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Encrypt(string str)
{
string sKey = Config.Config.HJJRMS;
if (sKey.Length < 9)
{
for (;;)
{
if (sKey.Length < 9)
sKey += sKey;
else
break;
}
}
string encryptKey = sKey.Substring(0, 8);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
des.Padding = PaddingMode.PKCS7;
byte[] inputByteArray;
inputByteArray = Encoding.UTF8.GetBytes(str);
byte[] key = Encoding.UTF8.GetBytes(encryptKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, key), 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);
}
return ret.ToString();
}