.net常用加密解密方法

http://www.cnblogs.com/qinweilong/archive/2010/06/30/1768535.html

複製代碼
private byte[] _power; /// <summary> /// 用戶權限 /// </summary> public byte[] Power { set { _power = value; } get { return _power; } } sql: Power binary 4000 解密: Encoding.Unicode.GetString(model.Power); 加密: byte[] Power = new UnicodeEncoding().GetBytes(this.HidPowers.Value); model.Power = Power;
複製代碼

 

複製代碼
using System; using System.Security.Cryptography; using System.Text; namespace BeidouWY.Common.DEncrypt { /// <summary> /// Encrypt 的摘要說明。 /// LiTianPing /// </summary> public class DEncrypt { /// <summary> /// 構造方法 /// </summary> public DEncrypt() { } #region 使用 缺省密鑰字符串 加密/解密string /// <summary> /// 使用缺省密鑰字符串加密string /// </summary> /// <param name="original">明文</param> /// <returns>密文</returns> public static string Encrypt(string original) { return Encrypt(original,"MATICSOFT"); } /// <summary> /// 使用缺省密鑰字符串解密string /// </summary> /// <param name="original">密文</param> /// <returns>明文</returns> public static string Decrypt(string original) { return Decrypt(original,"MATICSOFT",System.Text.Encoding.Default); } #endregion #region 使用 給定密鑰字符串 加密/解密string /// <summary> /// 使用給定密鑰字符串加密string /// </summary> /// <param name="original">原始文字</param> /// <param name="key">密鑰</param> /// <param name="encoding">字符編碼方案</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)); } #endregion #region 使用 缺省密鑰字符串 加密/解密/byte[] /// <summary> /// 使用缺省密鑰字符串解密byte[] /// </summary> /// <param name="encrypted">密文</param> /// <param name="key">密鑰</param> /// <returns>明文</returns> public static byte[] Decrypt(byte[] encrypted) { byte[] key = System.Text.Encoding.Default.GetBytes("MATICSOFT"); return Decrypt(encrypted,key); } /// <summary> /// 使用缺省密鑰字符串加密 /// </summary> /// <param name="original">原始數據</param> /// <param name="key">密鑰</param> /// <returns>密文</returns> public static byte[] Encrypt(byte[] original) { byte[] key = System.Text.Encoding.Default.GetBytes("MATICSOFT"); return Encrypt(original,key); } #endregion #region 使用 給定密鑰 加密/解密/byte[] /// <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); } #endregion } }
複製代碼

 

複製代碼
using System; using System.Security.Cryptography; using System.Text; namespace BeidouWY.Common.DEncrypt { /// <summary> /// DES加密/解密類。 /// LiTianPing /// </summary> public class DESEncrypt { public DESEncrypt() { } #region ========加密======== /// <summary> /// 加密 /// </summary> /// <param name="Text"></param> /// <returns></returns> public static string Encrypt(string Text) { return Encrypt(Text,"MATICSOFT"); } /// <summary> /// 加密數據 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> public static string Encrypt(string Text,string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray; inputByteArray=Encoding.Default.GetBytes(Text); des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); System.IO.MemoryStream ms=new System.IO.MemoryStream(); CryptoStream cs=new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write); cs.Write(inputByteArray,0,inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret=new StringBuilder(); foreach( byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}",b); } return ret.ToString(); } #endregion #region ========解密======== /// <summary> /// 解密 /// </summary> /// <param name="Text"></param> /// <returns></returns> public static string Decrypt(string Text) { return Decrypt(Text,"MATICSOFT"); } /// <summary> /// 解密數據 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> public static string Decrypt(string Text,string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); int len; len=Text.Length/2; byte[] inputByteArray = new byte[len]; int x,i; for(x=0;x<len;x++) { i = Convert.ToInt32(Text.Substring(x * 2, 2), 16); inputByteArray[x]=(byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); System.IO.MemoryStream ms=new System.IO.MemoryStream(); CryptoStream cs=new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write); cs.Write(inputByteArray,0,inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.Default.GetString(ms.ToArray()); } #endregion } }
複製代碼

 

複製代碼
using System; using System.Text; using System.Security.Cryptography; namespace BeidouWY.Common.DEncrypt { /// <summary> /// 得到隨機安全碼(哈希加密)。 /// </summary> public class HashEncode { public HashEncode() { // // TODO: 在此處添加構造函數邏輯 // } /// <summary> /// 得到隨機哈希加密字符串 /// </summary> /// <returns></returns> public static string GetSecurity() { string Security = HashEncoding(GetRandomValue()); return Security; } /// <summary> /// 得到一個隨機數值 /// </summary> /// <returns></returns> public static string GetRandomValue() { Random Seed = new Random(); string RandomVaule = Seed.Next(1, int.MaxValue).ToString(); return RandomVaule; } /// <summary> /// 哈希加密一個字符串 /// </summary> /// <param name="Security"></param> /// <returns></returns> public static string HashEncoding(string Security) { byte[] Value; UnicodeEncoding Code = new UnicodeEncoding(); byte[] Message = Code.GetBytes(Security); SHA512Managed Arithmetic = new SHA512Managed(); Value = Arithmetic.ComputeHash(Message); Security = ""; foreach(byte o in Value) { Security += (int) o + "O"; } return Security; } } }
複製代碼

 

複製代碼
using System; using System.Security; using System.Security.Cryptography; using System.Text; namespace BeidouWY.Common.DEncrypt { /// <summary> /// MD5Helper 的摘要說明。 /// </summary> public class MD5Helper { /// <summary> /// 得到一個加密的字符串 /// </summary> /// <param name="strIn">原始字符串</param> /// <returns>加密後字符串</returns> public static string GetMD5String(string strIn) { byte[] b=Encoding.Default.GetBytes(strIn); b=new MD5CryptoServiceProvider().ComputeHash(b); string strOut=""; for(int i=0;i<b.Length;i++) { strOut+=b[i].ToString("x").PadLeft(2,'2'); } return strOut; } } }
複製代碼

 

複製代碼
using System; using System.Text; using System.Security.Cryptography; namespace BeidouWY.Common.DEncrypt { /// <summary> /// RSA加密解密及RSA簽名和驗證 /// </summary> public class RSACryption { public RSACryption() { } #region RSA 加密解密 #region RSA 的密鑰產生 /// <summary> /// RSA 的密鑰產生 產生私鑰 和公鑰 /// </summary> /// <param name="xmlKeys"></param> /// <param name="xmlPublicKey"></param> public void RSAKey(out string xmlKeys,out string xmlPublicKey) { System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(); xmlKeys=rsa.ToXmlString(true); xmlPublicKey = rsa.ToXmlString(false); } #endregion #region RSA的加密函數 //############################################################################## //RSA 方式加密 //說明KEY必須是XML的行式,返回的是字符串 //在有一點需要說明!!該加密方式有 長度 限制的!! //############################################################################## //RSA的加密函數 string public string RSAEncrypt(string xmlPublicKey,string m_strEncryptString ) { byte[] PlainTextBArray; byte[] CypherTextBArray; string Result; RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(); rsa.FromXmlString(xmlPublicKey); PlainTextBArray = (new UnicodeEncoding()).GetBytes(m_strEncryptString); CypherTextBArray = rsa.Encrypt(PlainTextBArray, false); Result=Convert.ToBase64String(CypherTextBArray); return Result; } //RSA的加密函數 byte[] public string RSAEncrypt(string xmlPublicKey,byte[] EncryptString ) { byte[] CypherTextBArray; string Result; RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(); rsa.FromXmlString(xmlPublicKey); CypherTextBArray = rsa.Encrypt(EncryptString, false); Result=Convert.ToBase64String(CypherTextBArray); return Result; } #endregion #region RSA的解密函數 //RSA的解密函數 string public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString ) { byte[] PlainTextBArray; byte[] DypherTextBArray; string Result; System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(); rsa.FromXmlString(xmlPrivateKey); PlainTextBArray =Convert.FromBase64String(m_strDecryptString); DypherTextBArray=rsa.Decrypt(PlainTextBArray, false); Result=(new UnicodeEncoding()).GetString(DypherTextBArray); return Result; } //RSA的解密函數 byte public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString ) { byte[] DypherTextBArray; string Result; System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(); rsa.FromXmlString(xmlPrivateKey); DypherTextBArray=rsa.Decrypt(DecryptString, false); Result=(new UnicodeEncoding()).GetString(DypherTextBArray); return Result; } #endregion #endregion #region RSA數字簽名 #region 獲取Hash描述表 //獲取Hash描述表 public bool GetHash(string m_strSource, ref byte[] HashData) { //從字符串中取得Hash描述 byte[] Buffer; System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5"); Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource); HashData = MD5.ComputeHash(Buffer); return true; } //獲取Hash描述表 public bool GetHash(string m_strSource, ref string strHashData) { //從字符串中取得Hash描述 byte[] Buffer; byte[] HashData; System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5"); Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource); HashData = MD5.ComputeHash(Buffer); strHashData = Convert.ToBase64String(HashData); return true; } //獲取Hash描述表 public bool GetHash(System.IO.FileStream objFile, ref byte[] HashData) { //從文件中取得Hash描述 System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5"); HashData = MD5.ComputeHash(objFile); objFile.Close(); return true; } //獲取Hash描述表 public bool GetHash(System.IO.FileStream objFile, ref string strHashData) { //從文件中取得Hash描述 byte[] HashData; System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5"); HashData = MD5.ComputeHash(objFile); objFile.Close(); strHashData = Convert.ToBase64String(HashData); return true; } #endregion #region RSA簽名 //RSA簽名 public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref byte[] EncryptedSignatureData) { System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPrivate); System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA); //設置簽名的算法爲MD5 RSAFormatter.SetHashAlgorithm("MD5"); //執行簽名 EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature); return true; } //RSA簽名 public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref string m_strEncryptedSignatureData) { byte[] EncryptedSignatureData; System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPrivate); System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA); //設置簽名的算法爲MD5 RSAFormatter.SetHashAlgorithm("MD5"); //執行簽名 EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature); m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData); return true; } //RSA簽名 public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref byte[] EncryptedSignatureData) { byte[] HashbyteSignature; HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature); System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPrivate); System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA); //設置簽名的算法爲MD5 RSAFormatter.SetHashAlgorithm("MD5"); //執行簽名 EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature); return true; } //RSA簽名 public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref string m_strEncryptedSignatureData) { byte[] HashbyteSignature; byte[] EncryptedSignatureData; HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature); System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPrivate); System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA); //設置簽名的算法爲MD5 RSAFormatter.SetHashAlgorithm("MD5"); //執行簽名 EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature); m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData); return true; } #endregion #region RSA 簽名驗證 public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, byte[] DeformatterData) { System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPublic); System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA); //指定解密的時候HASH算法爲MD5 RSADeformatter.SetHashAlgorithm("MD5"); if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData)) { return true; } else { return false; } } public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, byte[] DeformatterData) { byte[] HashbyteDeformatter; HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter); System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPublic); System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA); //指定解密的時候HASH算法爲MD5 RSADeformatter.SetHashAlgorithm("MD5"); if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData)) { return true; } else { return false; } } public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, string p_strDeformatterData) { byte[] DeformatterData; System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPublic); System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA); //指定解密的時候HASH算法爲MD5 RSADeformatter.SetHashAlgorithm("MD5"); DeformatterData =Convert.FromBase64String(p_strDeformatterData); if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData)) { return true; } else { return false; } } public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, string p_strDeformatterData) { byte[] DeformatterData; byte[] HashbyteDeformatter; HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter); System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPublic); System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA); //指定解密的時候HASH算法爲MD5 RSADeformatter.SetHashAlgorithm("MD5"); DeformatterData =Convert.FromBase64String(p_strDeformatterData); if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData)) { return true; } else { return false; } } #endregion #endregion } }
複製代碼

發佈了136 篇原創文章 · 獲贊 26 · 訪問量 69萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章