123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Security.Cryptography;
- using System.IO;
- namespace ETD.Frame
- {
- public class EncodeEncrypt
- {
- #region ========加密一========
- //默认密钥向量
- //private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
- private static byte[] Keys = { 0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F };
- /// <summary>
- /// 加密的key
- /// </summary>
- private static string encryptKey = "36CBB25BB63A43E9B7399EAE796F41B9";
- /// <summary>
- /// 使用AES方式进行加密
- /// </summary>
- /// <param name="encryptString">需要加密的字符串</param>
- /// <param name="encryptKey">进行加密的Key</param>
- /// <returns></returns>
- public static string Encode(string encryptString)
- {
- encryptKey = Utils.GetSubString(encryptKey, 32, "");
- encryptKey = encryptKey.PadRight(32, ' ');
- RijndaelManaged rijndaelProvider = new RijndaelManaged();
- rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32));
- rijndaelProvider.IV = Keys;
- ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor();
- byte[] inputData = Encoding.UTF8.GetBytes(encryptString);
- byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length);
- return Convert.ToBase64String(encryptedData);
- }
- #endregion
- #region ========加密二========
- //默认密钥向量
- private static byte[] DESKeys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
- /// <summary>
- /// 加密的key
- /// </summary>
- private static string DESencryptKey = "!@#$%^&*";
- /// <summary>
- /// DES加密字符串
- /// </summary>
- /// <param name="encryptString">待加密的字符串</param>
- /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
- public static string DESEncode(string encryptString)
- {
- DESencryptKey = Utils.GetSubString(encryptKey, 8, "");
- DESencryptKey = DESencryptKey.PadRight(8, ' ');
- byte[] rgbKey = Encoding.UTF8.GetBytes(DESencryptKey.Substring(0, 8));
- byte[] rgbIV = DESKeys;
- 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());
- }
- #endregion
- #region ========加密三========
- /// <summary>
- /// 加密
- /// </summary>
- /// <param name="Text"></param>
- /// <returns></returns>
- public static string Encrypt(string Text)
- {
- return Encrypt(Text, "jijianguo");
- }
- /// <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
- }
- }
|