狠狠色丁香婷婷综合尤物/久久精品综合一区二区三区/中国有色金属学报/国产日韩欧美在线观看 - 国产一区二区三区四区五区tv

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

C#加密解密類:DES、3DES、MD5

admin
2021年1月30日 11:57 本文熱度 3350
using System;
using System.Security.Cryptography;
using System.IO;
using System.Data;
using System.Web;
using System.Text;
using System.Security;
namespace CMIS.Common
{
/// <summary>
/// 加密解密
/// </summary>
public class Encrypt
{
#region DES密鑰
/// <summary>
/// DES密鑰
/// </summary>
public static byte[] DESKey = new byte[] {0x52, 0xBC, 0xA1, 0x6A, 0xD5, 0x87, 0x3B, 0xE6, 0x82, 0x7A, 0x2A, 0x64, 0x3A, 0x7F, 0x32, 0xBB, 0x1B, 0x67, 0xE2, 0x5E, 0x06, 0x19, 0xB9, 0x2D, 0x68, 0xB3, 0x4F, 0xFB, 0xBF, 0xDD, 0x55 ,0xB8 };
#endregion
#region DES加密解密
/// <summary>
/// DES加密
/// </summary>
/// <param name="strSource">待加密字串</param>
/// <param name="key">32位Key值</param>
/// <returns>加密后的字符串</returns>
public static string DESEncrypt(string strSource)
{
return DESEncrypt(strSource, DESKey);
}
public static string DESEncrypt(string strSource,byte[] key)
{
SymmetricAlgorithm sa = Rijndael.Create();
sa.Key = key;
sa.Mode= CipherMode.ECB;
sa.Padding = PaddingMode.Zeros;
MemoryStream ms =
new MemoryStream();
CryptoStream cs =
new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
byte[] byt = Encoding.Unicode.GetBytes(strSource);
cs.Write(byt,
0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="strSource">待解密的字串</param>
/// <param name="key">32位Key值</param>
/// <returns>解密后的字符串</returns>
public static string DESDecrypt(string strSource)
{
return DESDecrypt(strSource, DESKey);
}
public static string DESDecrypt(string strSource,byte[] key)
{
SymmetricAlgorithm sa = Rijndael.Create();
sa.Key = key;
sa.Mode = CipherMode.ECB;
sa.Padding = PaddingMode.Zeros;
ICryptoTransform ct = sa.CreateDecryptor();
byte[] byt = Convert.FromBase64String(strSource);
MemoryStream ms =
new MemoryStream(byt);
CryptoStream cs =
new CryptoStream(ms, ct, CryptoStreamMode.Read);
StreamReader sr =
new StreamReader(cs, Encoding.Unicode);
return sr.ReadToEnd();
}
#endregion
#region 一個用hash實現的加密解密方法
/// <summary>
/// 加密
/// </summary>
/// <param name="src"></param>
/// <returns></returns>
public static string EncryptStrByHash(string src)
{
if (src.Length==0)
{
return "";
}
byte[] HaKey=System.Text.Encoding.ASCII.GetBytes((src+"Test").ToCharArray());
byte[] HaData=new byte[20];
HMACSHA1 Hmac=
new HMACSHA1(HaKey);
CryptoStream cs=
new CryptoStream(Stream.Null,Hmac,CryptoStreamMode.Write);
try
{
cs.Write(HaData,
0,HaData.Length);
}
finally
{
cs.Close();
}
string HaResult = System.Convert.ToBase64String(Hmac.Hash).Substring(0,16);
byte[] RiKey = System.Text.Encoding.ASCII.GetBytes(HaResult.ToCharArray());
byte[] RiDataBuf = System.Text.Encoding.ASCII.GetBytes(src.ToCharArray());
byte[] EncodedBytes = {};
MemoryStream ms =
new MemoryStream();
RijndaelManaged rv =
new RijndaelManaged();
cs =
new CryptoStream(ms, rv.CreateEncryptor(RiKey, RiKey), CryptoStreamMode.Write);
try
{
cs.Write(RiDataBuf,
0, RiDataBuf.Length);
cs.FlushFinalBlock();
EncodedBytes = ms.ToArray();
}
finally
{
ms.Close();
cs.Close();
}
return HaResult+System.Convert.ToBase64String(EncodedBytes);
}
/// <summary>
/// 解密
/// </summary>
/// <param name="src"></param>
/// <returns></returns>
public static string DecrypStrByHash(string src)
{
if (src.Length<40) return "";
byte[] SrcBytes = System.Convert.FromBase64String(src.Substring(16));
byte[] RiKey = System.Text.Encoding.ASCII.GetBytes(src.Substring(0,16).ToCharArray());
byte[] InitialText = new byte[SrcBytes.Length];
RijndaelManaged rv =
new RijndaelManaged();
MemoryStream ms =
new MemoryStream(SrcBytes);
CryptoStream cs =
new CryptoStream(ms, rv.CreateDecryptor(RiKey, RiKey), CryptoStreamMode.Read);
try
{
cs.Read(InitialText,
0, InitialText.Length);
}
finally
{
ms.Close();
cs.Close();
}
System.Text.StringBuilder Result =
new System.Text.StringBuilder();
for(int i=0; i < InitialText.Length; ++i) if (InitialText[i]>0) Result.Append((char) InitialText[i]);
return Result.ToString();
}

/// <summary>
/// 對加密后的密文重新編碼,如果密文長>16,則去掉前16個字符,如果長度小于16,返回空字符串
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string ReEncryptStrByHash(string s)
{
//string e= (new Encrypt()).EncryptStrByHash(s);
string e= EncryptStrByHash(s);
return ((e.Length>16)?e.Substring(16):"");
}
#endregion
#region Md5加密,生成16位或32位,生成的密文都是大寫
/// <summary>
/// MD5 16位加密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Md5To16(string str)
{
MD5CryptoServiceProvider md5 =
new MD5CryptoServiceProvider();
string pwd = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(str)), 4, 8);
pwd = pwd.Replace(
"-", "");
return pwd;
}

/// <summary>
/// MD5 32位加密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Md5To32(string str)
{
string pwd = "";
MD5 md5 = MD5.Create();
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
for (int i = 0; i < s.Length; i++){
pwd = pwd + s[i].ToString(
"X2");
}
return pwd;
}
#endregion
#region 3DES加密解密
/// <summary>
/// 3DES加密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Encrypt3DES(string str)
{
//密鑰
string sKey = "wyw308";
// //矢量,可為空
string sIV = "scf521";
// //構造對稱算法
SymmetricAlgorithm mCSP =
new TripleDESCryptoServiceProvider();
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
byt = Encoding.UTF8.GetBytes(str);
ms =
new MemoryStream();
cs =
new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt,
0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
/// <summary>
/// 帶指定密鑰和矢量的3DES加密
/// </summary>
/// <param name="str"></param>
/// <param name="sKey"></param>
/// <param name="sIV"></param>
/// <returns></returns>
public static string Encrypt3DES(string str,string sKey,string sIV)
{
SymmetricAlgorithm mCSP =
new TripleDESCryptoServiceProvider();
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
byt = Encoding.UTF8.GetBytes(str);
ms =
new MemoryStream();
cs =
new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt,
0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
/// <summary>
/// 3DES解密
/// </summary>
/// <param name="Value"></param>
/// <returns></returns>
public static string Decrypt3DES(string Value)
{
string sKey = "wyw308";
string sIV = "scf521";
SymmetricAlgorithm mCSP =
new TripleDESCryptoServiceProvider();
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
byt = Convert.FromBase64String(Value);
ms =
new MemoryStream();
cs =
new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt,
0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
}
/// <summary>
/// 帶指定密鑰和矢量的3DES解密
/// </summary>
/// <param name="Value"></param>
/// <param name="sKey"></param>
/// <param name="sIV"></param>
/// <returns></returns>
public static string Decrypt3DES(string str,string sKey,string sIV)
{
SymmetricAlgorithm mCSP =
new TripleDESCryptoServiceProvider();
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
byt = Convert.FromBase64String(str);
ms =
new MemoryStream();
cs =
new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt,
0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
}
#endregion
#region 一個簡單的加密解密方法,只支持英文
public static string EnCryptEnStr( string str ) //倒序加1加密
{
byte[] by=new byte[str.Length];
for( int i=0;
i<=str.Length
-1;
i++ )
{
by[i]=( byte )( ( byte )str[i]+1 );
}
str=
"";
for( int i=by.Length-1;
i>=
0;
i-- )
{
str+=( (
char )by[i] ).ToString( );
}
return str;
}
public static string DeCryptEnStr( string str ) //順序減1解碼
{
byte[] by=new byte[str.Length];
for( int i=0;
i<=str.Length
-1;
i++ )
{
by[i]=( byte )( ( byte )str[i]-1 );
}
str=
"";
for( int i=by.Length-1;
i>=
0;
i-- )
{
str+=( (
char )by[i] ).ToString( );
}
return str;
}
#endregion
#region 一個簡單的加密解密方法,在上一個的基礎上支持中文
public static string EnCryptCnStr(string str)
{
string htext = ""; // blank text
for ( int i = 0; i < str.Length; i++)
{
htext = htext + (
char) (str[i] + 10 - 1 * 2);
}
return htext;
}
public static string DeCryptCnStr(string str)
{
string dtext = "";
for ( int i=0; i < str.Length; i++)
{
dtext = dtext + (
char) (str[i] - 10 + 1*2);
}
return dtext;
}
#endregion
#region Discuz論壇Md5加密刪除
/// <summary>
/// Discuz論壇Md5加密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Md5(string str)
{
MD5 md5Hasher = MD5.Create();
string re="";
byte[] data = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(str+"%$#@!"));//yc
for (int i = 0; i < data.Length; i++){
re += data[i].ToString(
"x2");
}
return re;
}
#endregion
}
}

該文章在 2021/1/30 11:57:45 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業的專業生產管理軟件系統,系統成熟度和易用性得到了國內大量中小企業的青睞。
點晴PMS碼頭管理系統主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業務管理,結合碼頭的業務特點,圍繞調度、堆場作業而開發的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業的高效ERP管理信息系統。
點晴WMS倉儲管理系統提供了貨物產品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產管理,WMS管理系統,標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協同辦公管理系統。
Copyright 2010-2025 ClickSun All Rights Reserved