//'加密函数
private String Encrypt(String strText, String strEncrKey)
{
Byte[] byKey = {};
Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch(Exception ex)
{
return ex.Message;
}
}
解密算法private String Encrypt(String strText, String strEncrKey)
{
Byte[] byKey = {};
Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch(Exception ex)
{
return ex.Message;
}
}
1
2
3 //'解密函数
4 private String Decrypt(String strText, String sDecrKey)
5 {
6 Byte[] byKey = {};
7 Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
8 Byte[] inputByteArray = new byte[strText.Length];
9 try
10 {
11 byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
12 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
13 inputByteArray = Convert.FromBase64String(strText);
14 MemoryStream ms = new MemoryStream();
15 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
16 cs.Write(inputByteArray, 0, inputByteArray.Length);
17 cs.FlushFinalBlock();
18 System.Text.Encoding encoding = System.Text.Encoding.UTF8;
19 return encoding.GetString(ms.ToArray());
20 }
21 catch(Exception ex)
22 {
23 return ex.Message;
24 }
25 }
引自http://dotnet.aspx.cc/ShowDetail.aspx?id=7AE7D20A-A5DA-4303-AC2D-32046BE4D0862
3 //'解密函数
4 private String Decrypt(String strText, String sDecrKey)
5 {
6 Byte[] byKey = {};
7 Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
8 Byte[] inputByteArray = new byte[strText.Length];
9 try
10 {
11 byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
12 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
13 inputByteArray = Convert.FromBase64String(strText);
14 MemoryStream ms = new MemoryStream();
15 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
16 cs.Write(inputByteArray, 0, inputByteArray.Length);
17 cs.FlushFinalBlock();
18 System.Text.Encoding encoding = System.Text.Encoding.UTF8;
19 return encoding.GetString(ms.ToArray());
20 }
21 catch(Exception ex)
22 {
23 return ex.Message;
24 }
25 }
例子源码https://files.cnblogs.com/Inertial83/Cryptography.rar
浙公网安备 33010602011771号