1 /// <summary> 2 /// DES加密/解密类。 3 /// </summary> 4 public class HXEncrypt 5 { 6 public HXEncrypt() 7 { 8 } 9 10 #region DES加密 11 12 13 #region ========加密======== 14 15 /// <summary> 16 /// 加密 17 /// </summary> 18 /// <param name="Text"></param> 19 /// <returns></returns> 20 public static string Encrypt(string Text) 21 { 22 return Encrypt(Text, "bone"); 23 } 24 /// <summary> 25 /// 加密数据 26 /// </summary> 27 /// <param name="Text"></param> 28 /// <param name="sKey"></param> 29 /// <returns></returns> 30 public static string Encrypt(string Text, string sKey) 31 { 32 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 33 byte[] inputByteArray; 34 inputByteArray = Encoding.Default.GetBytes(Text); 35 des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 36 des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 37 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 38 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); 39 cs.Write(inputByteArray, 0, inputByteArray.Length); 40 cs.FlushFinalBlock(); 41 StringBuilder ret = new StringBuilder(); 42 foreach (byte b in ms.ToArray()) 43 { 44 ret.AppendFormat("{0:X2}", b); 45 } 46 return ret.ToString(); 47 } 48 49 #endregion 50 51 #region ========解密======== 52 53 54 /// <summary> 55 /// 解密 56 /// </summary> 57 /// <param name="Text"></param> 58 /// <returns></returns> 59 public static string Decrypt(string Text) 60 { 61 return Decrypt(Text, "bone"); 62 } 63 /// <summary> 64 /// 解密数据 65 /// </summary> 66 /// <param name="Text"></param> 67 /// <param name="sKey"></param> 68 /// <returns></returns> 69 public static string Decrypt(string Text, string sKey) 70 { 71 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 72 int len; 73 len = Text.Length / 2; 74 byte[] inputByteArray = new byte[len]; 75 int x, i; 76 for (x = 0; x < len; x++) 77 { 78 i = Convert.ToInt32(Text.Substring(x * 2, 2), 16); 79 inputByteArray[x] = (byte)i; 80 } 81 des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 82 des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 83 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 84 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); 85 cs.Write(inputByteArray, 0, inputByteArray.Length); 86 cs.FlushFinalBlock(); 87 return Encoding.Default.GetString(ms.ToArray()); 88 } 89 90 #endregion 91 92 #endregion 93 94 #region RSA非对称加密 95 96 97 /// <summary> 98 /// 生成公钥,私钥对 99 /// </summary> 100 public static string[] GenerateKeys() 101 { 102 string[] sKeys = new String[2]; 103 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 104 sKeys[0] = rsa.ToXmlString(true);//私钥 105 sKeys[1] = rsa.ToXmlString(false);//公钥 106 return sKeys; 107 } 108 109 /// <summary> 110 /// RSA 加密 111 /// </summary> 112 /// <param name="sSource" >明文</param> 113 /// <param name="sPublicKey" >公钥</param> 114 public static string EncryptString(string sSource, string sPublicKey) 115 { 116 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 117 string plaintext = sSource; 118 rsa.FromXmlString(sPublicKey); 119 byte[] cipherbytes; 120 byte[] byteEn = rsa.Encrypt(Encoding.UTF8.GetBytes("a"), false); 121 cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(plaintext), false); 122 123 StringBuilder sbString = new StringBuilder(); 124 for (int i = 0; i < cipherbytes.Length; i++) 125 { 126 sbString.Append(cipherbytes[i] + ","); 127 } 128 return sbString.ToString(); 129 } 130 131 132 /// <summary> 133 /// RSA 解密 134 /// </summary> 135 /// <param name="sSource">密文</param> 136 /// <param name="sPrivateKey">私钥</param> 137 public static string DecryptString(String sSource, string sPrivateKey) 138 { 139 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 140 rsa.FromXmlString(sPrivateKey); 141 byte[] byteEn = rsa.Encrypt(Encoding.UTF8.GetBytes("a"), false); 142 string[] sBytes = sSource.Split(','); 143 144 for (int j = 0; j < sBytes.Length; j++) 145 { 146 if (sBytes[j] != "") 147 { 148 byteEn[j] = Byte.Parse(sBytes[j]); 149 } 150 } 151 byte[] plaintbytes = rsa.Decrypt(byteEn, false); 152 return Encoding.UTF8.GetString(plaintbytes); 153 } 154 155 #endregion 156 }