DES加密解密类

简介: 1 /// 2 /// DES加密/解密类。 3 /// 4 public class HXEncrypt 5 { 6 public HXEncrypt() 7 { 8 ...
  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     }
View Code

 

目录
相关文章
|
1月前
|
Java 数据安全/隐私保护
des加密+base64编码,base64解码+des解密
des加密+base64编码,base64解码+des解密
23 0
|
5月前
|
算法 搜索推荐 Java
DES - 对称加密算法简要介绍与JAVA实现
DES - 对称加密算法简要介绍与JAVA实现
54 2
|
4月前
|
算法 安全 数据安全/隐私保护
C/C++学习 -- 分组加密算法(DES算法)
C/C++学习 -- 分组加密算法(DES算法)
34 0
|
4月前
|
JavaScript 前端开发 算法
JavaScript学习 -- 对称加密算法DES
JavaScript学习 -- 对称加密算法DES
20 0
|
28天前
|
算法 安全 程序员
详解 DES加密技术 | 凯撒密码 | 栅栏密码
详解 DES加密技术 | 凯撒密码 | 栅栏密码
112 0
|
7月前
|
算法 安全 数据安全/隐私保护
DES加密算法
DES加密算法
23 0
|
7月前
|
算法 安全 数据安全/隐私保护
C/C++学习 -- 分组加密算法(DES算法)
C/C++学习 -- 分组加密算法(DES算法)
139 0
|
7月前
|
JavaScript 前端开发 算法
JavaScript学习 -- 对称加密算法DES
JavaScript学习 -- 对称加密算法DES
23 0
|
7月前
|
算法 安全 调度
DES加密算法解析
DES加密算法解析
118 0
|
7月前
|
数据采集 JavaScript 前端开发
“探秘JS加密算法:MD5、Base64、DES/AES、RSA你都知道吗?”
“探秘JS加密算法:MD5、Base64、DES/AES、RSA你都知道吗?”
112 0