388 lines
12 KiB
C#
388 lines
12 KiB
C#
using System;
|
||
using System.IO;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
|
||
namespace Hncore.Infrastructure.Common
|
||
{
|
||
public class SecurityHelper
|
||
{
|
||
#region AES加密
|
||
|
||
/// <summary>
|
||
/// AES加密
|
||
/// </summary>
|
||
/// <param name="toEncrypt"></param>
|
||
/// <returns></returns>
|
||
public static string AESEncrypt(string toEncrypt, string key)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(toEncrypt))
|
||
return string.Empty;
|
||
// 256-AES key
|
||
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
|
||
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
|
||
|
||
RijndaelManaged rDel = new RijndaelManaged();
|
||
rDel.Key = keyArray;
|
||
rDel.Mode = CipherMode.ECB;
|
||
rDel.Padding = PaddingMode.PKCS7;
|
||
|
||
ICryptoTransform cTransform = rDel.CreateEncryptor();
|
||
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
||
|
||
|
||
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region AES解密
|
||
|
||
/// <summary>
|
||
/// AES解密
|
||
/// </summary>
|
||
/// <param name="toDecrypt"></param>
|
||
/// <returns></returns>
|
||
public static string Decrypt(string toDecrypt, string key)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(toDecrypt))
|
||
return string.Empty;
|
||
try
|
||
{
|
||
// 256-AES key
|
||
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
|
||
byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
|
||
|
||
RijndaelManaged rDel = new RijndaelManaged();
|
||
rDel.Key = keyArray;
|
||
rDel.Mode = CipherMode.ECB;
|
||
rDel.Padding = PaddingMode.PKCS7;
|
||
|
||
ICryptoTransform cTransform = rDel.CreateDecryptor();
|
||
|
||
|
||
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
||
return UTF8Encoding.UTF8.GetString(resultArray);
|
||
}
|
||
catch(Exception ex)
|
||
{
|
||
LogHelper.Error("aes Decrypt", ex.Message);
|
||
return toDecrypt;
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region MD5加密
|
||
|
||
/// <summary>
|
||
/// MD5加密
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
public static string GetMd5Hash(string input, Encoding encoding = null)
|
||
{
|
||
if (encoding == null)
|
||
{
|
||
encoding = Encoding.UTF8;
|
||
}
|
||
|
||
|
||
MD5 myMD5 = new MD5CryptoServiceProvider();
|
||
byte[] signed = myMD5.ComputeHash(encoding.GetBytes(input));
|
||
string signResult = byte2mac(signed);
|
||
return signResult.ToUpper();
|
||
}
|
||
|
||
//MD5加密方法
|
||
private static string byte2mac(byte[] signed)
|
||
{
|
||
StringBuilder EnText = new StringBuilder();
|
||
foreach (byte Byte in signed)
|
||
{
|
||
EnText.AppendFormat("{0:x2}", Byte);
|
||
}
|
||
|
||
return EnText.ToString();
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 对字符串进行DES加密
|
||
|
||
/// <summary>
|
||
/// 对字符串进行DES加密
|
||
/// </summary>
|
||
/// <param name="sourceString">待加密的字符串</param>
|
||
/// <returns>加密后的BASE64编码的字符串</returns>
|
||
public static string DesEncrypt(string sourceString, string key, string iv)
|
||
{
|
||
byte[] btKey = Encoding.Default.GetBytes(key);
|
||
byte[] btIV = Encoding.Default.GetBytes(iv);
|
||
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
|
||
using (MemoryStream ms = new MemoryStream())
|
||
{
|
||
byte[] inData = Encoding.Default.GetBytes(sourceString);
|
||
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
|
||
{
|
||
cs.Write(inData, 0, inData.Length);
|
||
cs.FlushFinalBlock();
|
||
}
|
||
|
||
return Convert.ToBase64String(ms.ToArray());
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 对DES加密后的字符串进行解密
|
||
|
||
/// <summary>
|
||
/// 对DES加密后的字符串进行解密
|
||
/// </summary>
|
||
/// <param name="encryptedString">待解密的字符串</param>
|
||
/// <returns>解密后的字符串</returns>
|
||
public static string DesDecrypt(string encryptedString, string key, string iv)
|
||
{
|
||
byte[] btKey = Encoding.Default.GetBytes(key);
|
||
byte[] btIV = Encoding.Default.GetBytes(iv);
|
||
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
|
||
using (MemoryStream ms = new MemoryStream())
|
||
{
|
||
byte[] inData = Convert.FromBase64String(encryptedString);
|
||
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
|
||
{
|
||
cs.Write(inData, 0, inData.Length);
|
||
cs.FlushFinalBlock();
|
||
}
|
||
|
||
return Encoding.Default.GetString(ms.ToArray());
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
|
||
public static string Sha1(string str)
|
||
{
|
||
SHA1 sha1 = new SHA1CryptoServiceProvider();
|
||
|
||
byte[] bytes_in = Encoding.UTF8.GetBytes(str);
|
||
byte[] bytes_out = sha1.ComputeHash(bytes_in);
|
||
sha1.Dispose();
|
||
|
||
var sb = new StringBuilder();
|
||
foreach (byte b in bytes_out)
|
||
{
|
||
sb.Append(b.ToString("x2"));
|
||
}
|
||
|
||
return sb.ToString();
|
||
}
|
||
|
||
public static string HMACSHA1(string text, string key)
|
||
{
|
||
HMACSHA1 myhmacsha1 = new HMACSHA1(Encoding.UTF8.GetBytes(key));
|
||
byte[] byteArray = Encoding.UTF8.GetBytes(text);
|
||
MemoryStream stream = new MemoryStream(byteArray);
|
||
string signature = Convert.ToBase64String(myhmacsha1.ComputeHash(stream));
|
||
|
||
return signature;
|
||
}
|
||
|
||
#region JS Aes解密
|
||
|
||
/// <summary>
|
||
/// JS Aes解密
|
||
/// </summary>
|
||
/// <param name="toDecrypt"></param>
|
||
/// <param name="key"></param>
|
||
/// <param name="iv"></param>
|
||
/// <returns></returns>
|
||
public static string JsAesDecrypt(string toDecrypt, string key, string iv)
|
||
{
|
||
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
|
||
byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv);
|
||
byte[] cipherText = HexToByteArray(toDecrypt);
|
||
// Check arguments.
|
||
if (cipherText == null || cipherText.Length <= 0)
|
||
{
|
||
throw new ArgumentNullException("cipherText");
|
||
}
|
||
|
||
if (key == null || key.Length <= 0)
|
||
{
|
||
throw new ArgumentNullException("key");
|
||
}
|
||
|
||
if (iv == null || iv.Length <= 0)
|
||
{
|
||
throw new ArgumentNullException("key");
|
||
}
|
||
|
||
string plaintext = null;
|
||
using (var rijAlg = new RijndaelManaged())
|
||
{
|
||
//Settings
|
||
rijAlg.Mode = CipherMode.CBC;
|
||
rijAlg.Padding = PaddingMode.PKCS7;
|
||
rijAlg.FeedbackSize = 128;
|
||
|
||
rijAlg.Key = keyArray;
|
||
rijAlg.IV = ivArray;
|
||
|
||
var decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
|
||
|
||
using (var msDecrypt = new MemoryStream(cipherText))
|
||
{
|
||
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
|
||
{
|
||
using (var srDecrypt = new StreamReader(csDecrypt))
|
||
{
|
||
plaintext = srDecrypt.ReadToEnd();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
return plaintext;
|
||
}
|
||
|
||
private static byte[] HexToByteArray(string hex)
|
||
{
|
||
int NumberChars = hex.Length;
|
||
byte[] bytes = new byte[NumberChars / 2];
|
||
for (int i = 0; i < NumberChars; i += 2)
|
||
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
|
||
return bytes;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region JS Aes 加密
|
||
|
||
/// <summary>
|
||
/// JsAesEncrypt
|
||
/// </summary>
|
||
/// <param name="plainText"></param>
|
||
/// <param name="key"></param>
|
||
/// <param name="iv"></param>
|
||
/// <returns></returns>
|
||
public static string JsAesEncrypt(string plainText, string key, string iv)
|
||
{
|
||
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
|
||
byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv);
|
||
|
||
// Check arguments.
|
||
if (plainText == null || plainText.Length <= 0)
|
||
{
|
||
throw new ArgumentNullException("plainText");
|
||
}
|
||
|
||
if (key == null || key.Length <= 0)
|
||
{
|
||
throw new ArgumentNullException("key");
|
||
}
|
||
|
||
if (iv == null || iv.Length <= 0)
|
||
{
|
||
throw new ArgumentNullException("key");
|
||
}
|
||
|
||
byte[] encrypted;
|
||
using (var rijAlg = new RijndaelManaged())
|
||
{
|
||
rijAlg.Mode = CipherMode.CBC;
|
||
rijAlg.Padding = PaddingMode.PKCS7;
|
||
rijAlg.FeedbackSize = 128;
|
||
|
||
rijAlg.Key = keyArray;
|
||
rijAlg.IV = ivArray;
|
||
|
||
var encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
|
||
using (var msEncrypt = new MemoryStream())
|
||
{
|
||
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
|
||
{
|
||
using (var swEncrypt = new StreamWriter(csEncrypt))
|
||
{
|
||
swEncrypt.Write(plainText);
|
||
}
|
||
|
||
encrypted = msEncrypt.ToArray();
|
||
}
|
||
}
|
||
}
|
||
|
||
// Return the encrypted bytes from the memory stream.
|
||
return ByteArrayToHex(encrypted);
|
||
}
|
||
|
||
private static string ByteArrayToHex(byte[] ba)
|
||
{
|
||
string hex = BitConverter.ToString(ba);
|
||
return hex.Replace("-", "");
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 加密隐藏信息(将原信息其中一部分数据替换为特殊字符)
|
||
|
||
/// <summary>
|
||
/// 加密隐藏信息(将原信息其中一部分数据替换为特殊字符)
|
||
/// </summary>
|
||
/// <param name="param">原参数信息</param>
|
||
/// <param name="key">更换后的特殊字符</param>
|
||
/// <param name="index">下标</param>
|
||
/// <param name="length">位数,-1代表到队尾</param>
|
||
/// <returns></returns>
|
||
public static string Encrypt(string param, string key, int index, int length = -1)
|
||
{
|
||
if (string.IsNullOrEmpty(param))
|
||
{
|
||
return "";
|
||
}
|
||
|
||
string str = "";
|
||
if (index > param.Length - 1)
|
||
{
|
||
return param;
|
||
}
|
||
|
||
str = param.Substring(0, index);
|
||
if (length == -1)
|
||
{
|
||
length = param.Length - index;
|
||
}
|
||
|
||
for (int i = 0; i < length; i++)
|
||
{
|
||
str += key;
|
||
}
|
||
|
||
if (index + length < param.Length)
|
||
{
|
||
str += param.Substring(index + length);
|
||
}
|
||
|
||
return str;
|
||
}
|
||
|
||
#endregion
|
||
|
||
|
||
/// <summary>
|
||
/// 将密码使用MD5算法求哈希值
|
||
/// </summary>
|
||
/// <param name="password"></param>
|
||
/// <returns></returns>
|
||
public static string HashPassword(string password)
|
||
{
|
||
using (MD5 md5 = MD5.Create())
|
||
{
|
||
byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(password));
|
||
return Convert.ToBase64String(bytes);
|
||
}
|
||
}
|
||
}
|
||
} |