//名称空间
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
//方法
//加密方法
public string Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte数组中
//原来使用的UTF8编码,我改成Unicode编码了,不行
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);
//建立加密对象的密钥和偏移量
//原文使用ASCIIEncoding.ASCII方法的GetBytes方法
//使得输入密码必须输入英文文本
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(),CryptoStreamMode.Write);
//Write the byte array into the crypto stream
//(It will end up in the memory stream)
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//Get the data back from the memory stream, and into a string
StringBuilder ret = new StringBuilder();
foreach(byte b in ms.ToArray())
{
//Format as hex
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
//解密方法
public string Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//Put the input string into the byte array
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for(int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
//建立加密对象的密钥和偏移量,此值重要,不能修改
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(),CryptoStreamMode.Write);
//Flush the data through the crypto stream into the memory stream
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//Get the decrypted data back from the memory stream
//建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
//方法
//加密方法
public string Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte数组中
//原来使用的UTF8编码,我改成Unicode编码了,不行
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);
//建立加密对象的密钥和偏移量
//原文使用ASCIIEncoding.ASCII方法的GetBytes方法
//使得输入密码必须输入英文文本
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(),CryptoStreamMode.Write);
//Write the byte array into the crypto stream
//(It will end up in the memory stream)
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//Get the data back from the memory stream, and into a string
StringBuilder ret = new StringBuilder();
foreach(byte b in ms.ToArray())
{
//Format as hex
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
//解密方法
public string Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//Put the input string into the byte array
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for(int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
//建立加密对象的密钥和偏移量,此值重要,不能修改
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(),CryptoStreamMode.Write);
//Flush the data through the crypto stream into the memory stream
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//Get the decrypted data back from the memory stream
//建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
来源:http://blog.csdn.net/keyu23/archive/2005/09/27/490577.aspx
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Web;
namespace Test.Com
{
/// <summary>
/// DESEncryptor 的摘要说明。
/// </summary>
public class DESEncryptor
{
私有成员
公共属性
构造函数
DES加密字符串
DES解密字符串
DES加密文件
DES解密文件
MD5
}
}
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Web;
namespace Test.Com
{
/// <summary>
/// DESEncryptor 的摘要说明。
/// </summary>
public class DESEncryptor
{
私有成员
公共属性
构造函数
DES加密字符串
DES解密字符串
DES加密文件
DES解密文件
MD5
}
}
来源: http://www.3s8.cn/develop/c1/200611/324.html
1using System;
2using System.Security;
3using System.Security.Cryptography;
4using System.Diagnostics;
5using System.Web;
6using System.Text;
7using System.ComponentModel;
8using System.Collections;
9
10namespace NSE.Data
11{
12 public class Encryption
13 {
14 /// <summary>
15 /// 转换string到Byte树组
16 /// </summary>
17 /// <param name="s">要转换的字符串</param>
18 /// <returns>转换的Byte数组</returns>
19 public Byte[] StringToByteArray(String s)
20 {
21 /*
22 Char[] ca = s.ToCharArray();
23 Byte[] ba = new Byte[ca.Length];
24 for(int i=0; i<ba.Length; i++) ba[i] = (Byte)ca[i];
25 return ba;*/
26
27 return Encoding.UTF8.GetBytes(s);
28 }
29
30 /// <summary>
31 /// 转换Byte数组到字符串
32 /// </summary>
33 /// <param name="a_arrByte">Byte数组</param>
34 /// <returns>字符串</returns>
35 public string ByteArrayToString(Byte[] a_arrByte)
36 {
37 /*
38 //char[] ca = new char[a_arrByte.Length] ;
39 for(int i = 0 ; i < a_arrByte.Length ; i ++)
40 {
41 result += (char)a_arrByte[i] ;
42 }*/
43
44 return Encoding.UTF8.GetString(a_arrByte);
45 }
46
47
48 /// <summary>
49 /// 3des加密字符串
50 /// </summary>
51 /// <param name="a_strString">要加密的字符串</param>
52 /// <param name="a_strKey">密钥</param>
53 /// <returns>加密后并经base64编码的字符串</returns>
54 /// <remarks>静态方法,采用默认ascii编码</remarks>
55 public string Encrypt3DES(string a_strString, string a_strKey)
56 {
57 TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
58 MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
59
60 DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(a_strKey));
61 DES.Mode = CipherMode.ECB;
62
63 ICryptoTransform DESEncrypt = DES.CreateEncryptor();
64
65 byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(a_strString);
66 return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
67 }//end method
68
69 /// <summary>
70 /// 3des加密字符串
71 /// </summary>
72 /// <param name="a_strString">要加密的字符串</param>
73 /// <param name="a_strKey">密钥</param>
74 /// <param name="encoding">编码方式</param>
75 /// <returns>加密后并经base63编码的字符串</returns>
76 /// <remarks>重载,指定编码方式</remarks>
77 public string Encrypt3DES(string a_strString, string a_strKey, Encoding encoding)
78 {
79 TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
80 MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
81
82 DES.Key = hashMD5.ComputeHash(encoding.GetBytes(a_strKey));
83 DES.Mode = CipherMode.ECB;
84
85 ICryptoTransform DESEncrypt = DES.CreateEncryptor();
86
87
88 byte[] Buffer = encoding.GetBytes(a_strString);
89 return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
90 }
91
92
93 /// <summary>
94 /// 3des解密字符串
95 /// </summary>
96 /// <param name="a_strString">要解密的字符串</param>
97 /// <param name="a_strKey">密钥</param>
98 /// <returns>解密后的字符串</returns>
99 /// <exception cref="">密钥错误</exception>
100 /// <remarks>静态方法,采用默认ascii编码</remarks>
101 public string Decrypt3DES(string a_strString, string a_strKey)
102 {
103 TripleDESCryptoServiceProvider DES = new
104 TripleDESCryptoServiceProvider();
105 MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
106
107 DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(a_strKey));
108 DES.Mode = CipherMode.ECB;
109
110 ICryptoTransform DESDecrypt = DES.CreateDecryptor();
111
112 string result = "";
113 try
114 {
115 byte[] Buffer = Convert.FromBase64String(a_strString);
116 result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
117 }
118 catch (Exception e)
119 {
120 //#if DEBUG
121 Console.WriteLine("错误:{0}", e);
122 //#endif//DEBUG
123 throw (new Exception("Invalid Key or input string is not a valid base64 string", e));
124 }
125
126 return result;
127 }//end method
128
129 /// <summary>
130 /// 3des解密字符串
131 /// </summary>
132 /// <param name="a_strString">要解密的字符串</param>
133 /// <param name="a_strKey">密钥</param>
134 /// <param name="encoding">编码方式</param>
135 /// <returns>解密后的字符串</returns>
136 /// <exception cref="">密钥错误</exception>
137 /// <remarks>静态方法,指定编码方式</remarks>
138 public string Decrypt3DES(string a_strString, string a_strKey, Encoding encoding)
139 {
140 TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
141 MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
142
143 DES.Key = hashMD5.ComputeHash(encoding.GetBytes(a_strKey));
144 DES.Mode = CipherMode.ECB;
145
146 ICryptoTransform DESDecrypt = DES.CreateDecryptor();
147
148 string result = "";
149 try
150 {
151 byte[] Buffer = Convert.FromBase64String(a_strString);
152 result = encoding.GetString(DESDecrypt.TransformFinalBlock
153 (Buffer, 0, Buffer.Length));
154 }
155 catch (Exception e)
156 {
157 //#if DEBUG
158 Console.WriteLine("错误:{0}", e);
159 //#endif//DEBUG
160 throw (new Exception("Invalid Key or input string is not a valid base64 string", e));
161 }
162
163 return result;
164 }//end method
165
166 }
167}
168
2using System.Security;
3using System.Security.Cryptography;
4using System.Diagnostics;
5using System.Web;
6using System.Text;
7using System.ComponentModel;
8using System.Collections;
9
10namespace NSE.Data
11{
12 public class Encryption
13 {
14 /// <summary>
15 /// 转换string到Byte树组
16 /// </summary>
17 /// <param name="s">要转换的字符串</param>
18 /// <returns>转换的Byte数组</returns>
19 public Byte[] StringToByteArray(String s)
20 {
21 /*
22 Char[] ca = s.ToCharArray();
23 Byte[] ba = new Byte[ca.Length];
24 for(int i=0; i<ba.Length; i++) ba[i] = (Byte)ca[i];
25 return ba;*/
26
27 return Encoding.UTF8.GetBytes(s);
28 }
29
30 /// <summary>
31 /// 转换Byte数组到字符串
32 /// </summary>
33 /// <param name="a_arrByte">Byte数组</param>
34 /// <returns>字符串</returns>
35 public string ByteArrayToString(Byte[] a_arrByte)
36 {
37 /*
38 //char[] ca = new char[a_arrByte.Length] ;
39 for(int i = 0 ; i < a_arrByte.Length ; i ++)
40 {
41 result += (char)a_arrByte[i] ;
42 }*/
43
44 return Encoding.UTF8.GetString(a_arrByte);
45 }
46
47
48 /// <summary>
49 /// 3des加密字符串
50 /// </summary>
51 /// <param name="a_strString">要加密的字符串</param>
52 /// <param name="a_strKey">密钥</param>
53 /// <returns>加密后并经base64编码的字符串</returns>
54 /// <remarks>静态方法,采用默认ascii编码</remarks>
55 public string Encrypt3DES(string a_strString, string a_strKey)
56 {
57 TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
58 MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
59
60 DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(a_strKey));
61 DES.Mode = CipherMode.ECB;
62
63 ICryptoTransform DESEncrypt = DES.CreateEncryptor();
64
65 byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(a_strString);
66 return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
67 }//end method
68
69 /// <summary>
70 /// 3des加密字符串
71 /// </summary>
72 /// <param name="a_strString">要加密的字符串</param>
73 /// <param name="a_strKey">密钥</param>
74 /// <param name="encoding">编码方式</param>
75 /// <returns>加密后并经base63编码的字符串</returns>
76 /// <remarks>重载,指定编码方式</remarks>
77 public string Encrypt3DES(string a_strString, string a_strKey, Encoding encoding)
78 {
79 TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
80 MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
81
82 DES.Key = hashMD5.ComputeHash(encoding.GetBytes(a_strKey));
83 DES.Mode = CipherMode.ECB;
84
85 ICryptoTransform DESEncrypt = DES.CreateEncryptor();
86
87
88 byte[] Buffer = encoding.GetBytes(a_strString);
89 return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
90 }
91
92
93 /// <summary>
94 /// 3des解密字符串
95 /// </summary>
96 /// <param name="a_strString">要解密的字符串</param>
97 /// <param name="a_strKey">密钥</param>
98 /// <returns>解密后的字符串</returns>
99 /// <exception cref="">密钥错误</exception>
100 /// <remarks>静态方法,采用默认ascii编码</remarks>
101 public string Decrypt3DES(string a_strString, string a_strKey)
102 {
103 TripleDESCryptoServiceProvider DES = new
104 TripleDESCryptoServiceProvider();
105 MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
106
107 DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(a_strKey));
108 DES.Mode = CipherMode.ECB;
109
110 ICryptoTransform DESDecrypt = DES.CreateDecryptor();
111
112 string result = "";
113 try
114 {
115 byte[] Buffer = Convert.FromBase64String(a_strString);
116 result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
117 }
118 catch (Exception e)
119 {
120 //#if DEBUG
121 Console.WriteLine("错误:{0}", e);
122 //#endif//DEBUG
123 throw (new Exception("Invalid Key or input string is not a valid base64 string", e));
124 }
125
126 return result;
127 }//end method
128
129 /// <summary>
130 /// 3des解密字符串
131 /// </summary>
132 /// <param name="a_strString">要解密的字符串</param>
133 /// <param name="a_strKey">密钥</param>
134 /// <param name="encoding">编码方式</param>
135 /// <returns>解密后的字符串</returns>
136 /// <exception cref="">密钥错误</exception>
137 /// <remarks>静态方法,指定编码方式</remarks>
138 public string Decrypt3DES(string a_strString, string a_strKey, Encoding encoding)
139 {
140 TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
141 MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
142
143 DES.Key = hashMD5.ComputeHash(encoding.GetBytes(a_strKey));
144 DES.Mode = CipherMode.ECB;
145
146 ICryptoTransform DESDecrypt = DES.CreateDecryptor();
147
148 string result = "";
149 try
150 {
151 byte[] Buffer = Convert.FromBase64String(a_strString);
152 result = encoding.GetString(DESDecrypt.TransformFinalBlock
153 (Buffer, 0, Buffer.Length));
154 }
155 catch (Exception e)
156 {
157 //#if DEBUG
158 Console.WriteLine("错误:{0}", e);
159 //#endif//DEBUG
160 throw (new Exception("Invalid Key or input string is not a valid base64 string", e));
161 }
162
163 return result;
164 }//end method
165
166 }
167}
168