initial commit

This commit is contained in:
what 2021-07-26 13:04:16 -07:00
commit 1b60743303
274 changed files with 25866 additions and 0 deletions

View file

@ -0,0 +1,80 @@
// Decompiled with JetBrains decompiler
// Type: SEGATools.Encrypt.DESKey
// Assembly: SEGATools, Version=1.0.3.0, Culture=neutral, PublicKeyToken=611be24fdeb07e08
// MVID: D631183F-57B1-40A1-B502-5364D288307A
// Assembly location: SEGATools.dll
using System;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
namespace SEGATools.Encrypt
{
public class DESKey
{
private static readonly Logger.ILog logger = Logger.CreateLog();
private static readonly byte[] DEFAULT_NAOMI_IV = new byte[8];
private static readonly Regex VALID_KEY_STRING = new Regex("[a-f0-9]{16}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
public byte[] Key { get; private set; }
public byte[] IV => DESKey.DEFAULT_NAOMI_IV;
public ICryptoTransform DecryptionCryptoTransform { get; private set; }
public ICryptoTransform EncryptionCryptoTransform { get; private set; }
private DESKey(
byte[] Key,
ICryptoTransform encryptionCryptoTransform,
ICryptoTransform decryptionCryptoTransform)
{
this.Key = Key;
this.EncryptionCryptoTransform = encryptionCryptoTransform;
this.DecryptionCryptoTransform = decryptionCryptoTransform;
}
public string KeyString => string.Format("{7:X}{6:X}{5:X}{4:X}{3:X}{2:X}{1:X}{0:X}", (object) this.Key[0], (object) this.Key[1], (object) this.Key[2], (object) this.Key[3], (object) this.Key[4], (object) this.Key[5], (object) this.Key[6], (object) this.Key[7]);
public override string ToString() => string.Format("DESKey: 0x{0}", (object) this.KeyString);
public static DESKey Parse(string key)
{
byte[] numArray = new byte[key.Length / 2];
for (int index = 0; index < numArray.Length; ++index)
{
numArray[index] = (byte) ((key[2 * index] > 'F' ? (int) key[2 * index] - 87 : (key[2 * index] > '9' ? (int) key[2 * index] - 55 : (int) key[2 * index] - 48)) << 4);
numArray[index] |= key[2 * index + 1] > 'F' ? (byte) ((int) key[2 * index + 1] - 87) : (key[2 * index + 1] > '9' ? (byte) ((int) key[2 * index + 1] - 55) : (byte) ((int) key[2 * index + 1] - 48));
}
if (BitConverter.IsLittleEndian)
Array.Reverse((Array) numArray);
DES des = (DES) new DESCryptoServiceProvider();
des.KeySize = 64;
des.Padding = PaddingMode.None;
des.Mode = CipherMode.ECB;
ICryptoTransform encryptor = des.CreateEncryptor(numArray, DESKey.DEFAULT_NAOMI_IV);
ICryptoTransform decryptor = des.CreateDecryptor(numArray, DESKey.DEFAULT_NAOMI_IV);
return new DESKey(numArray, encryptor, decryptor);
}
public static bool TryParse(string key)
{
if (string.IsNullOrEmpty(key))
return false;
string str = key.Trim();
if (!DESKey.VALID_KEY_STRING.Match(str).Success)
return false;
try
{
DESKey.Parse(str);
return true;
}
catch (CryptographicException ex)
{
logger.Error(ex);
return false;
}
}
}
}

View file

@ -0,0 +1,133 @@
// Decompiled with JetBrains decompiler
// Type: SEGATools.Encrypt.DesEncryptDecryptTool
// Assembly: SEGATools, Version=1.0.3.0, Culture=neutral, PublicKeyToken=611be24fdeb07e08
// MVID: D631183F-57B1-40A1-B502-5364D288307A
// Assembly location: SEGATools.dll
using SEGATools.UserProcess;
using SEGATools.VirtualFile;
using System;
using System.ComponentModel;
using System.IO;
using System.Security.Cryptography;
namespace SEGATools.Encrypt
{
public class DesEncryptDecryptTool : UserProcessBase
{
private const int BUFFER_SIZE = 65536;
public event AsyncOperationProgressChangedEventHandler DecryptionProgressChanged
{
add => this.AsyncOperationProgressChanged += value;
remove => this.AsyncOperationProgressChanged -= value;
}
public event AsyncOperationCompletedEventHandler DecryptionCompleted
{
add => this.AsyncOperationCompleted += value;
remove => this.AsyncOperationCompleted -= value;
}
public void EncryptAsync(
IVirtualFile inputFile,
IVirtualFile outputFile,
DESKey Key,
object taskId)
{
AsyncOperation asyncOperation = this.CreateAsyncOperation(taskId);
new DesEncryptDecryptTool.WorkerEventHandler(this.EncryptOrDecryptWorker).BeginInvoke(inputFile, outputFile, Key.EncryptionCryptoTransform, asyncOperation, (AsyncCallback) null, (object) null);
}
public void DecryptAsync(
IVirtualFile inputFile,
IVirtualFile outputFile,
DESKey Key,
object taskId)
{
AsyncOperation asyncOperation = this.CreateAsyncOperation(taskId);
new DesEncryptDecryptTool.WorkerEventHandler(this.EncryptOrDecryptWorker).BeginInvoke(inputFile, outputFile, Key.DecryptionCryptoTransform, asyncOperation, (AsyncCallback) null, (object) null);
}
private void EncryptOrDecryptWorker(
IVirtualFile inputFile,
IVirtualFile outputFile,
ICryptoTransform CryptoTransform,
AsyncOperation asyncOp)
{
byte[] numArray = new byte[65536];
Exception exception = (Exception) null;
try
{
using (System.IO.Stream fileInputStream = inputFile.FileInputStream)
{
using (System.IO.Stream fileOutputStream = outputFile.FileOutputStream)
{
fileInputStream.Seek(0L, SeekOrigin.Begin);
while (fileInputStream.Position < fileInputStream.Length)
{
if (!this.TaskCanceled(asyncOp))
{
using (MemoryStream memoryStream = new MemoryStream(numArray))
{
using (CryptoStream CryptoStream = new CryptoStream((System.IO.Stream) memoryStream, CryptoTransform, CryptoStreamMode.Write))
this.EncryptOrDecryptBuffer(numArray, CryptoStream, fileInputStream, fileOutputStream);
}
int num = (int) ((double) fileInputStream.Position / (double) fileInputStream.Length * 100.0);
this.ReportProgress(new UserProcessProgressChangedEventArgs(inputFile.OriginalFileName, outputFile.OriginalFileName, num, num, asyncOp.UserSuppliedState), asyncOp);
}
else
break;
}
}
}
}
catch (Exception ex)
{
exception = ex;
}
if (!this.TaskCanceled(asyncOp))
{
if (exception == null)
goto label_27;
}
try
{
File.Delete(outputFile.OriginalFileName);
}
catch (Exception ex)
{
UserProcessBase.logger.ErrorFormat("Unable to delete the file {0}: {1}", (object) outputFile.OriginalFileName, (object) ex.Message);
}
label_27:
this.ReportCompletion(outputFile.OriginalFileName, exception, asyncOp);
}
private void EncryptOrDecryptBuffer(
byte[] Buffer,
CryptoStream CryptoStream,
System.IO.Stream InputStream,
System.IO.Stream OutputStream)
{
int num1 = 0;
int num2 = Buffer.Length / 8;
InputStream.Read(Buffer, 0, Buffer.Length);
while (num1 < num2)
{
int num3 = num1++ * 8;
if (BitConverter.IsLittleEndian)
Array.Reverse((Array) Buffer, num3, 8);
CryptoStream.Write(Buffer, num3, 8);
if (BitConverter.IsLittleEndian)
Array.Reverse((Array) Buffer, num3, 8);
}
OutputStream.Write(Buffer, 0, Buffer.Length);
}
private delegate void WorkerEventHandler(
IVirtualFile inputFile,
IVirtualFile outputFile,
ICryptoTransform CryptoTransform,
AsyncOperation asyncOp);
}
}