添加项目文件。
This commit is contained in:
237
Common/EncryptHelper.cs
Normal file
237
Common/EncryptHelper.cs
Normal file
@ -0,0 +1,237 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Security.Cryptography;
|
||||
using System.IO;
|
||||
namespace Epost.Common
|
||||
{
|
||||
#region 加解密
|
||||
/// <summary>
|
||||
/// 加解密
|
||||
/// </summary>
|
||||
public class EncryptHelper
|
||||
{
|
||||
#region 全局变量
|
||||
/// <summary>
|
||||
/// 加密密钥必须为8位字符串
|
||||
/// </summary>
|
||||
protected static string Key = "12345678";
|
||||
#endregion
|
||||
|
||||
#region 方法
|
||||
|
||||
#region MD5
|
||||
|
||||
#region MD5加密 16位
|
||||
/// <summary>
|
||||
/// MD5加密 16位
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string EncryptMD5By16(string str)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region MD5加密 32位
|
||||
/// <summary>
|
||||
/// MD5加密 32位
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string EncryptMD5By32(string str)
|
||||
{
|
||||
//MD5加密
|
||||
var md5 = MD5.Create();
|
||||
var bs = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
|
||||
var sb = new StringBuilder();
|
||||
foreach (byte b in bs)
|
||||
{
|
||||
sb.Append(b.ToString("x2"));
|
||||
}
|
||||
//所有字符转为大写
|
||||
return sb.ToString().ToUpper();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region MD5解密 16位
|
||||
/// <summary>
|
||||
/// MD5解密 16位
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string DecryptMD5By16(string str)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region MD5解密 32位
|
||||
/// <summary>
|
||||
/// MD5解密 32位
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string DecryptMD5By32(string str)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
#region DES
|
||||
|
||||
#region DES加密 16位
|
||||
/// <summary>
|
||||
/// DES加密 16位
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string EncryptDESBy16(string str)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region DES加密 32位
|
||||
/// <summary>
|
||||
/// DES加密 32位
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string EncryptDESBy32(string str)
|
||||
{
|
||||
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
|
||||
byte[] inputByteArray = Encoding.Default.GetBytes(str);
|
||||
des.Key = ASCIIEncoding.ASCII.GetBytes(Key);
|
||||
des.IV = ASCIIEncoding.ASCII.GetBytes(Key);
|
||||
MemoryStream ms = new MemoryStream();
|
||||
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), 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);
|
||||
}
|
||||
ret.ToString();
|
||||
return ret.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DES加密 32位
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static string EncryptDESBy32(string str, string key)
|
||||
{
|
||||
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
|
||||
byte[] inputByteArray;
|
||||
inputByteArray = Encoding.Default.GetBytes(str);
|
||||
des.Key = ASCIIEncoding.ASCII.GetBytes(key);
|
||||
des.IV = ASCIIEncoding.ASCII.GetBytes(key);
|
||||
System.IO.MemoryStream ms = new System.IO.MemoryStream();
|
||||
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), 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();
|
||||
|
||||
//DESCryptoServiceProvider des = new DESCryptoServiceProvider();
|
||||
//byte[] inputByteArray = Encoding.Default.GetBytes(str);
|
||||
//des.Key = ASCIIEncoding.ASCII.GetBytes(key);
|
||||
//des.IV = ASCIIEncoding.ASCII.GetBytes(key);
|
||||
//MemoryStream ms = new MemoryStream();
|
||||
//CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), 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);
|
||||
//}
|
||||
//ret.ToString();
|
||||
//return ret.ToString();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region DES解密 16位
|
||||
/// <summary>
|
||||
/// DES解密 16位
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string DecryptDESBy16(string str)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region DES解密 32位
|
||||
/// <summary>
|
||||
/// DES解密 32位
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string DecryptDESBy32(string str)
|
||||
{
|
||||
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
|
||||
byte[] inputByteArray = new byte[str.Length / 2];
|
||||
for (int x = 0; x < str.Length / 2; x++)
|
||||
{
|
||||
int i = (Convert.ToInt32(str.Substring(x * 2, 2), 16));
|
||||
inputByteArray[x] = (byte)i;
|
||||
}
|
||||
des.Key = ASCIIEncoding.ASCII.GetBytes(Key);
|
||||
des.IV = ASCIIEncoding.ASCII.GetBytes(Key);
|
||||
MemoryStream ms = new MemoryStream();
|
||||
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
|
||||
cs.Write(inputByteArray, 0, inputByteArray.Length);
|
||||
cs.FlushFinalBlock();
|
||||
StringBuilder ret = new StringBuilder();
|
||||
return System.Text.Encoding.Default.GetString(ms.ToArray());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DES解密 32位
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static string DecryptDESBy32(string str, string key)
|
||||
{
|
||||
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
|
||||
int len;
|
||||
len = str.Length / 2;
|
||||
byte[] inputByteArray = new byte[len];
|
||||
int x, i;
|
||||
for (x = 0; x < len; x++)
|
||||
{
|
||||
i = Convert.ToInt32(str.Substring(x * 2, 2), 16);
|
||||
inputByteArray[x] = (byte)i;
|
||||
}
|
||||
des.Key = ASCIIEncoding.ASCII.GetBytes(key);
|
||||
des.IV = ASCIIEncoding.ASCII.GetBytes(key);
|
||||
System.IO.MemoryStream ms = new System.IO.MemoryStream();
|
||||
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
|
||||
cs.Write(inputByteArray, 0, inputByteArray.Length);
|
||||
cs.FlushFinalBlock();
|
||||
return Encoding.Default.GetString(ms.ToArray());
|
||||
}
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
}
|
Reference in New Issue
Block a user