yl
2025-03-19 14ebf45749361ad5043ff3f1562cd66b7b82a50d
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
 
namespace VueWebCoreApi.Tools
{
    public class Encrypt
    {
        private static RijndaelManaged RMCrypto = null;
 
        private Hashtable keyList = new Hashtable();
 
        public static string EncryptStr(string str, string strKey, string strIV)
        {
            CryptoStream cryptoStream = null;
            MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
            string result;
            try
            {
                Encrypt.RMCrypto = new RijndaelManaged();
                if (str == null || str.Trim().Length == 0)
                {
                    result = "";
                }
                else
                {
                    byte[] rgbKey = mD5CryptoServiceProvider.ComputeHash(Encoding.ASCII.GetBytes(strKey));
                    byte[] rgbIV = mD5CryptoServiceProvider.ComputeHash(Encoding.ASCII.GetBytes(strIV));
                    MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(str));
                    cryptoStream = new CryptoStream(memoryStream, Encrypt.RMCrypto.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                    result = Convert.ToBase64String(memoryStream.ToArray());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (null != cryptoStream)
                {
                    cryptoStream = null;
                }
                if (null != Encrypt.RMCrypto)
                {
                    Encrypt.RMCrypto.Clear();
                    Encrypt.RMCrypto = null;
                }
            }
            return result;
        }
 
        public static string DecryptStr(string str, string strKey, string strIV)
        {
            string result;
            if (str == null || str.Trim().Length == 0)
            {
                result = "";
            }
            else
            {
                CryptoStream cryptoStream = null;
                MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
                try
                {
                    Encrypt.RMCrypto = new RijndaelManaged();
                    byte[] buffer = Convert.FromBase64String(str);
                    byte[] rgbKey = mD5CryptoServiceProvider.ComputeHash(Encoding.ASCII.GetBytes(strKey));
                    byte[] rgbIV = mD5CryptoServiceProvider.ComputeHash(Encoding.ASCII.GetBytes(strIV));
                    MemoryStream memoryStream = new MemoryStream(buffer);
                    cryptoStream = new CryptoStream(memoryStream, Encrypt.RMCrypto.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Read);
                    result = Encoding.UTF8.GetString(memoryStream.ToArray());
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (null != cryptoStream)
                    {
                        cryptoStream = null;
                    }
                    if (null != Encrypt.RMCrypto)
                    {
                        Encrypt.RMCrypto.Clear();
                        Encrypt.RMCrypto = null;
                    }
                }
            }
            return result;
        }
 
        public void Add(string key, string value)
        {
            if (this.keyList.ContainsKey(key))
            {
                this.keyList[key] = value;
            }
            else
            {
                this.keyList.Add(key, value);
            }
        }
 
        public string GetEncryptXml(string name, string key, string iv)
        {
            XmlDocument xmlDocument = new XmlDocument();
            XmlElement xmlElement = xmlDocument.CreateElement(name);
            foreach (object current in this.keyList.Keys)
            {
                XmlElement xmlElement2 = xmlDocument.CreateElement(Convert.ToString(current));
                xmlElement2.InnerText = Convert.ToString(this.keyList[current]);
                xmlElement.AppendChild(xmlElement2);
            }
            xmlDocument.AppendChild(xmlElement);
            return Encrypt.EncryptStr(xmlDocument.OuterXml, key, iv);
        }
 
        public Hashtable GetKeyList(string name, string encryptStr, string key, string iv)
        {
            string xml = Encrypt.DecryptStr(encryptStr, key, iv);
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(xml);
            this.keyList.Clear();
            XmlNode xmlNode = xmlDocument.SelectSingleNode(name);
            if (null != xmlNode)
            {
                foreach (XmlNode xmlNode2 in xmlNode.ChildNodes)
                {
                    this.keyList.Add(xmlNode2.Name, xmlNode2.InnerText);
                }
            }
            return this.keyList;
        }
    }
}