Skip to content

Instantly share code, notes, and snippets.

@Modder4869
Created June 30, 2023 16:53
Show Gist options
  • Save Modder4869/805f5e38f2a353ea28b1734494d30f09 to your computer and use it in GitHub Desktop.
Save Modder4869/805f5e38f2a353ea28b1734494d30f09 to your computer and use it in GitHub Desktop.
N-INNOCENCE
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
string filePath = @"A"; // Replace with the path to your encrypted file
string password = "4fTrch#60b<3aO48Vb";
string destinationFilePath = "path_to_decrypted_file";
string salt = Path.GetFileName(filePath);
byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
using (Stream baseStream = File.OpenRead(filePath))
{
using (FileStream sourceStream = File.OpenRead(filePath))
{
using (FileStream destinationStream = File.OpenWrite(destinationFilePath))
{
// Create an instance of SeekableAesStream using the source stream, password, and salt
using (SeekableAesStream aesStream = new SeekableAesStream(sourceStream, password, saltBytes))
{
// Read from the aesStream and write to the destination stream
aesStream.CopyTo(destinationStream);
}
}
}
}
public class SeekableAesStream : Stream
{
private Stream baseStream;
private AesManaged aes;
private ICryptoTransform encryptor;
public bool autoDisposeBaseStream { get; set; } = true;
/// <param name="salt">//** WARNING **: MUST be unique for each stream otherwise there is NO security</param>
public SeekableAesStream(Stream baseStream, string password, byte[] salt)
{
this.baseStream = baseStream;
using (var key = new Rfc2898DeriveBytes(password, salt))
{
aes = new AesManaged();
aes.KeySize = 128;
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.None;
aes.Key = key.GetBytes(aes.KeySize / 8);
aes.IV = new byte[16]; //zero buffer is adequate since we have to use new salt for each stream
encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
}
}
private void cipher(byte[] buffer, int offset, int count, long streamPos)
{
//find block number
var blockSizeInByte = aes.BlockSize / 8;
var blockNumber = (streamPos / blockSizeInByte) + 1;
var keyPos = streamPos % blockSizeInByte;
//buffer
var outBuffer = new byte[blockSizeInByte];
var nonce = new byte[blockSizeInByte];
var init = false;
for (int i = offset; i < count; i++)
{
//encrypt the nonce to form next xor buffer (unique key)
if (!init || (keyPos % blockSizeInByte) == 0)
{
BitConverter.GetBytes(blockNumber).CopyTo(nonce, 0);
encryptor.TransformBlock(nonce, 0, nonce.Length, outBuffer, 0);
if (init) keyPos = 0;
init = true;
blockNumber++;
}
buffer[i] ^= outBuffer[keyPos]; //simple XOR with generated unique key
keyPos++;
}
}
public override bool CanRead { get { return baseStream.CanRead; } }
public override bool CanSeek { get { return baseStream.CanSeek; } }
public override bool CanWrite { get { return baseStream.CanWrite; } }
public override long Length { get { return baseStream.Length; } }
public override long Position { get { return baseStream.Position; } set { baseStream.Position = value; } }
public override void Flush() { baseStream.Flush(); }
public override void SetLength(long value) { baseStream.SetLength(value); }
public override long Seek(long offset, SeekOrigin origin) { return baseStream.Seek(offset, origin); }
public override int Read(byte[] buffer, int offset, int count)
{
var streamPos = Position;
var ret = baseStream.Read(buffer, offset, count);
cipher(buffer, offset, count, streamPos);
return ret;
}
public override void Write(byte[] buffer, int offset, int count)
{
cipher(buffer, offset, count, Position);
baseStream.Write(buffer, offset, count);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
encryptor?.Dispose();
aes?.Dispose();
if (autoDisposeBaseStream)
baseStream?.Dispose();
}
base.Dispose(disposing);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment