Skip to content

Commit bc548ab

Browse files
committedSep 20, 2019
Working Encrypt/Decrypt for common files
1 parent 0bac4b4 commit bc548ab

File tree

4 files changed

+157
-11
lines changed

4 files changed

+157
-11
lines changed
 

‎ConquerToolsKit/ConquerToolsKit/ConquerDatFile.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public DatFileConfig GetCurrentConfig()
6868
/// <summary>
6969
/// Open the file and get content
7070
/// </summary>
71-
public bool Open()
71+
public bool Open(bool isDecrypted = false)
7272
{
7373
bool success = false;
7474
switch (CurrentDatFileType)
@@ -78,7 +78,14 @@ public bool Open()
7878
case DatFileType.MAGICTYPEOP:
7979
{
8080
byte[] content = File.ReadAllBytes(CurrentFilename);
81-
string oneBigString = Encoding.ASCII.GetString(Decrypt(content));
81+
string oneBigString = "";
82+
if (isDecrypted)
83+
{
84+
oneBigString = Encoding.ASCII.GetString(content);
85+
} else
86+
{
87+
oneBigString = Encoding.ASCII.GetString(Decrypt(content));
88+
}
8289
string[] contentLines = oneBigString.Split('\n');
8390
List<string> rawBuilder = new List<string>();
8491

@@ -111,7 +118,7 @@ public bool Open()
111118
n++;
112119
}
113120
CurrentFileContent.Add(nLine, dfline);
114-
rawBuilder.Add(currentLine + Environment.NewLine);
121+
rawBuilder.Add(currentLine.TrimEnd('\r') + Environment.NewLine);
115122
nLine++;
116123
}
117124
}
@@ -152,13 +159,30 @@ public void Save()
152159
foreach (string str in CurrentRAWFileContent)
153160
{
154161
stream.Write(Encoding.ASCII.GetBytes(str), 0, Encoding.ASCII.GetBytes(str).Length);
155-
stream.Write(Encoding.ASCII.GetBytes("\n"), 0, Encoding.ASCII.GetBytes("\n").Length);
156162
}
157163
// Save to file
158164
File.WriteAllBytes(outputFilename, Encrypt(stream.ToArray()));
159165
// Log Info: Original Size of file is changed. Any error on process?¿
160166
}
161167

168+
/// <summary>
169+
/// Save the file (decrypted)
170+
/// </summary>
171+
public void DecryptedSave()
172+
{
173+
// Force .txt extension output
174+
string outputFilename = Path.ChangeExtension(CurrentFilename, "txt");
175+
// Encrypt the content
176+
MemoryStream stream = new MemoryStream();
177+
foreach (string str in CurrentRAWFileContent)
178+
{
179+
stream.Write(Encoding.ASCII.GetBytes(str), 0, Encoding.ASCII.GetBytes(str).Length);
180+
}
181+
// Save to file
182+
File.WriteAllBytes(outputFilename, stream.ToArray());
183+
// Log Info: Original Size of file is changed. Any error on process?¿
184+
}
185+
162186
private byte[] Decrypt(byte[] b)
163187
{
164188
for (int i = 0; i < b.Length; i++)

‎ConquerToolsKit/ConquerToolsKit/ConquerTools.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,14 @@ public void AutoDetectionDecrypt(string filename, string filenameOutput)
158158
DatFileConfig datFileConfig = ConquerToolsHelper.CTools.CurrentConfig.DatFilesConfig.Where(x => x.Key == dc.CurrentDatFileType).FirstOrDefault().Value;
159159
if (datFileConfig != null)
160160
{
161-
dc.Open();
161+
if (Path.GetExtension(filename) == ".txt")
162+
{
163+
dc.Open(true);
164+
}
165+
else
166+
{
167+
dc.Open();
168+
}
162169
}
163170
}
164171

@@ -169,7 +176,13 @@ public void CustomDecrypt(string filename, string filenameOutput, ConquerDatFile
169176
DatFileConfig datFileConfig = ConquerToolsHelper.CTools.CurrentConfig.DatFilesConfig.Where(x => x.Key == dc.CurrentDatFileType).FirstOrDefault().Value;
170177
if (datFileConfig != null)
171178
{
172-
dc.Open();
179+
if (Path.GetExtension(filename) == ".txt")
180+
{
181+
dc.Open(true);
182+
} else
183+
{
184+
dc.Open();
185+
}
173186
}
174187
}
175188

‎ConquerToolsKit/ConquerToolsKit/Main.Designer.cs

Lines changed: 83 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎ConquerToolsKit/ConquerToolsKit/Main.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Reflection;
56
using System.Text;
67
using System.Windows.Forms;
78
using static ConquerToolsKit.ConquerDatFile;
@@ -19,11 +20,13 @@ public Main()
1920
private void Main_Load(object sender, EventArgs e)
2021
{
2122
cbxDatFileType.DataSource = Enum.GetNames(typeof(DatFileType));
23+
cbxFileType.DataSource = Enum.GetNames(typeof(DatFileType));
2224
}
2325

2426
private void SelectFile_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
2527
{
2628
lblSelectedDatFile.Text = "Selected File: " + selectFile.FileName;
29+
lblSelectedDatGeneral.Text = "Selected File: " + selectFile.FileName;
2730
}
2831

2932
private void BtnDecryptDat_Click(object sender, EventArgs e)
@@ -107,5 +110,33 @@ private void MainIcon_Click(object sender, EventArgs e)
107110
{
108111
tabTools.SelectedIndex = tabTools.TabCount - 1;
109112
}
113+
114+
private void BtnOpenFileGeneral_Click(object sender, EventArgs e)
115+
{
116+
selectFile.Filter = "Encrypted or Decrypted Conquer Dat File|*.dat;*.txt";
117+
DialogResult dres = selectFile.ShowDialog();
118+
}
119+
120+
private void btnEncryptDecrypt_Click(object sender, EventArgs e)
121+
{
122+
if (selectFile.CheckFileExists)
123+
{
124+
string ext = Path.GetExtension(selectFile.FileName);
125+
string filenameOutput = Path.ChangeExtension(selectFile.FileName, ext == ".dat" ? "txt" : "dat");
126+
Enum.TryParse(cbxDatFileType.SelectedItem.ToString(), out DatFileType datFileType);
127+
if (datFileType == DatFileType.AUTODETECT)
128+
{
129+
ConquerToolsHelper.CTools.AutoDetectionDecrypt(selectFile.FileName, filenameOutput);
130+
}
131+
else
132+
{
133+
ConquerToolsHelper.CTools.CustomDecrypt(selectFile.FileName, filenameOutput, datFileType);
134+
}
135+
if (ext == ".dat") { ConquerToolsHelper.CTools.SelectedDatFile.DecryptedSave(); } else { ConquerToolsHelper.CTools.SelectedDatFile.Save(); }
136+
} else
137+
{
138+
MessageBox.Show("Please, select some dat file.", Assembly.GetCallingAssembly().GetName().Name, MessageBoxButtons.OK, MessageBoxIcon.Warning);
139+
}
140+
}
110141
}
111142
}

0 commit comments

Comments
 (0)