private void EncryptedDownload(Uri downloadUri, PlainFileKey plainFileKey)
        {
            FileDecryptionCipher cipher;

            try {
                cipher = Crypto.Sdk.Crypto.CreateFileDecryptionCipher(plainFileKey);
            } catch (CryptoException ce) {
                string message = "Creation of decryption engine for encrypted download " + ActionId + " failed!";
                DracoonClient.Log.Debug(Logtag, message);
                throw new DracoonCryptoException(CryptoErrorMapper.ParseCause(ce), ce);
            }

            try {
                ProgressReportTimer = Stopwatch.StartNew();
                long downloadedByteCount = 0;
                while (downloadedByteCount < AssociatedNode.Size.GetValueOrDefault(0))
                {
                    byte[] chunk = DownloadChunk(downloadUri, downloadedByteCount, AssociatedNode.Size.GetValueOrDefault(0));
                    EncryptedDataContainer encryptedContainer = new EncryptedDataContainer(chunk, null);
                    PlainDataContainer     plainContainer     = cipher.ProcessBytes(encryptedContainer);
                    OutputStream.Write(plainContainer.Content, 0, plainContainer.Content.Length);
                    downloadedByteCount += chunk.Length;
                }

                byte[] encryptionTag = Convert.FromBase64String(plainFileKey.Tag);
                EncryptedDataContainer tagContainer   = new EncryptedDataContainer(null, encryptionTag);
                PlainDataContainer     finalContainer = cipher.DoFinal(tagContainer);
                OutputStream.Write(finalContainer.Content, 0, finalContainer.Content.Length);
                if (LastNotifiedProgressValue != downloadedByteCount)
                {
                    // Notify 100 percent progress
                    NotifyProgress(ActionId, downloadedByteCount, AssociatedNode.Size.GetValueOrDefault(0));
                }
            } catch (CryptoException ce) {
                const string message = "Decryption of file failed while downloading!";
                DracoonClient.Log.Debug(Logtag, message);
                throw new DracoonFileIOException(message, ce);
            } catch (IOException ioe) {
                if (IsInterrupted)
                {
                    throw new ThreadInterruptedException();
                }

                const string message = "Write to stream failed!";
                DracoonClient.Log.Debug(Logtag, message);
                throw new DracoonFileIOException(message, ioe);
            } finally {
                ProgressReportTimer.Stop();
            }
        }
        private void EncryptedUpload(ref PlainFileKey plainFileKey)
        {
            DracoonClient.Log.Debug(LogTag, "Uploading file [" + FileUploadRequest.Name + "] in encrypted proxied way.");
            FileEncryptionCipher cipher;

            try {
                cipher = Crypto.Sdk.Crypto.CreateFileEncryptionCipher(plainFileKey);
            } catch (CryptoException ce) {
                string message = "Creation of encryption engine for encrypted upload " + ActionId + " failed!";
                DracoonClient.Log.Debug(LogTag, message);
                throw new DracoonCryptoException(CryptoErrorMapper.ParseCause(ce));
            }

            try {
                long   uploadedByteCount = 0;
                byte[] buffer            = new byte[DracoonClient.HttpConfig.ChunkSize];
                int    bytesRead         = 0;
                while ((bytesRead = InputStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    EncryptedDataContainer encryptedContainer = EncryptChunk(cipher, bytesRead, buffer, false);
                    ProcessEncryptedChunk(new Uri(UploadToken.UploadUrl), encryptedContainer, uploadedByteCount, cipher, false);
                    uploadedByteCount += encryptedContainer.Content.Length;
                }

                EncryptedDataContainer finalEncryptedContainer = EncryptChunk(cipher, bytesRead, buffer, true);
                plainFileKey.Tag   = ProcessEncryptedChunk(new Uri(UploadToken.UploadUrl), finalEncryptedContainer, uploadedByteCount, cipher, true);
                uploadedByteCount += finalEncryptedContainer.Content.Length;
                if (LastNotifiedProgressValue != uploadedByteCount)
                {
                    // Notify 100 percent progress
                    NotifyProgress(ActionId, uploadedByteCount, OptionalFileSize);
                }
            } catch (IOException ioe) {
                if (IsInterrupted)
                {
                    throw new ThreadInterruptedException();
                }

                string message = "Read from stream failed!";
                DracoonClient.Log.Debug(LogTag, message);
                throw new DracoonFileIOException(message, ioe);
            } finally {
                ProgressReportTimer?.Stop();
            }
        }
 private void OnDbInitializationComplete()
 {
     if (InvokeRequired)
     {
         BeginInvoke(new MethodInvoker(OnDbInitializationComplete));
     }
     else
     {
         ProgressReportTimer.Stop();
         UpdateCurrentProgress(1, 1);
         ProgressStatusLabel.Visible = false;
         label2.Visible = false;
         if (CTS.IsCancellationRequested)
         {
             InitializationStepLabel.Text = "Intialization cancelled";
         }
         else
         {
             InitializationStepLabel.Text = "Intialization complete";
         }
     }
 }
        private List <ApiS3FileUploadPart> EncryptedS3Upload(ref PlainFileKey plainFileKey)
        {
            DracoonClient.Log.Debug(LogTag, "Uploading file [" + FileUploadRequest.Name + "] via encrypted s3 direct upload.");
            FileEncryptionCipher cipher;

            try {
                cipher = Crypto.Sdk.Crypto.CreateFileEncryptionCipher(plainFileKey);
            } catch (CryptoException ce) {
                DracoonClient.Log.Debug(LogTag, "Creation of encryption engine for encrypted upload " + ActionId + " failed!");
                throw new DracoonCryptoException(CryptoErrorMapper.ParseCause(ce));
            }

            try {
                int    chunkSize = DefineS3ChunkSize();
                int    s3UrlBatchSize = DefineS3BatchSize(chunkSize);
                long   uploadedByteCount = 0;
                byte[] buffer = new byte[chunkSize];
                int    bytesRead, offset = 0;

                while ((bytesRead = InputStream.Read(buffer, offset, buffer.Length - offset)) >= 0)
                {
                    int    nextByte = InputStream.ReadByte(); // Check if further bytes are available
                    byte[] chunk;

                    EncryptedDataContainer container = EncryptChunk(cipher, bytesRead + offset, buffer, false);
                    if (nextByte == -1)
                    {
                        // It is the last block
                        EncryptedDataContainer finalContainer = EncryptChunk(cipher, bytesRead + offset, buffer, true);
                        plainFileKey.Tag = Convert.ToBase64String(finalContainer.Tag);
                        chunk            = new byte[container.Content.Length + finalContainer.Content.Length];
                        Buffer.BlockCopy(finalContainer.Content, 0, chunk, container.Content.Length, finalContainer.Content.Length);
                    }
                    else
                    {
                        chunk = new byte[container.Content.Length];
                    }

                    Buffer.BlockCopy(container.Content, 0, chunk, 0, container.Content.Length);

                    if (chunk.Length < chunkSize)
                    {
                        S3Urls = RequestS3Urls(S3Parts.Count + 1, 1, chunk.Length);
                    }
                    else if (S3Urls.Count == 0)
                    {
                        S3Urls = RequestS3Urls(S3Parts.Count + 1, s3UrlBatchSize, chunkSize);
                    }

                    string partETag = UploadS3ChunkWebClient(S3Urls.Dequeue(), chunk, uploadedByteCount);
                    S3Parts.Add(new ApiS3FileUploadPart()
                    {
                        PartEtag   = partETag,
                        PartNumber = S3Parts.Count + 1
                    });
                    uploadedByteCount += chunk.Length;

                    if (nextByte != -1)
                    {
                        // Do it every time if the current block isn't the last
                        Buffer.SetByte(buffer, 0, (byte)nextByte);
                        offset = 1;
                    }
                    else
                    {
                        break;
                    }
                }

                if (LastNotifiedProgressValue != uploadedByteCount)
                {
                    // Notify 100 percent progress
                    NotifyProgress(ActionId, uploadedByteCount, OptionalFileSize);
                }

                return(S3Parts);
            } catch (IOException ioe) {
                if (IsInterrupted)
                {
                    throw new ThreadInterruptedException();
                }

                string message = "Read from stream failed!";
                DracoonClient.Log.Debug(LogTag, message);
                throw new DracoonFileIOException(message, ioe);
            } finally {
                ProgressReportTimer?.Stop();
            }
        }