Esempio n. 1
0
        internal static void Encrypt(CryptoRequest request)
        {
            if (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");
            }

            CryptoContainer container = null;

            if (!request.SkipValidations)
            {
                container = CryptoContainer.CreateForEncryption(request);
                container.WriteEmptyHeaderData();
            }

            using (var aes = new AesManaged())
            {
                aes.IV        = request.IV;
                aes.Key       = request.Key;
                aes.Padding   = PaddingMode.ISO10126;
                aes.BlockSize = 128;
                if (request.SkipValidations)
                {
                    aes.Padding = PaddingMode.PKCS7;
                }
                using (var encryptor = aes.CreateEncryptor())
                {
                    CryptoStream cs         = new CryptoStream(request.OutData, encryptor, 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();
                }
            }
            if (!request.SkipValidations)
            {
                container.WriteChecksAndEmbeddedData();
            }
        }
Esempio n. 2
0
        internal static void Encrypt(CryptoRequest request)
        {
            CryptoContainer            container = request.ValidateEncryption();
            ReportAndCancellationToken token     = request.Token ?? new ReportAndCancellationToken();

            token.CanReportProgress = request.InData.CanSeek;

            using (var aes = GetAes())
                using (var encryptor = GetEncryptorAndSetAes(aes, request))
                {
                    int bufferSize = aes.BlockSize;
                    if (token.CanReportProgress ?? false)
                    {
                        token.NumberOfIterations = (int)Math.Ceiling(request.InData.Length / (double)bufferSize);
                    }
                    CryptoStream cs             = new CryptoStream(request.OutData, encryptor, CryptoStreamMode.Write);
                    byte[]       buffer         = new byte[bufferSize];
                    int          read           = 0;
                    int          iterationCount = 0;
                    while ((read = request.InData.Read(buffer, 0, bufferSize)) > 0)
                    {
                        cs.Write(buffer, 0, read);
                        cs.Flush();

                        if (token.IsCanceled)
                        {
                            break;
                        }
                        iterationCount++;
                        token.ReportProgressInternal(iterationCount);
                    }
                    if (token.IsCanceled)
                    {
                        return;
                    }
                    cs.FlushFinalBlock();
                }
            if (!request.SkipValidations)
            {
                container.WriteChecksAndEmbeddedData();
            }
        }