Skip to content

Commit

Permalink
Made Flawless2 run on .NET Framework 4
Browse files Browse the repository at this point in the history
  • Loading branch information
kolya5544 committed Dec 10, 2022
1 parent 293750d commit 57acaeb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Flawless2.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<OutputType>Library</OutputType>
<TargetFramework>net40</TargetFramework>
</PropertyGroup>

</Project>
40 changes: 21 additions & 19 deletions FlawlessAlgo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,30 @@ public MemoryStream Encrypt(byte[] contents)
{
//here we make sure the vulnerability of last block being sometimes half empty doesn't exist.
FillRandom(ref block);
ms.Read(block);
ms.Read(block, 0, block.Length);

EncryptBlock(ref block, index++);
blocks.Write(block);
blocks.Write(block, 0, block.Length);
}

blocks.Position = 0;

output.Position = 4;
byte[] lenBytes = BitConverter.GetBytes((ushort)contents.Length);
byte[] lenBytes = BitConverter.GetBytes((uint)contents.Length);
LenEncAlgo(ref lenBytes, InitialKey); //encrypts the Length header

output.Write(lenBytes);
output.Write(blocks.ToArray());
output.Write(lenBytes, 0, lenBytes.Length);
var arr = blocks.ToArray();
output.Write(arr, 0, arr.Length);

output.Position = 0;
byte[] resultArr = output.ToArray(); resultArr = resultArr.TakeLast(resultArr.Length - 4).ToArray(); //this extracts the encrypted payload
byte[] resultArr = output.ToArray(); resultArr = resultArr.ToList().GetRange(4, resultArr.Length - 4).ToArray(); //this extracts the encrypted payload
byte[] checksum1 = CRC16(resultArr); //this checksum is used to determine if contents should be decrypted
byte[] checksum2 = CRC16(contents); //this checksum is used to check if contents were decrypted properly. It is encrypted in a way similar to LenEncAlgo encryption.
CheckEncAlgo(ref checksum2, InitialKey); //encrypts content checksum
output.Position = 0;
output.Write(checksum1);
output.Write(checksum2);
output.Write(checksum1, 0, checksum1.Length);
output.Write(checksum2, 0, checksum2.Length);
output.Position = 0;

LastOperationSuccess = true;
Expand All @@ -63,9 +64,9 @@ public static bool IsEncrypted(byte[] content)
byte[] buffer = new byte[2];
byte[] data = new byte[content.Length - 4]; //w/out checksums

ms.Read(encChecksum);
ms.Read(buffer);
ms.Read(data);
ms.Read(encChecksum, 0, encChecksum.Length);
ms.Read(buffer, 0, buffer.Length);
ms.Read(data, 0, data.Length);

byte[] crc = CRC16(data);
if (crc[0] == encChecksum[0] && crc[1] == encChecksum[1])
Expand All @@ -78,30 +79,30 @@ public static bool IsEncrypted(byte[] content)
public MemoryStream Decrypt(byte[] contents)
{
MemoryStream ms = new MemoryStream(contents);
byte[] buffer = new byte[2];
byte[] buffer = new byte[4];
byte[] contentCheck = new byte[2];
ms.Position = 2; //skipping first two bytes as we don't care about encryption checksum.
ms.Read(contentCheck); //taking next two bytes containing content checksum
ms.Read(contentCheck, 0, contentCheck.Length); //taking next two bytes containing content checksum
CheckEncAlgo(ref contentCheck, InitialKey); // decrypting content check checksum
ms.Read(buffer); //reading two bytes (ushort) to buffer
ms.Read(buffer, 0, buffer.Length); //reading two bytes (ushort) to buffer
LenEncAlgo(ref buffer, InitialKey); //decrypts the Length header
ushort textlen = BitConverter.ToUInt16(buffer);
uint textlen = BitConverter.ToUInt32(buffer, 0);

//now just reading the remaining blocks
var blocks = new byte[ms.Length - 6]; // encryption checksum (2 bytes) + content checksum (2 bytes) + length (2 bytes)
ms.Read(blocks);
var blocks = new byte[ms.Length - 8]; // encryption checksum (2 bytes) + content checksum (2 bytes) + length (4 bytes)
ms.Read(blocks, 0, blocks.Length);

MemoryStream rms = new MemoryStream(blocks);
MemoryStream output = new MemoryStream();
int index = 0;
for (int i = 0; i < blocks.Length / 8; i++)
{
byte[] block = new byte[8];
rms.Read(block);
rms.Read(block, 0, block.Length);
//blockList.Add(block);
EncryptBlock(ref block, index++);

output.Write(block);
output.Write(block, 0, block.Length);
}

output.SetLength(textlen);
Expand All @@ -126,6 +127,7 @@ private static void LenEncAlgo(ref byte[] lenBytes, string key)
{
byte[] lenBytesKey = sha256(key + "len"); //here we calculate a special key
lenBytes[0] ^= lenBytesKey[0]; lenBytes[1] ^= lenBytesKey[1]; //and then use it to encrypt content length header.
lenBytes[2] ^= lenBytesKey[2]; lenBytes[3] ^= lenBytesKey[3];
}

private static void CheckEncAlgo(ref byte[] checkBytes, string key)
Expand Down

0 comments on commit 57acaeb

Please sign in to comment.