SoftSecurity.cs
3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace HslCommunication.BasicFramework
{
/**********************************************************************************
*
* Create Date:2017-05-03 16:52:48
* Author By Richard.Hu
*
*
**********************************************************************************/
/// <summary>
/// 字符串加密解密相关的自定义类
/// </summary>
public static class SoftSecurity
{
/// <summary>
/// 加密数据,采用对称加密的方式
/// </summary>
/// <param name="pToEncrypt">待加密的数据</param>
/// <returns>加密后的数据</returns>
internal static string MD5Encrypt(string pToEncrypt)
{
return MD5Encrypt(pToEncrypt, "zxcvBNMM");
}
/// <summary>
/// 加密数据,采用对称加密的方式
/// </summary>
/// <param name="pToEncrypt">待加密的数据</param>
/// <param name="Password">密钥,长度为8,英文或数字</param>
/// <returns>加密后的数据</returns>
public static string MD5Encrypt(string pToEncrypt, string Password)
{
string aisdhaisdhwdb = Password;
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = Encoding.ASCII.GetBytes(aisdhaisdhwdb);
des.IV = Encoding.ASCII.GetBytes(aisdhaisdhwdb);
MemoryStream ms = new 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);
}
ret.ToString();
return ret.ToString();
}
/// <summary>
/// 解密过程,使用的是对称的加密
/// </summary>
/// <param name="pToDecrypt">等待解密的字符</param>
/// <returns>返回原密码,如果解密失败,返回‘解密失败’</returns>
internal static string MD5Decrypt(string pToDecrypt)
{
return MD5Decrypt(pToDecrypt, "zxcvBNMM");
}
/// <summary>
/// 解密过程,使用的是对称的加密
/// </summary>
/// <param name="pToDecrypt">等待解密的字符</param>
/// <param name="password">密钥,长度为8,英文或数字</param>
/// <returns>返回原密码,如果解密失败,返回‘解密失败’</returns>
public static string MD5Decrypt(string pToDecrypt, string password)
{
if (pToDecrypt == "") return pToDecrypt;
string zxcawrafdgegasd = password;
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
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 = Encoding.ASCII.GetBytes(zxcawrafdgegasd);
des.IV = Encoding.ASCII.GetBytes(zxcawrafdgegasd);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
cs.Dispose();
return Encoding.Default.GetString(ms.ToArray());
}
}
}