Exemplo n.º 1
0
        public byte[] Decrypt(byte[] input)
        {
            byte[] output = new byte[input.Length];

            byte[] key          = (byte[])(NormalKeys[Slot].Clone());
            byte[] ctr_iv_nonce = new byte[0x10];
            CTR_IV_NONCE.CopyTo(ctr_iv_nonce, 0);

            switch (Mode)
            {
            case AesMode.NotSupported:
                throw new NotSupportedException();

            case AesMode.CBC:
                using (var _aes = CreateAes(key, ctr_iv_nonce, CipherMode.CBC, PaddingMode.None))
                {
                    _aes.CreateDecryptor(_aes.Key, _aes.IV).TransformBlock(input, 0, input.Length, output, 0);
                }
                break;

            case AesMode.CTR:
                using (var _aes = new AesCtr(ctr_iv_nonce))
                {
                    _aes.CreateDecryptor(key).TransformBlock(input, 0, input.Length, output, 0);
                }
                break;

            case AesMode.ECB:
                using (var _aes = CreateAes(key, ctr_iv_nonce, CipherMode.ECB, PaddingMode.None))
                {
                    _aes.CreateDecryptor(_aes.Key, _aes.IV).TransformBlock(input, 0, input.Length, output, 0);
                }
                break;
            }

            return(output);
        }
Exemplo n.º 2
0
        public void Decrypt(Stream inStream, Stream outStream, long count)
        {
            byte[] key          = (byte[])(NormalKeys[Slot].Clone());
            byte[] ctr_iv_nonce = new byte[0x10];
            CTR_IV_NONCE.CopyTo(ctr_iv_nonce, 0);
            switch (Mode)
            {
            case AesMode.NotSupported:
                throw new NotSupportedException();

            case AesMode.CBC:
                using (var _aes = CreateAes(key, ctr_iv_nonce, CipherMode.CBC, PaddingMode.None))
                {
                    using (var decryptor = _aes.CreateDecryptor(_aes.Key, _aes.IV))
                    {
                        byte[] inBuf;
                        byte[] outBuf;
                        while (count > 0)
                        {
                            inBuf  = new byte[count > 0x100000 ? 0x100000 : count];
                            outBuf = new byte[count > 0x100000 ? 0x100000 : count];
                            inStream.Read(inBuf, 0, inBuf.Length);
                            decryptor.TransformBlock(inBuf, 0, inBuf.Length, outBuf, 0);
                            outStream.Write(outBuf, 0, outBuf.Length);
                            count -= inBuf.Length;
                        }
                    }
                }
                break;

            case AesMode.CTR:
                using (var _aes = new AesCtr(ctr_iv_nonce))
                {
                    using (var decryptor = _aes.CreateDecryptor(key))
                    {
                        byte[] inBuf;
                        byte[] outBuf;
                        while (count > 0)
                        {
                            inBuf  = new byte[count > 0x100000 ? 0x100000 : count];
                            outBuf = new byte[count > 0x100000 ? 0x100000 : count];
                            inStream.Read(inBuf, 0, inBuf.Length);
                            decryptor.TransformBlock(inBuf, 0, inBuf.Length, outBuf, 0);
                            outStream.Write(outBuf, 0, outBuf.Length);
                            count -= inBuf.Length;
                        }
                    }
                }
                break;

            case AesMode.ECB:
                using (var _aes = CreateAes(key, ctr_iv_nonce, CipherMode.ECB, PaddingMode.None))
                {
                    using (var decryptor = _aes.CreateDecryptor(key, _aes.IV))
                    {
                        byte[] inBuf;
                        byte[] outBuf;
                        while (count > 0)
                        {
                            inBuf  = new byte[count > 0x100000 ? 0x100000 : count];
                            outBuf = new byte[count > 0x100000 ? 0x100000 : count];
                            inStream.Read(inBuf, 0, inBuf.Length);
                            decryptor.TransformBlock(inBuf, 0, inBuf.Length, outBuf, 0);
                            outStream.Write(outBuf, 0, outBuf.Length);
                            count -= inBuf.Length;
                        }
                    }
                }
                break;
            }
        }