79 lines
3.2 KiB
C#
79 lines
3.2 KiB
C#
using System;
|
||
using System.IO;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
|
||
namespace HL_FristAidPlatform_Public
|
||
{
|
||
/// <summary>
|
||
/// 加密方法
|
||
/// </summary>
|
||
public class EncryptClass
|
||
{
|
||
private static string iv = "wangchao";
|
||
|
||
/// <summary>
|
||
/// 加密密匙 湖南翰林健康科技首拼简写
|
||
/// </summary>
|
||
private static string encryptKey = "hnscjkkj";
|
||
|
||
/// <summary>
|
||
/// DES加密字符串
|
||
/// </summary>
|
||
/// <param name="encryptString">待加密的字符串</param>
|
||
/// <param name="encryptKey">加密密钥,要求为8位</param>
|
||
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
|
||
public static string EncryptDES(string encryptString)
|
||
{
|
||
string returnStr = string.Empty;
|
||
try
|
||
{
|
||
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
|
||
byte[] rgbIV = Encoding.UTF8.GetBytes(iv);
|
||
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
|
||
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
|
||
dCSP.Mode = CipherMode.CBC;
|
||
dCSP.Padding = PaddingMode.PKCS7;
|
||
MemoryStream mStream = new MemoryStream();
|
||
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
|
||
cStream.Write(inputByteArray, 0, inputByteArray.Length);
|
||
cStream.FlushFinalBlock();
|
||
returnStr = Convert.ToBase64String(mStream.ToArray());
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
PublicClass.WriteErrorLog("EncryptClass", "EncryptDES:\r\n" + ex);
|
||
}
|
||
return returnStr;
|
||
}
|
||
|
||
/// <summary>
|
||
/// DES解密字符串
|
||
/// </summary>
|
||
/// <param name="decryptString">待解密的字符串</param>
|
||
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
|
||
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
|
||
public static string DecryptDES(string decryptString)
|
||
{
|
||
try
|
||
{
|
||
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey);
|
||
byte[] rgbIV = Encoding.UTF8.GetBytes(iv);
|
||
byte[] inputByteArray = Convert.FromBase64String(decryptString);
|
||
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
|
||
dCSP.Mode = CipherMode.CBC;
|
||
dCSP.Padding = PaddingMode.PKCS7;
|
||
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 (Exception ex)
|
||
{
|
||
PublicClass.WriteErrorLog("EncryptClass", "DecryptDES:\r\n" + ex);
|
||
return decryptString;
|
||
}
|
||
}
|
||
}
|
||
} |