Skip to content

Commit

Permalink
Forgot to push this sooner, turns out, I was dumb, and it all uses PK…
Browse files Browse the repository at this point in the history
…CS7 and not PKCS7/Zeros for en/decryption
  • Loading branch information
TraceEntertains committed Sep 6, 2024
1 parent 439462c commit 6b93156
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions tns2tool/Cryptography.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static class Cryptography

public static byte[] DecryptAllBytesAesAndGZip(string path)
{
byte[] byteBuffer = DecryptAllBytesAes(path, PaddingMode.Zeros);
byte[] byteBuffer = DecryptAllBytesAes(path);
using MemoryStream dataStream = new(byteBuffer);
using MemoryStream outStream = new();
using GZipStream gzipStream = new(dataStream, CompressionMode.Decompress);
Expand All @@ -36,21 +36,21 @@ public static byte[] DecryptAllBytesAesAndGZip(string path)
return byteBuffer;
}

public static byte[] DecryptAllBytesAes(string path, PaddingMode paddingMode = PaddingMode.PKCS7)
public static byte[] DecryptAllBytesAes(string path)
{
byte[] fileBuffer = File.ReadAllBytes(path);
string fileName = Path.GetFileNameWithoutExtension(path);

return DecryptAllBytesAes(fileBuffer, fileName, paddingMode);
return DecryptAllBytesAes(fileBuffer, fileName);
}

public static byte[] DecryptAllBytesAes(byte[] encryptedBytes, string fileName, PaddingMode paddingMode)
public static byte[] DecryptAllBytesAes(byte[] encryptedBytes, string fileName)
{
using Aes aes = Aes.Create();
aes.BlockSize = 128;
aes.KeySize = 256;
aes.Mode = CipherMode.CBC;
aes.Padding = paddingMode;
aes.Padding = PaddingMode.PKCS7;

CreateKey(aes.KeySize, out byte[] keyOut, aes.BlockSize, out byte[] ivOut, fileName);

Expand All @@ -70,35 +70,35 @@ public static byte[] DecryptAllBytesAes(byte[] encryptedBytes, string fileName,

public static byte[] EncryptAllBytesAesAndGZip(string path)
{
using FileStream fileStream = File.OpenRead(path);
using GZipStream gzipStream = new(fileStream, CompressionMode.Compress);
using MemoryStream outStream = new();
using GZipStream gzipStream = new(outStream, CompressionLevel.Optimal);
using FileStream fileStream = File.OpenRead(path);

gzipStream.CopyTo(outStream);
fileStream.CopyTo(gzipStream);
gzipStream.Close();
fileStream.Close();

string fileName = Path.GetFileNameWithoutExtension(path);

// outStream.Position = 0 is not required due to the use of outStream.ToArray()
return EncryptAllBytesAes(outStream.ToArray(), fileName, PaddingMode.Zeros);
return EncryptAllBytesAes(outStream.ToArray(), fileName);
}

public static byte[] EncryptAllBytesAes(string filePath, PaddingMode paddingMode = PaddingMode.PKCS7)
public static byte[] EncryptAllBytesAes(string filePath)
{
byte[] fileBuffer = File.ReadAllBytes(filePath);
string fileName = Path.GetFileNameWithoutExtension(filePath);

return EncryptAllBytesAes(fileBuffer, fileName, paddingMode);
return EncryptAllBytesAes(fileBuffer, fileName);
}

public static byte[] EncryptAllBytesAes(byte[] decryptedBytes, string fileName, PaddingMode paddingMode)
public static byte[] EncryptAllBytesAes(byte[] decryptedBytes, string fileName)
{
using Aes aes = Aes.Create();
aes.BlockSize = 128;
aes.KeySize = 256;
aes.Mode = CipherMode.CBC;
aes.Padding = paddingMode;
aes.Padding = PaddingMode.PKCS7;

CreateKey(aes.KeySize, out byte[] keyOut, aes.BlockSize, out byte[] ivOut, fileName);
aes.Key = keyOut;
Expand Down
2 changes: 1 addition & 1 deletion tns2tool/tns2tool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>0.2.1.1</Version>
<Version>0.2.2</Version>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<Configurations>Debug;Release;FullRelease</Configurations>
<RuntimeIdentifiers>win-x64;osx-x64;linux-x64</RuntimeIdentifiers>
Expand Down

0 comments on commit 6b93156

Please sign in to comment.