大家好,又见面了,我是你们的朋友全栈君。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
/******************************
* 概要:MD5加密
* 设计者:DuanXuWen
* 时间:20180309
* 版本:0.1
* 修改者:
* 修改时间:
* ***************************/
namespace Common
{
public class EncrypHelper
{
/// <summary>
///静态无参构造
/// </summary>
static EncrypHelper()
{
//默认的密钥
SecretKey = “HelloWorld..!!”;
}
/// <summary>
/// 使用SHA256加密字符串
/// </summary>
/// <param name=”Source”></param>
/// <returns></returns>
public static string EncrypToSHA(string Source)
{
SHA256Managed sha256 = new SHA256Managed();
byte[] s = UTF8Encoding.UTF8.GetBytes(Source);
byte[] t = sha256.ComputeHash(s);
return Convert.ToBase64String(t);
}
/// <summary>
/// MD5加密(32位)
/// </summary>
/// <param name=”str”>加密字符</param>
/// <returns></returns>
public static string encrypt(string str)
{
string cl = str;
string pwd = “”;
MD5 md5 = MD5.Create();
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
for (int i = 0; i < s.Length; i++)
{
pwd = pwd + s[i].ToString(“X”);
}
return pwd;
}
/// <summary>
/// 缺省的密钥
/// </summary>
public static readonly string SecretKey;
/// <summary>
/// 使用缺省密钥字符串加密string
/// </summary>
/// <param name=”original”>明文</param>
/// <returns>密文</returns>
public static string Encrypt(string original)
{
return Encrypt(original, SecretKey);
}
/// <summary>
/// 使用缺省密钥字符串解密string
/// </summary>
/// <param name=”original”>密文</param>
/// <returns>明文</returns>
public static string Decrypt(string original)
{
return Decrypt(original, SecretKey, System.Text.Encoding.Default);
}
/// <summary>
/// 使用给定密钥字符串加密string
/// </summary>
/// <param name=”original”>原始文字</param>
/// <param name=”key”>密钥</param>
/// <returns>密文</returns>
public static string Encrypt(string original, string key)
{
byte[] buff = System.Text.Encoding.Default.GetBytes(original);
byte[] kb = System.Text.Encoding.Default.GetBytes(key);
return Convert.ToBase64String(Encrypt(buff, kb));
}
/// <summary>
/// 使用给定密钥字符串解密string
/// </summary>
/// <param name=”original”>密文</param>
/// <param name=”key”>密钥</param>
/// <returns>明文</returns>
public static string Decrypt(string original, string key)
{
return Decrypt(original, key, System.Text.Encoding.Default);
}
/// <summary>
/// 使用给定密钥字符串解密string,返回指定编码方式明文
/// </summary>
/// <param name=”encrypted”>密文</param>
/// <param name=”key”>密钥</param>
/// <param name=”encoding”>字符编码方案</param>
/// <returns>明文</returns>
public static string Decrypt(string encrypted, string key, Encoding encoding)
{
byte[] buff = Convert.FromBase64String(encrypted);
byte[] kb = System.Text.Encoding.Default.GetBytes(key);
return encoding.GetString(Decrypt(buff, kb));
}
/// <summary>
/// 生成MD5摘要
/// </summary>
/// <param name=”original”>数据源</param>
/// <returns>摘要</returns>
public static byte[] MakeMd5(byte[] original)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
byte[] keyhash = hashmd5.ComputeHash(original);
hashmd5 = null;
return keyhash;
}
/// <summary>
/// 使用给定密钥加密
/// </summary>
/// <param name=”original”>明文</param>
/// <param name=”key”>密钥</param>
/// <returns>密文</returns>
public static byte[] Encrypt(byte[] original, byte[] key)
{
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = MakeMd5(key);
des.Mode = CipherMode.ECB;
return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);
}
/// <summary>
/// 使用给定密钥解密数据
/// </summary>
/// <param name=”encrypted”>密文</param>
/// <param name=”key”>密钥</param>
/// <returns>明文</returns>
public static byte[] Decrypt(byte[] encrypted, byte[] key)
{
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = MakeMd5(key);
des.Mode = CipherMode.ECB;
return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
}
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/157562.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...