forked from ivanmeler/SamFirm_Reborn
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCrypto.cs
195 lines (187 loc) · 7.58 KB
/
Crypto.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
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
namespace hadesFirm
{
internal class Crypto
{
private static readonly byte[] IV = new byte[1];
public static Form1 form;
private static byte[] KEY;
public static int Decrypt(string encryptedFile, string outputFile, bool GUI = true)
{
using (FileStream fileStream1 = new FileStream(encryptedFile, FileMode.Open))
{
using (FileStream fileStream2 = new FileStream(outputFile, FileMode.Create))
{
RijndaelManaged rijndaelManaged = new RijndaelManaged();
rijndaelManaged.Mode = CipherMode.ECB;
rijndaelManaged.BlockSize = 128;
rijndaelManaged.Padding = PaddingMode.PKCS7;
using (ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(Crypto.KEY, Crypto.IV))
{
try
{
Utility.PreventDeepSleep(Utility.PDSMode.Start);
using (CryptoStream cryptoStream = new CryptoStream((Stream) fileStream1, decryptor, CryptoStreamMode.Read))
{
byte[] buffer = new byte[256 * 1024];
long bytesRead = 0;
int count;
do
{
Utility.PreventDeepSleep(Utility.PDSMode.Continue);
bytesRead += (long) (count = cryptoStream.Read(buffer, 0, buffer.Length));
fileStream2.Write(buffer, 0, count);
if (GUI)
Crypto.form.SetProgressBar(Utility.GetProgress(bytesRead, fileStream1.Length), bytesRead);
else
CmdLine.SetProgress(Utility.GetProgress(bytesRead, fileStream1.Length));
}
while (count > 0);
}
}
catch (CryptographicException ex)
{
Logger.WriteLog("Error decrypting file: Wrong key.", false);
return 3;
}
catch (TargetInvocationException ex)
{
Logger.WriteLog("Error decrypting file: Please turn off FIPS compliance checking.", false);
return 800;
}
catch (IOException ex)
{
Logger.WriteLog("Error decrypting file: IOException: " + ex.Message, false);
return 3;
}
finally
{
Utility.PreventDeepSleep(Utility.PDSMode.Stop);
}
}
}
}
return 0;
}
public static int DecryptAndUnzip(string encryptedFile, string outputDirectory, bool GUI = true)
{
using (FileStream fileStream1 = new FileStream(encryptedFile, FileMode.Open))
{
RijndaelManaged rijndaelManaged = new RijndaelManaged();
rijndaelManaged.Mode = CipherMode.ECB;
rijndaelManaged.BlockSize = 128;
rijndaelManaged.Padding = PaddingMode.PKCS7;
using (ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(Crypto.KEY, Crypto.IV))
{
try
{
Utility.PreventDeepSleep(Utility.PDSMode.Start);
using (CryptoStream cryptoStream = new CryptoStream((Stream)fileStream1, decryptor, CryptoStreamMode.Read))
{
Logger.WriteLog($"Please note that the sum of unzipped files might be larger than the downloaded firmware file", false);
using (ZipInputStream s = new ZipInputStream(cryptoStream, 256 * 1024))
{
ZipEntry entry;
byte[] data = new byte[256 * 1024];
long bytesRead = 0;
long fileSize = fileStream1.Length;
while ((entry = s.GetNextEntry()) != null)
{
Utility.PreventDeepSleep(Utility.PDSMode.Continue);
if (entry.IsFile)
{
if (entry.CanDecompress)
{
fileSize -= entry.CompressedSize;
fileSize += entry.Size;
string outputFile = Path.Combine(outputDirectory, entry.Name);
string directory = Path.GetDirectoryName(outputFile);
if (!Directory.Exists(directory))
{
Logger.WriteLog($"Creating directory {directory}", false);
Directory.CreateDirectory(directory);
}
Logger.WriteLog($"Writing file {outputFile}", false);
using (FileStream fileStream2 = new FileStream(outputFile, FileMode.Create))
{
int size;
while ((size = s.Read(data, 0, data.Length)) > 0)
{
bytesRead += size;
fileStream2.Write(data, 0, size);
if (GUI)
Crypto.form.SetProgressBar(Utility.GetProgress(bytesRead, System.Math.Max(fileSize, bytesRead)), bytesRead);
else
CmdLine.SetProgress(Utility.GetProgress(bytesRead, System.Math.Max(fileSize, bytesRead)));
}
}
try
{
File.SetLastWriteTime(outputFile, entry.DateTime);
}
catch { }
}
else
{
bytesRead += entry.Size;
if (GUI)
Crypto.form.SetProgressBar(Utility.GetProgress(bytesRead, System.Math.Max(fileSize, bytesRead)), bytesRead);
else
CmdLine.SetProgress(Utility.GetProgress(bytesRead, System.Math.Max(fileSize, bytesRead)));
}
}
}
}
}
}
catch (CryptographicException)
{
Logger.WriteLog("Error decrypting file: Wrong key.", false);
return 3;
}
catch (TargetInvocationException)
{
Logger.WriteLog("Error decrypting file: Please turn off FIPS compliance checking.", false);
return 800;
}
catch (IOException ex)
{
Logger.WriteLog("Error decrypting file: IOException: " + ex.Message, false);
return 3;
}
catch (System.Exception ex)
{
Logger.WriteLog("Error decrypting file: Exception: " + ex.Message, false);
return 3;
}
finally
{
Utility.PreventDeepSleep(Utility.PDSMode.Stop);
}
}
}
return 0;
}
public static void SetDecryptKey(string region, string model, string version)
{
StringBuilder stringBuilder = new StringBuilder(region);
stringBuilder.Append(':');
stringBuilder.Append(model);
stringBuilder.Append(':');
stringBuilder.Append(version);
byte[] bytes = Encoding.ASCII.GetBytes(stringBuilder.ToString());
using (MD5 md5 = MD5.Create())
Crypto.KEY = md5.ComputeHash(bytes);
}
public static void SetDecryptKey(string version, string LogicValue)
{
byte[] bytes = Encoding.ASCII.GetBytes(Utility.GetLogicCheck(version, LogicValue));
using (MD5 md5 = MD5.Create())
Crypto.KEY = md5.ComputeHash(bytes);
}
}
}