<commit_before>﻿using System;
using System.IO;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;

namespace BinderTool.Core
{
    public static class CryptographyUtility
    {
        public static MemoryStream DecryptAesEcb(Stream inputStream, byte[] key)
        {
            AesEngine engine = new AesEngine();
            KeyParameter parameter = new KeyParameter(key);

            BufferedBlockCipher cipher = new BufferedBlockCipher(new CbcBlockCipher(engine));
            cipher.Init(false, parameter);

            return DecryptAes(inputStream, cipher);
        }

        public static MemoryStream DecryptAesCbc(Stream inputStream, byte[] key, byte[] iv)
        {
            AesEngine engine = new AesEngine();
            KeyParameter keyParameter = new KeyParameter(key);
            ICipherParameters parameters = new ParametersWithIV(keyParameter, iv);

            BufferedBlockCipher cipher = new BufferedBlockCipher(new CbcBlockCipher(engine));
            cipher.Init(true, parameters);
            return DecryptAes(inputStream, cipher);
        }

        public static MemoryStream DecryptAesCtrDsII(Stream inputStream, byte[] key, byte[] iv)
        {
            byte[] ivNew = new byte[16];
            ivNew[0] = 128;
            ivNew[12] = 0;
            ivNew[13] = 0;
            ivNew[14] = 0;
            ivNew[15] = 0;
            // TODO: Analyze how the game calculates the iv. Maybe it is salted?

            return DecryptAesCtr(inputStream, key, ivNew);
        }

        public static MemoryStream DecryptAesCtr(Stream inputStream, byte[] key, byte[] iv)
        {
            AesEngine engine = new AesEngine();
            KeyParameter keyParameter = new KeyParameter(key);
            ICipherParameters parameters = new ParametersWithIV(keyParameter, iv);

            BufferedBlockCipher cipher = new BufferedBlockCipher(new SicBlockCipher(engine));
            cipher.Init(false, parameters);
            return DecryptAes(inputStream, cipher);
        }

        private static MemoryStream DecryptAes(Stream inputStream, BufferedBlockCipher cipher)
        {
            byte[] input = new byte[inputStream.Length];
            byte[] output = new byte[cipher.GetOutputSize((int) inputStream.Length)];

            inputStream.Read(input, 0, (int) inputStream.Length);

            int len = cipher.ProcessBytes(input, 0, input.Length, output, 0);
            cipher.DoFinal(output, len);

            MemoryStream outputStream = new MemoryStream();
            outputStream.Write(output, 0, input.Length);
            outputStream.Seek(0, SeekOrigin.Begin);
            return outputStream;
        }<commit_msg>Fixed using the wrong AES mode to decrypt files<commit_after>﻿using System;
using System.IO;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;

namespace BinderTool.Core
{
    public static class CryptographyUtility
    {
        public static MemoryStream DecryptAesEcb(Stream inputStream, byte[] key)
        {
            AesEngine engine = new AesEngine();
            KeyParameter parameter = new KeyParameter(key);
            BufferedBlockCipher cipher = new BufferedBlockCipher(engine);
            cipher.Init(false, parameter);
            return DecryptAes(inputStream, cipher);
        }

        public static MemoryStream DecryptAesCbc(Stream inputStream, byte[] key, byte[] iv)
        {
            AesEngine engine = new AesEngine();
            KeyParameter keyParameter = new KeyParameter(key);
            ICipherParameters parameters = new ParametersWithIV(keyParameter, iv);
            BufferedBlockCipher cipher = new BufferedBlockCipher(new CbcBlockCipher(engine));
            cipher.Init(true, parameters);
            return DecryptAes(inputStream, cipher);
        }

        public static MemoryStream DecryptAesCtrDsII(Stream inputStream, byte[] key, byte[] iv)
        {
            byte[] ivNew = new byte[16];
            ivNew[0] = 128;
            ivNew[12] = 0;
            ivNew[13] = 0;
            ivNew[14] = 0;
            ivNew[15] = 0;
            // TODO: Analyze how the game calculates the iv. Maybe it is salted/encrypted?

            return DecryptAesCtr(inputStream, key, ivNew);
        }

        public static MemoryStream DecryptAesCtr(Stream inputStream, byte[] key, byte[] iv)
        {
            AesEngine engine = new AesEngine();
            KeyParameter keyParameter = new KeyParameter(key);
            ICipherParameters parameters = new ParametersWithIV(keyParameter, iv);

            BufferedBlockCipher cipher = new BufferedBlockCipher(new SicBlockCipher(engine));
            cipher.Init(false, parameters);
            return DecryptAes(inputStream, cipher);
        }

        private static MemoryStream DecryptAes(Stream inputStream, BufferedBlockCipher cipher)
        {
            byte[] input = new byte[inputStream.Length];
            byte[] output = new byte[cipher.GetOutputSize((int) inputStream.Length)];

            inputStream.Read(input, 0, (int) inputStream.Length);

            int len = cipher.ProcessBytes(input, 0, input.Length, output, 0);
            cipher.DoFinal(output, len);

            MemoryStream outputStream = new MemoryStream();
            outputStream.Write(output, 0, input.Length);
            outputStream.Seek(0, SeekOrigin.Begin);
            return outputStream;
        }
{'fraction_non_alphanumeric': 0.08744434550311665, 'fraction_numerical': 0.00819234194122885, 'mean_word_length': 2.4838709677419355, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4876556', 'n_tokens_mistral': 1606, 'n_tokens_neox': 1516, 'n_words': 383}