/// <summary>
/// Base64加密
/// </summary>
/// <param name="source">待加密的明文</param>
/// <param name="encode">编码方式</param>
/// <returns></returns>
public static string EncodeBase64(string source, Encoding encode)
{
byte[] bytes = encode.GetBytes(source);
return Convert.ToBase64String(bytes);
}
/// <summary>
/// Base64加密
/// </summary>
/// <param name="source">待加密的明文</param>
/// <returns></returns>
///
/// <seealso cref="SecurityUtil.EncodeBase64(string,Encoding)">
/// 参看SecurityUtil.EncodeBase64(string,Encoding)方法的说明 </seealso>
public static string EncodeBase64(string source)
{
return EncodeBase64(source, Encoding.UTF8);
}
/// <summary>
/// Base64解密
/// </summary>
/// <param name="source">待解密的明文</param>
/// <param name="encode">编码方式</param>
/// <returns></returns>
public static string DecodeBase64(string source, Encoding encode)
{
string decode = "";
byte[] bytes = Convert.FromBase64String(source);
decode = encode.GetString(bytes);
return decode;
}
/// <summary>
/// AES加密
/// </summary>
/// <param name="context">待加密的内容</param>
/// <param name="keyBytes">加密密钥</param>
/// <param name="keySize"></param>
/// <param name="blockSize"></param>
/// <param name="ivBytes"></param>
/// <returns></returns>
public static string AESEncrypt(string context, byte[] keyBytes,int keySize,int blockSize,byte[] ivBytes)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.PKCS7;
rijndaelCipher.KeySize = keySize;
rijndaelCipher.BlockSize = blockSize;
// 加密密钥
rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = ivBytes;
ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
byte[] plainText = Encoding.UTF8.GetBytes(context);
byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
return Convert.ToBase64String(cipherBytes);
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="context"></param>
/// <param name="keyBytes"></param>
/// <param name="keySize"></param>
/// <param name="blockSize"></param>
/// <param name="ivBytes"></param>
/// <returns></returns>
public static string AESDecrypt(string context, byte[] keyBytes, int keySize, int blockSize, byte[] ivBytes)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.PKCS7;
rijndaelCipher.KeySize = keySize;
rijndaelCipher.BlockSize = blockSize;
byte[] encryptedData = Convert.FromBase64String(context);
rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = ivBytes;
ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
return Encoding.UTF8.GetString(plainText);
}
//16位加密
/// <summary>
/// MD516位加密
/// </summary>
/// <param name="encryptData">待加密的数据</param>
/// <param name="encodingType">编码方式</param>
/// <returns></returns>
public static string Encrypt16(string encryptData, Encoding encodingType)
{
byte[] bEncrypt = encodingType.GetBytes(encryptData);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(bEncrypt, 4, 8));
t2 = t2.Replace("-", "");
return t2;
}
//32位加密
/// <summary>
/// MD532位加密(大写)
/// </summary>
/// <param name="encryptData">待加密的数据</param>
/// <param name="encodingType">编码方式</param>
/// <returns></returns>
public static string Encrypt32(string encryptData, Encoding encodingType)
{
byte[] bEncrypt = encodingType.GetBytes(encryptData);
byte[] bComputeHash = MD5.Create().ComputeHash(bEncrypt);
StringBuilder sb = new StringBuilder();
foreach (byte item in bComputeHash)
{
sb.Append(item.ToString("X2"));
}
return sb.ToString();
}
//32位加密
/// <summary>
/// MD532位加密(小写)
/// </summary>
/// <param name="encryptData">待加密的数据</param>
/// <param name="encodingType">编码方式</param>
/// <returns></returns>
public static string Encrypt32Lower(string encryptData, Encoding encodingType)
{
byte[] bEncrypt = encodingType.GetBytes(encryptData);
byte[] bComputeHash = MD5.Create().ComputeHash(bEncrypt);
StringBuilder sb = new StringBuilder();
foreach (byte item in bComputeHash)
{
sb.Append(item.ToString("x2"));
}
return sb.ToString();
}
//64位加密
/// <summary>
/// MD564位加密
/// </summary>
/// <param name="encryptData">待加密的数据</param>
/// <param name="encodingType">编码方式</param>
/// <returns></returns>
public static string Encrypt64(string encryptData, Encoding encodingType)
{
byte[] bEncrypt = encodingType.GetBytes(encryptData);
//string pwd = "";
MD5 md5 = MD5.Create(); //实例化一个md5对像
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择
byte[] s = md5.ComputeHash(bEncrypt);
return Convert.ToBase64String(s);
}
/// <summary>
/// 字符串 str 按照简体中文(ASCIIEncoding.ASCII)的编码方式
/// </summary>
/// <param name="encodingData">待编码的内容</param>
/// <returns></returns>
public static byte[] GetBytesByASCIIEncoding(string encodingData) {
return ASCIIEncoding.ASCII.GetBytes(encodingData);
}
public static byte[] GetSHA1HashBytes(byte[] encryptData) {
return new SHA1CryptoServiceProvider().ComputeHash(encryptData);
}
/// <summary>
/// HmacSHA256加密64位
/// </summary>
/// <param name="message">待加密内容</param>
/// <param name="secret">加密密钥</param>
/// <returns></returns>
public static string HmacSHA256Encrypt64(string message,string secret) {
return HmacSHA256Encrypt64(message,secret,new System.Text.ASCIIEncoding());
}
/// <summary>
/// HmacSHA256加密64位
/// </summary>
/// <param name="message">待加密内容</param>
/// <param name="secret">加密密钥</param>
/// <param name="encoding">编码方式</param>
/// <returns></returns>
public static string HmacSHA256Encrypt64(string message, string secret,Encoding encoding)
{
secret = secret ?? "";
byte[] keyByte = encoding.GetBytes(secret);
byte[] messageBytes = encoding.GetBytes(message);
using (var hmacsha256 = new HMACSHA256(keyByte))
{
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
return Convert.ToBase64String(hashmessage);
}
}
Base64、AES、MD5、HMACSHA256的加密解密
最新推荐文章于 2024-07-18 18:44:07 发布