C#加密算法总结

时间:2022-04-01 17:04:01

C#加密算法总结

MD5加密

         /// <summary>
/// MD5加密
/// </summary>
/// <param name="strPwd">原字符串</param>
/// <returns>加密后字符串</returns>
public static string GetMD5(string strPwd)
{
//MD5 对象创建的两种方式
//MD5 md5 = MD5.Create();
MD5 md5 = new MD5CryptoServiceProvider();
//将输入的密码转换成字节数组
byte[] bPwd = Encoding.UTF8.GetBytes(strPwd);
//计算指定字节数组的哈希值
byte[] bMD5 = md5.ComputeHash(bPwd);
//释放加密服务提供类的所有资源
md5.Clear();
StringBuilder sbMD5Pwd = new StringBuilder();
for (int i = ; i < bMD5.Length; i++)
{
//将每个字节数据转换为2位的16进制的字符
sbMD5Pwd.Append(bMD5[i].ToString("x2"));
}
return sbMD5Pwd.ToString();
}

SHA1加密

         /// <summary>
/// 使用 SHA1 加密算法来加密
/// </summary>
/// <param name="sourceString">原字符串</param>
/// <returns>加密后字符串</returns>
public static string SHA1_Encrypt(string sourceString)
{
byte[] StrRes = Encoding.UTF8.GetBytes(sourceString);
HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
StrRes = iSHA.ComputeHash(StrRes);
StringBuilder EnText = new StringBuilder();
foreach (byte iByte in StrRes)
{
EnText.AppendFormat("{0:x2}", iByte);
}
return EnText.ToString();
}

SHA256加密

         /// <summary>
/// SHA256 加密
/// </summary>
/// <param name="sourceString">原字符串</param>
/// <returns>加密后字符串</returns>
public static string SHA256_Encrypt(string sourceString)
{
byte[] data = Encoding.UTF8.GetBytes(sourceString);
SHA256 shaM = SHA256.Create();
byte[] result = shaM.ComputeHash(data);
StringBuilder EnText = new StringBuilder();
foreach (byte iByte in result)
{
EnText.AppendFormat("{0:x2}", iByte);
}
return EnText.ToString();
}

SHA384加密

         /// <summary>
/// SHA384 加密
/// </summary>
/// <param name="sourceString">原字符串</param>
/// <returns>加密后字符串</returns>
public static string SHA384_Encrypt(string sourceString)
{
byte[] data = Encoding.UTF8.GetBytes(sourceString);
SHA384 shaM = SHA384.Create();
byte[] result = shaM.ComputeHash(data);
StringBuilder EnText = new StringBuilder();
foreach (byte iByte in result)
{
EnText.AppendFormat("{0:x2}", iByte);
}
return EnText.ToString();
}

SHA512加密

         /// <summary>
/// SHA512_加密
/// </summary>
/// <param name="sourceString">原字符串</param>
/// <returns>加密后字符串</returns>
public static string SHA512_Encrypt(string sourceString)
{
byte[] data = Encoding.UTF8.GetBytes(sourceString);
SHA512 shaM = new SHA512Managed();
byte[] result = shaM.ComputeHash(data);
StringBuilder EnText = new StringBuilder();
foreach (byte iByte in result)
{
EnText.AppendFormat("{0:x2}", iByte);
}
return EnText.ToString();
}

AES加密、解密

         /// <summary>
/// AES加密
/// </summary>
/// <param name="encryptStr">加密字符串</param>
/// <param name="encryptKey">密钥</param>
/// <returns>密码</returns>
public static string AESEncrypt(string encryptStr, string encryptKey)
{
if (string.IsNullOrWhiteSpace(encryptStr))
return string.Empty; encryptKey = StringHelper.SubString(encryptKey, );
encryptKey = encryptKey.PadRight(, ' '); //分组加密算法
SymmetricAlgorithm des = Rijndael.Create();
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptStr);//得到需要加密的字节数组
//设置密钥及密钥向量
des.Key = Encoding.UTF8.GetBytes(encryptKey);
des.IV = _aeskeys;
byte[] cipherBytes = null;
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock();
cipherBytes = ms.ToArray();//得到加密后的字节数组
cs.Close();
ms.Close();
}
}
return Convert.ToBase64String(cipherBytes);
} /// <summary>
/// AES解密
/// </summary>
/// <param name="decryptStr">解密字符串</param>
/// <param name="decryptKey">密钥</param>
/// <returns>原码</returns>
public static string AESDecrypt(string decryptStr, string decryptKey)
{
if (string.IsNullOrWhiteSpace(decryptStr))
return string.Empty; decryptKey = StringHelper.SubString(decryptKey, );
decryptKey = decryptKey.PadRight(, ' '); byte[] cipherText = Convert.FromBase64String(decryptStr); SymmetricAlgorithm des = Rijndael.Create();
des.Key = Encoding.UTF8.GetBytes(decryptKey);
des.IV = _aeskeys;
byte[] decryptBytes = new byte[cipherText.Length];
using (MemoryStream ms = new MemoryStream(cipherText))
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read))
{
cs.Read(decryptBytes, , decryptBytes.Length);
cs.Close();
ms.Close();
}
}
return Encoding.UTF8.GetString(decryptBytes).Replace("\0", "");//将字符串后尾的'\0'去掉
}

DES加密

  

         /// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
public string EncryptDES(string encryptString, string encryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(, ));
byte[] rgbIV = Keys;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, , inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
catch
{
return encryptString;
}
} /// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
public string DecryptDES(string decryptString, string decryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(, ));
byte[] rgbIV = Keys;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, , inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return decryptString;
}
}

加密方法封装代码:

 using System.IO;
using System.Security.Cryptography;
using System.Text; namespace EncryptTools
{
public class EncryptHelper
{
/// <summary>
/// 获取某个哈希算法对应下的哈希值
/// </summary>
/// <param name="sourceString">源字符串</param>
/// <param name="algorithm">哈希算法</param>
/// <returns>经过计算的哈希值</returns>
private static string GetHash(string sourceString, HashAlgorithm algorithm)
{
byte[] sourceBytes = Encoding.UTF8.GetBytes(sourceString);
byte[] result = algorithm.ComputeHash(sourceBytes);
algorithm.Clear();
StringBuilder sb = new StringBuilder();
for (int i = ; i < result.Length; i++)
{
sb.Append(result[i].ToString("X2"));
}
return sb.ToString();
} /// <summary>
/// 获取MD5值
/// </summary>
/// <param name="sourceString">源字符串</param>
/// <returns>MD5值</returns>
public static string GetMD5(string sourceString)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
return GetHash(sourceString, md5);
} /// <summary>
/// 获取SHA1值
/// </summary>
/// <param name="sourceString">源字符串</param>
/// <returns>SHA1值</returns>
public static string GetSHA1(string sourceString)
{
SHA1 sha1 = new SHA1CryptoServiceProvider();
return GetHash(sourceString, sha1);
} /// <summary>
/// 获取SHA256值
/// </summary>
/// <param name="sourceString">源字符串</param>
/// <returns>SHA256值</returns>
public static string GetSHA256(string sourceString)
{
SHA256 sha256 = SHA256.Create();
return GetHash(sourceString, sha256);
} /// <summary>
/// 获取SHA384值
/// </summary>
/// <param name="sourceString">源字符串</param>
/// <returns>SHA384值</returns>
public static string GetSHA384(string sourceString)
{
SHA384 sha384 = SHA384.Create();
return GetHash(sourceString, sha384);
} /// <summary>
/// 获取SHA512值
/// </summary>
/// <param name="sourceString">源字符串</param>
/// <returns>SHA512值</returns>
public static string GetSHA512(string sourceString)
{
SHA512 sha512 = SHA512.Create();
return GetHash(sourceString, sha512);
} public static string GetFileBase64String(string filePath)
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (BinaryReader reader = new BinaryReader(fs))
{
try
{
return GetBase64String(reader.ReadBytes((int)fs.Length));
}
catch (System.Exception ex)
{
throw ex;
}
}
}
} public static string GetBase64String(string sourceString)
{
byte[] buffer = Encoding.UTF8.GetBytes(sourceString);
return GetBase64String(buffer);
} public static string GetBase64String(string sourceString, Encoding encoding)
{
byte[] buffer = encoding.GetBytes(sourceString);
return GetBase64String(buffer);
} public static string GetBase64String(byte[] sourceBytes)
{
string base64String = System.Convert.ToBase64String(sourceBytes);
return base64String;
}
}
}