-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathProjectFileReader.cs
More file actions
175 lines (156 loc) · 7.2 KB
/
Copy pathProjectFileReader.cs
File metadata and controls
175 lines (156 loc) · 7.2 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
using QIQI.EProjectFile.Encryption;
using QIQI.EProjectFile.Internal;
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace QIQI.EProjectFile
{
public class ProjectFileReader : IDisposable
{
public delegate string OnInputPassword(string passwordHint);
public bool IsFinish { get; private set; } = false;
private BinaryReader reader;
public bool CryptEC { get; } = false;
public ProjectFileReader(Stream stream, OnInputPassword inputPassword = null)
{
reader = new BinaryReader(stream, Encoding.GetEncoding("gbk"));
int magic1 = reader.ReadInt32();
int magic2 = reader.ReadInt32();
if (magic1 == 0x454C5457) // WTLE
{
switch (magic2)
{
case 0x00000001:
{
string password = inputPassword?.Invoke(null);
if (string.IsNullOrEmpty(password))
{
throw new Exception("没有输入密码 或 未正确响应InputPassword事件");
}
const int lengthOfRead = 8;
var cryptoTransform = new EStdCryptoTransform(EplSecret.EStd.Factory.Create(Encoding.GetEncoding("gbk").GetBytes(password)));
// 分块的时候,是不区分加密与非加密部分的,不进行 SeekToBegin 直接解密会导致分块错误
// 然而我们不能保证 Stream 一定 CanSeek,因此使用 PrefixedStream
var cryptoStream = new CryptoStream(new PrefixedStream(stream, new byte[lengthOfRead]), cryptoTransform, CryptoStreamMode.Read);
// 跳过非加密部分(但使得 CryptoStream 按与加密部分相同的方式来将这些数据考虑进分块过程)
cryptoStream.Read(new byte[lengthOfRead], 0, lengthOfRead);
reader = new BinaryReader(cryptoStream);
if (!reader.ReadBytes(cryptoTransform.SecretId.Length).SequenceEqual(cryptoTransform.SecretId))
{
throw new Exception("密码错误");
}
}
break;
case 0x00020001:
{
CryptEC = true;
int tip_bytes = reader.ReadInt32();
string tip = reader.ReadStringWithFixedLength(Encoding.GetEncoding("gbk"), tip_bytes);
string password = inputPassword?.Invoke(tip);
if (string.IsNullOrEmpty(password))
{
throw new Exception("没有输入密码 或 未正确响应InputPassword事件");
}
int lengthOfRead = 4 /* [int]magic1 */ + 4 /* [int]magic2 */ + 4 /* [int]tip_bytes */ + tip_bytes;
var cryptoTransform = new CryptoECTransform(EplSecret.EC.Factory.Create(Encoding.GetEncoding("gbk").GetBytes(password)));
// 分块的时候,是不区分加密与非加密部分的,不进行 SeekToBegin 直接解密会导致分块错误
// 然而我们不能保证 Stream 一定 CanSeek,因此使用 PrefixedStream
var cryptoStream = new CryptoStream(new PrefixedStream(stream, new byte[lengthOfRead]), cryptoTransform, CryptoStreamMode.Read);
// 跳过非加密部分(但使得 CryptoStream 按与加密部分相同的方式来将这些数据考虑进分块过程)
cryptoStream.Read(new byte[lengthOfRead], 0, lengthOfRead);
reader = new BinaryReader(cryptoStream);
if (!reader.ReadBytes(cryptoTransform.SecretId.Length).SequenceEqual(cryptoTransform.SecretId))
{
throw new Exception("密码错误");
}
}
break;
default:
throw new Exception($"不支持此类加密文件 [Type=0x{magic2:x8}]");
}
magic1 = reader.ReadInt32();
magic2 = reader.ReadInt32();
}
if (magic1 != 0x54574E43 || magic2 != 0x47525045) // CNWTEPRG
{
throw new Exception("不是易语言工程文件");
}
}
public RawSectionInfo ReadSection()
{
if (IsFinish)
{
throw new EndOfStreamException();
}
RawSectionInfo section = new RawSectionInfo();
if (!(reader.ReadInt32() == 0x15117319))
{
throw new Exception("Magic错误");
}
reader.ReadInt32(); // Skip InfoCheckSum
section.Key = reader.ReadInt32();
section.Name = DecodeName(section.Key, reader.ReadBytes(30));
reader.ReadInt16(); // 对齐填充(确认于易语言V5.71)
reader.ReadInt32(); // Skip Index
section.IsOptional = reader.ReadInt32() != 0;
reader.ReadInt32(); // Skip DataCheckSum
int dataLength = reader.ReadInt32();
if (CryptEC)
{
dataLength ^= 1;
}
reader.ReadBytes(40); // 保留未用(确认于易语言V5.71)
section.Data = new byte[dataLength];
reader.Read(section.Data, 0, dataLength);
if (section.Key == 0x07007319)
{
IsFinish = true;
}
return section;
}
private static string DecodeName(int key, byte[] encodedName)
{
if (encodedName == null)
{
return string.Empty;
}
byte[] r = (byte[])encodedName.Clone();
if (key != 0x07007319)
{
var keyBytes = unchecked(new byte[] { (byte)key, (byte)(key >> 8), (byte)(key >> 16), (byte)(key >> 24) });
for (int i = 0; i < r.Length; i++)
{
r[i] ^= keyBytes[(i + 1) % 4];
}
}
int count = Array.IndexOf<byte>(r, 0);
if (count != -1)
{
var t = new byte[count];
Array.Copy(r, t, count);
r = t;
}
return Encoding.GetEncoding("gbk").GetString(r);
}
#region IDisposable Support
private bool disposedValue = false; // 要检测冗余调用
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
reader.Dispose();
}
disposedValue = true;
}
}
public void Dispose()
{
Dispose(true);
}
#endregion
}
}