首页 > 代码库 > 加解密封装
加解密封装
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace CCTVRigthsCenter.Util
{
public class EncryptHelper
{
/// <summary>
/// MD5 hash加密
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string MD5(string s)
{
var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
var result = BitConverter.ToString(md5.ComputeHash(UnicodeEncoding.UTF8.GetBytes(s.Trim())));
return result;
}
public static string GetMD5HashFromFile(Stream file)
{
try
{
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(file);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
catch (Exception ex)
{
throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
}
}
#region DES加密解密
//默认密钥向量
private static byte[] keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
public static string Encode(string encryptString, string encryptKey = "cmyprint")
{
encryptKey = encryptKey.Substring(0, 8);
encryptKey = encryptKey.PadRight(8, ‘ ‘);
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
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, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
/// <summary>
/// WebApi的basic认证里的加密
/// </summary>
/// <param name="data"></param>
/// <param name="application"></param>
/// <param name="key"></param>
/// <param name="IV"></param>
/// <returns></returns>
public static string Encode(string data, string application = "IOS", string key = "swkj1111", string IV = "swkj1111")
{
byte[] inputByteArray = Encoding.UTF8.GetBytes(data);
byte[] rgbKey = Encoding.UTF8.GetBytes(key);
byte[] rgbIV = Encoding.UTF8.GetBytes(IV);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
string desString = Convert.ToBase64String(mStream.ToArray());
return EncodeBase64(Encoding.UTF8, application + ":" + desString);
}
/// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
public static string Decode(string decryptString, string decryptKey = "cmyprint")
{
try
{
decryptKey = decryptKey.Substring(0, 8);
decryptKey = decryptKey.PadRight(8, ‘ ‘);
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
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, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return "";
}
}
#endregion
#region Base64加解密
/// <summary>
/// Base64加密
/// </summary>
/// <param name="codeName">加密采用的编码方式</param>
/// <param name="source">待加密的明文</param>
/// <returns></returns>
private static string EncodeBase64(Encoding encode, string source)
{
string strRulst;
byte[] bytes = encode.GetBytes(source);
try
{
strRulst = Convert.ToBase64String(bytes);
}
catch
{
strRulst = source;
}
return strRulst;
}
/// <summary>
/// Base64加密,采用utf8编码方式加密
/// </summary>
/// <param name="source">待加密的明文</param>
/// <returns>加密后的字符串</returns>
public static string EncodeBase64(string source)
{
return EncodeBase64(Encoding.UTF8, source);
}
/// <summary>
/// Base64解密
/// </summary>
/// <param name="codeName">解密采用的编码方式,注意和加密时采用的方式一致</param>
/// <param name="result">待解密的密文</param>
/// <returns>解密后的字符串</returns>
private static string DecodeBase64(Encoding encode, string result)
{
string decode = "";
byte[] bytes = Convert.FromBase64String(result);
try
{
decode = encode.GetString(bytes);
}
catch
{
decode = result;
}
return decode;
}
/// <summary>
/// Base64解密,采用utf8编码方式解密
/// </summary>
/// <param name="result">待解密的密文</param>
/// <returns>解密后的字符串</returns>
public static string DecodeBase64(string result)
{
return DecodeBase64(Encoding.UTF8, result);
}
#endregion
#region BEGIN 新增加密 add by show 2016-10-10 16:09:20
/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="application">应用名称</param>
/// <param name="key">加密密钥,要求为8位</param>
/// <param name="IV">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
public static string EncryptApplicationAndURL(string encryptString, string application, string key, string IV)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(key);
byte[] rgbIV = Encoding.UTF8.GetBytes(IV);
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, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
string desString = Convert.ToBase64String(mStream.ToArray());
return EncryptHelper.EncodeBase64(application + ":" + desString);
}
catch
{
throw;
}
}
/// <summary>
/// 描述:Base64URLSafe解密
/// 作者:show
/// 时间:2016-10-10 09:07:20
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string Base64URLSafeDecode(string text)
{
if (String.IsNullOrEmpty(text))
return "";
text = text.Replace(‘-‘, ‘+‘).Replace(‘_‘, ‘/‘);
var yu = text.Length % 4;
if (yu > 0)
{
text = text.PadRight(text.Length + 4 - yu, ‘=‘);
}
return Encoding.UTF8.GetString(Convert.FromBase64String(text));
}
/// <summary>
/// 描述:URL安全的base64编码
/// 作者:show
/// 时间:2016-10-10 15:50:20
/// </summary>
/// <param name="text">要编码的字符串</param>
/// <returns></returns>
public static string ToBase64URLSafe(string text)
{
if (String.IsNullOrEmpty(text))
return "";
byte[] bs = Encoding.UTF8.GetBytes(text);
var encodedStr = Convert.ToBase64String(bs);
encodedStr = encodedStr.Replace(‘+‘, ‘-‘).Replace(‘/‘, ‘_‘).TrimEnd(‘=‘);
return encodedStr;
}
#endregion END 新增加密
}
}
加解密封装