Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
210 lines (182 loc) · 7.54 KB
/
Copy pathProgram.cs
File metadata and controls
210 lines (182 loc) · 7.54 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
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
using System;
using System.IO;
using System.Security.Cryptography;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.OpenSsl;
using ProtoBuf;
using Dispatch;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Newtonsoft.Json;
namespace ys.dcur
{
class Program
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileName
{
public int lStructSize;
public IntPtr hwndOwner;
public IntPtr hInstance;
public string lpstrFilter;
public string lpstrCustomFilter;
public int nMaxCustFilter;
public int nFilterIndex;
public string lpstrFile;
public int nMaxFile;
public string lpstrFileTitle;
public int nMaxFileTitle;
public string lpstrInitialDir;
public string lpstrTitle;
public int Flags;
public short nFileOffset;
public short nFileExtension;
public string lpstrDefExt;
public IntPtr lCustData;
public IntPtr lpfnHook;
public string lpTemplateName;
public IntPtr pvReserved;
public int dwReserved;
public int FlagsEx;
}
[DllImport("comdlg32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool GetSaveFileName([In, Out] OpenFileName ofn);
static string SaveFile()
{
OpenFileName ofn = new OpenFileName
{
lStructSize = Marshal.SizeOf(typeof(OpenFileName)),
lpstrFilter = "Json文件 (*.json)\0*.json\0所有文件 (*.*)\0*.*\0",
lpstrFile = new string('\0', 260),
nMaxFile = 260,
lpstrFileTitle = new string('\0', 100),
nMaxFileTitle = 100,
Flags = 0x00000002 | 0x00000004 | 0x00000008 | 0x00080000,
lpstrTitle = "保存"
};
if (GetSaveFileName(ofn))
{
return ofn.lpstrFile;
}
else
{
return null;
}
}
private static AsymmetricCipherKeyPair GetAsymmetricCipherKeyPairFromPem(string pemKey)
{
using (TextReader reader = new StringReader(pemKey))
{
PemReader pemReader = new PemReader(reader);
AsymmetricCipherKeyPair keyPair = (AsymmetricCipherKeyPair)pemReader.ReadObject();
if (keyPair != null)
{
return keyPair;
}
throw new Exception("PEM 私钥格式无效");
}
}
private static RsaKeyParameters GetRsaKeyParametersFromPem(string pemKey)
{
using (TextReader reader = new StringReader(pemKey))
{
PemReader pemReader = new PemReader(reader);
RsaKeyParameters publicKeyParams = (RsaKeyParameters)pemReader.ReadObject();
if (publicKeyParams != null)
{
return publicKeyParams;
}
throw new Exception("PEM 公钥格式无效");
}
}
static void Main()
{
string pubKeyPath = "keys/ServerPub.pem";
string[] privKeyPaths =
{
"keys/ClientPri2.pem",
"keys/ClientPri3.pem",
"keys/ClientPri4.pem",
"keys/ClientPri5.pem"
};
string pubKeyText = File.ReadAllText(pubKeyPath);
RsaKeyParameters pubKeyParameters = GetRsaKeyParametersFromPem(pubKeyText);
RSAParameters rsaPubParameters = DotNetUtilities.ToRSAParameters(pubKeyParameters);
RSACryptoServiceProvider rsaPublic = new RSACryptoServiceProvider();
rsaPublic.ImportParameters(rsaPubParameters);
string DataFile = "data.txt";
if (!File.Exists(DataFile))
{
Console.WriteLine($"{DataFile} 不存在");
Console.Write("请按任意键继续...");
Console.ReadKey();
Environment.Exit(0);
}
string jsonData = File.ReadAllText(DataFile);
dynamic parsedData = JsonConvert.DeserializeObject(jsonData);
string contentBase64 = parsedData.content;
string signBase64 = parsedData.sign;
byte[] content = Convert.FromBase64String(contentBase64);
byte[] sign = Convert.FromBase64String(signBase64);
int keySizeBytes = 256; // 2048 位密钥为 256 字节
List<byte> decryptedData = new List<byte>();
foreach (string privKeyPath in privKeyPaths)
{
string privKeyText = File.ReadAllText(privKeyPath);
AsymmetricCipherKeyPair keyPair = GetAsymmetricCipherKeyPairFromPem(privKeyText);
RsaPrivateCrtKeyParameters privKeyParameters = (RsaPrivateCrtKeyParameters)keyPair.Private;
RSAParameters rsaPrivParameters = DotNetUtilities.ToRSAParameters(privKeyParameters);
RSACryptoServiceProvider rsaPrivate = new RSACryptoServiceProvider();
rsaPrivate.ImportParameters(rsaPrivParameters);
try
{
for (int i = 0; i < content.Length; i += keySizeBytes)
{
int blockSize = Math.Min(keySizeBytes, content.Length - i);
byte[] block = new byte[blockSize];
Array.Copy(content, i, block, 0, blockSize);
byte[] decryptedBlock = rsaPrivate.Decrypt(block, false);
decryptedData.AddRange(decryptedBlock);
}
Console.WriteLine($"成功使用 {privKeyPath} 解密!");
break;
}
catch
{
Console.WriteLine($"尝试使用 {privKeyPath} 解密失败");
decryptedData.Clear();
}
}
if (decryptedData.Count == 0)
{
Console.WriteLine("所有私钥解密失败。");
Console.Write("请按任意键继续...");
Console.ReadKey();
Environment.Exit(0);
}
byte[] result = decryptedData.ToArray();
bool verified = rsaPublic.VerifyData(result, CryptoConfig.MapNameToOID("SHA256"), sign);
Console.WriteLine("验证签名: " + verified);
MemoryStream stream = new MemoryStream(result);
QueryCurrRegionHttpRsp message = Serializer.Deserialize<QueryCurrRegionHttpRsp>(stream);
stream.Close();
string JsonOutput = JsonConvert.SerializeObject(message, Formatting.Indented);
string FormattedJson = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(JsonOutput), Formatting.Indented);
string FilePath = SaveFile();
if (FilePath == null)
{
Console.WriteLine("用户未选择保存文件");
}
else
{
File.WriteAllText(FilePath, FormattedJson);
Console.WriteLine($"JSON 数据已保存到文件: {FilePath}");
Console.WriteLine("解码已完成");
}
Console.Write("请按任意键继续...");
Console.ReadKey();
}
}
}