Example #1
0
        public CryptoContainer ValidateDecrypt(CryptoRequest request)
        {
            CryptoContainer container = null;

            if (!request.SkipValidations)
            {
                container = CryptoContainer.CreateForDecryption(request);
                var validationResult = container.ReadAndValidateDataForDecryption();
                if (!validationResult.IsValid)
                {
                    throw validationResult.ExceptionToThrow ?? new Exception("Unknown error");
                }
                request.IV = container.GetIV();
                if (request.Password != null)
                {
                    request.Key = container.CalculateKey();
                }
            }

            if (request.Key == null || request.Key.Length != 32)
            {
                throw new ArgumentException("Key must be 32 bytes long.");
            }
            if (request.IV == null || request.IV.Length != 16)
            {
                throw new ArgumentException($"IV must be 16 bytes in length");
            }

            return(container);
        }
Example #2
0
        internal static void Decrypt(CryptoRequest request)
        {
            CryptoContainer container = null;

            if (!request.SkipValidations)
            {
                container = CryptoContainer.CreateForDecryption(request);
                var validationResult = container.ReadAndValidateDataForDecryption();
                if (!validationResult.IsValid)
                {
                    throw validationResult.ExceptionToThrow ?? new Exception("Unknown error");
                }
                request.IV = container.GetIV();
                if (request.Password != null)
                {
                    request.Key = container.CalculateKey();
                }
            }

            if (request.Key == null || request.Key.Length != 32)
            {
                throw new ArgumentException("Key must be 32 bytes long.");
            }
            if (request.IV == null || request.IV.Length != 16)
            {
                throw new ArgumentException($"IV must be 16 bytes in length");
            }

            using (var aes = new AesManaged())
            {
                aes.IV      = request.IV;
                aes.Key     = request.Key;
                aes.Padding = PaddingMode.ISO10126;
                if (request.SkipValidations)
                {
                    aes.Padding = PaddingMode.PKCS7;
                }
                aes.BlockSize = 128;
                using (var decryptor = aes.CreateDecryptor())
                {
                    CryptoStream cs         = new CryptoStream(request.OutData, decryptor, CryptoStreamMode.Write);
                    int          bufferSize = aes.BlockSize;
                    byte[]       buffer     = new byte[bufferSize];
                    int          read       = 0;
                    while ((read = request.InData.Read(buffer, 0, bufferSize)) > 0)
                    {
                        cs.Write(buffer, 0, read);
                        cs.Flush();
                    }
                    cs.FlushFinalBlock();
                }
            }
        }