Beispiel #1
0
        private static async Task <(MemoryStream output, IMS2SizeHeader size)> InternalGetEncryptionStreamAsync(Stream input, long inputSize, IMultiArray key, IMultiArray iv, long headerSize)
        {
            var cipher   = CipherUtilities.GetCipher("AES/CTR/NoPadding");
            var keyParam = ParameterUtilities.CreateKeyParameter("AES", key[inputSize]);

            cipher.Init(true, new ParametersWithIV(keyParam, iv[inputSize]));

            return(await InternalGetEncryptionStreamAsync(input, inputSize, cipher, headerSize).ConfigureAwait(false));
        }
Beispiel #2
0
        private static async Task <MemoryStream> InternalGetDecryptionStreamAsync(Stream input, IMS2SizeHeader size, IMultiArray key, IMultiArray iv, bool zlibCompressed)
        {
            var cipher   = CipherUtilities.GetCipher("AES/CTR/NoPadding");
            var keyParam = ParameterUtilities.CreateKeyParameter("AES", key[(uint)size.CompressedSize]);

            cipher.Init(true, new ParametersWithIV(keyParam, iv[(uint)size.CompressedSize]));

            return(await InternalGetDecryptionStreamAsync(input, size, cipher, zlibCompressed).ConfigureAwait(false));
        }
Beispiel #3
0
        public static async Task <(MemoryStream output, IMS2SizeHeader size)> GetEncryptionStreamAsync(Stream input, long inputSize, IMultiArray key, IMultiArray iv, bool zlibCompress)
        {
            if (zlibCompress)
            {
                using var ms = new MemoryStream();
                using (var z = new ZlibStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression, true))
                {
                    byte[] inputBytes = new byte[inputSize];
                    int    read       = await input.ReadAsync(inputBytes, 0, (int)inputSize).ConfigureAwait(false);

                    if (read != inputSize)
                    {
                        throw new EndOfStreamException();
                    }
                    await z.WriteAsync(inputBytes, 0, (int)inputSize).ConfigureAwait(false);
                }

                ms.Position = 0;
                return(await InternalGetEncryptionStreamAsync(ms, ms.Length, key, iv, inputSize).ConfigureAwait(false));
            }

            return(await InternalGetEncryptionStreamAsync(input, inputSize, key, iv, inputSize).ConfigureAwait(false));
        }
Beispiel #4
0
        public static async Task <MemoryStream> GetDecryptionStreamAsync(Stream input, IMS2SizeHeader size, IMultiArray key, IMultiArray iv, bool zlibCompressed)
        {
            using var ms = new MemoryStream();

            byte[] encodedBytes = new byte[size.EncodedSize];
            await input.ReadAsync(encodedBytes, 0, encodedBytes.Length).ConfigureAwait(false);

            var encoder = new Base64Encoder();

            encoder.Decode(encodedBytes, 0, encodedBytes.Length, ms);
            if (ms.Length != size.CompressedSize)
            {
                throw new ArgumentException("Compressed bytes from input do not match with header size.", nameof(input));
            }

            ms.Position = 0;
            return(await InternalGetDecryptionStreamAsync(ms, size, key, iv, zlibCompressed).ConfigureAwait(false));
        }