moli
2022-05-01 68a321cfa55422bb7f6f5ad7a01366e69c519d1e
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Test.MethodClass.Coding
{
    public class MyCoding
    {
        public static string Encoding(string str) 
        {
            var res = Encoding(str, "asdbvsosdf");
            return res;
        }
 
        public static string Decoding(string str) 
        {
            var res = Decoding(str, "asdbvsosdf");
            return res;
        }
 
 
        private static string Encoding(string str,string key, CodingType type = CodingType.Standard,int offset = 0) 
        {
            string res = "";
            offset += 21;
            int countLeng = 1024 + 32;
            str = StringToBase64(str);
            byte[] b = System.Text.Encoding.UTF8.GetBytes(str);
 
            var newKey = MD5(key, true, 32);
            int index = b.Length % (1024) == 0 ? b.Length / (1024) : b.Length / (1024) + 1;
            byte[][] bs = new byte[index][];
            while (index > 0) 
            {
                index--;
 
                int positionKey = (((index + offset) * index) + offset);
                string _key = MD5(newKey + positionKey.ToString(), true, 32);
                //_key = _key.Substring(0,(b.Length % 32));
                byte[] _keyb = System.Text.Encoding.UTF8.GetBytes(_key);
                byte[] _newb = new byte[_keyb.Length + countLeng]; //新字符长度
 
                if (index == (bs.Length-1))
                {
                    _newb = new byte[_keyb.Length + b.Length - (index * 1024)]; //新字符长度
                }
                else { }
 
                int _newbIndex = 0;
                int _keybIndex = 1;
                int _bIndex = 0;
                if (_newb.Length > 32 * 2)
                {   
                    //密文长度大于混淆参数长度
                    while (_newbIndex < _newb.Length)
                    {
 
                        if (_newbIndex == (_newb.Length / _keyb.Length) * _keybIndex) 
                        {
                            //如果当前位置等于混肴参数的位置,则插入混肴参数
                            _newb[_newbIndex] = _keyb[_keybIndex - 1];
                            _keybIndex++;
                        }
                        else 
                        {
                            //否则插入标准文体
                            _newb[_newbIndex] = b[(index * 1024) + _bIndex];
                            _bIndex++;
                        }
                        _newbIndex++;
                    }
                }
                else 
                {
                    //密文长度小于混淆参数长度
                    while (_newbIndex < _newb.Length)
                    {
 
                        if (_newbIndex == (_newb.Length / (_newb.Length - _keyb.Length) * _bIndex))
                        {
                            //如果当前位置等于密文的位置,则插入密文
                            _newb[_newbIndex] = b[(index * 1024) + _bIndex];
                            _bIndex++;
                        }
                        else 
                        {
                            //否则插入混淆参数
                            _newb[_newbIndex] = _keyb[_keybIndex - 1];
                            if (_keybIndex < _keyb.Length)
                            {
                                _keybIndex++;
                            }
                        }
                        _newbIndex++;
                    }
                }
                bs[index] = _newb;
            }
            int resByteLeng = 0;
 
            foreach (var a in bs) 
            {
                resByteLeng += a.Length;
            }
 
            byte[] allByte = new byte[resByteLeng];
            int allByteIndex = 0;
            foreach (var a in bs) 
            {
                foreach (var c in a) 
                {
                    allByte[allByteIndex] = c;
                    allByteIndex++;
                }
            }
            res = System.Text.Encoding.UTF8.GetString(allByte);
 
            return res;
        }
 
        private static string Decoding(string str, string key, CodingType type = CodingType.Standard, int offset = 0) 
        {
            string res = "";
            offset += 21;
            int countLeng = 1024 + 32;
            byte[] strb = System.Text.Encoding.UTF8.GetBytes(str);
            int leng = strb.Length % countLeng == 0 ? (strb.Length / countLeng) : ((strb.Length / countLeng) + 1);
            byte[][] bs = new byte[leng][];
 
            var newKey = MD5(key, true, 32);
 
            for (int i = 0; i < leng; i++) 
            {
                int positionKey = (((i + offset) * i) + offset);
                string _key = MD5(newKey + positionKey.ToString(), true, 32);
                byte[] _keyb = System.Text.Encoding.UTF8.GetBytes(_key);
 
 
                byte[] tempb = new byte[1024];
                int tempIndex = 0;
                int tempKeyIndex = 1;
 
                if (i == leng - 1)
                {
                    //最后一个
                    var lastLeng = strb.Length - (i * countLeng);
                    tempb = new byte[lastLeng - _keyb.Length];
                    if (lastLeng > 32 * 2)
                    {
                        while (tempIndex < tempb.Length)
                        {
                            if (tempIndex == ((lastLeng / tempb.Length) * tempIndex)+ (tempKeyIndex - 1))
                            {
                                tempKeyIndex++;
                            }
                            else {
                                int strbIndex = ((countLeng * i) + tempIndex + (tempKeyIndex - 1));
                                tempb[tempIndex] = strb[strbIndex];
                                tempIndex++;
                            }
                        }
                    }
                    else 
                    {
                        var __index = 0;
                        while (__index < lastLeng)
                        {
 
                            if (__index == ((lastLeng / tempb.Length) * tempIndex))
                            {
                                int strbIndex = ((countLeng * i) + tempIndex + (tempKeyIndex - 1));
                                tempb[tempIndex] = strb[strbIndex];
                                tempIndex++;
                            }
                            else 
                            {
                                tempKeyIndex++;
                            }
                            __index++;
                        }
                    }
                }
                else 
                {
                    //其他
                    while (tempIndex < tempb.Length) 
                    {
                        if (tempIndex == (((countLeng / _keyb.Length) * tempKeyIndex) - 1))
                        {
                            tempKeyIndex++;
                        }
                        else { }
                        int strbIndex = ((countLeng * i) + tempIndex + (tempKeyIndex - 1));
                        tempb[tempIndex] = strb[strbIndex];
                        tempIndex++;
                    }
                }
                bs[i] = tempb;
            }
 
            int allLeng = 0;
            foreach (var a in bs) 
            {
                allLeng += a.Length;
            }
 
            var resb = new byte[allLeng];
            int resbIndex = 0;
            foreach (var a in bs) 
            {
                foreach (var c in a) 
                {
                    resb[resbIndex++] = c;
                }
            }
 
            res = Base64ToString(System.Text.Encoding.UTF8.GetString(resb));
 
            return res;
        }
 
 
 
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str">待加密的字符串</param>
        /// <param name="IsUpper">是否大写</param>
        /// <param name="bit">加密位数16或者32</param>
        /// <returns></returns>
        private static string MD5(string str, bool IsUpper, int bit)
        {
            System.Security.Cryptography.MD5 md = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] byteStream = System.Text.Encoding.UTF8.GetBytes(str);
            byte[] temp = md.ComputeHash(byteStream);
            StringBuilder tempStr = new StringBuilder();
            string result = null;
            foreach (var a in temp)
            {
                tempStr.Append(a.ToString("x2"));
            }
            if (IsUpper)
            {
                result = tempStr.ToString().ToUpper();
            }
            else
            {
                result = tempStr.ToString().ToLower();
            }
            if (bit == 16)
            {
                result = result.Substring(8, 16);
            }
            else if (bit == 32)
            {
                result = result.ToString();
            }
            else
            {
                result = string.Empty;
            }
            return result;
        }
 
        /// <summary>
        /// base64转普通字符串
        /// </summary>
        /// <param name="base64">base64字符串</param>
        /// <returns></returns>
        private static string Base64ToString(string base64)
        {
            var res = "";
            byte[] c = Convert.FromBase64String(base64);
            res = System.Text.Encoding.UTF8.GetString(c);
            return res;
        }
 
        /// <summary>
        /// 字符串转base64
        /// </summary>
        /// <param name="str">待转换的字符串</param>
        /// <returns></returns>
        private static string StringToBase64(string str)
        {
            var res = "";
            var b = System.Text.Encoding.UTF8.GetBytes(str);
            res = Convert.ToBase64String(b);
            return res;
        }
    }
 
    internal enum CodingType 
    {
        /// <summary>
        /// 常规
        /// </summary>
        Standard = 0
    }
}