Exemple #1
0
        public async Task <bool> UploadFileAndEncryptAsync(FileCrypt fileCrypt, Stream stream)
        {
            fileCrypt.FileCryptId = await this._fileCryptRepository.SaveFileDetailsForEncryptionAsync(fileCrypt);

            Assembly asm                   = Assembly.GetExecutingAssembly();
            var      memoryStream          = new MemoryStream();
            var      memoryStreamEncrypted = new MemoryStream();
            string   tempFilePath          = "";

            fileCrypt.EncryptedFileName     = "FileEncrypt_" + fileCrypt.FileCryptId.ToString();
            fileCrypt.EncryptedFileFullPath = fileCrypt.EncryptedFilePath + fileCrypt.EncryptedFileName + fileCrypt.EncryptedFileExtension;
            tempFilePath = fileCrypt.EncryptedFilePath + fileCrypt.EncryptedFileName + ".temp";
            string path = System.IO.Path.GetDirectoryName(asm.Location);
            string privateKeyFileFullPath = path + @"\" + GlobalAppConfigurations.Instance.GetValue("PGPPrivateKeyFileFullPath").ToString();
            string publicKeyFileFullPath  = path + @"\" + GlobalAppConfigurations.Instance.GetValue("PGPPublicKeyFileFullPath").ToString();
            string pgpKeyPassword         = GlobalAppConfigurations.Instance.GetValue("PGPKeyPassword").ToString();

            // Write File from stream to Byte
            await stream.CopyToAsync(memoryStream);

            byte[] fileInByte = memoryStream.ToArray();

            // Save Encrytped File byte to File
            using (var fileStream = new FileStream(tempFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                using (var memoryStreamToFile = new MemoryStream(fileInByte))
                {
                    byte[] bytes = new byte[memoryStreamToFile.Length];
                    await memoryStreamToFile.ReadAsync(bytes, 0, (int)memoryStreamToFile.Length);

                    await fileStream.WriteAsync(bytes, 0, bytes.Length);
                }

            using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
            {
                try
                {
                    await pgp.EncryptFileAsync(tempFilePath,
                                               fileCrypt.EncryptedFileFullPath,
                                               publicKeyFileFullPath,
                                               true, true);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            if (File.Exists(tempFilePath))
            {
                File.Delete(tempFilePath);
            }

            bool isUpdateSuccess = await this._fileCryptRepository.UpdateFileDetailsAfterEncryptionAsync(fileCrypt);

            return(isUpdateSuccess);
        }
Exemple #2
0
        private async void btnEncrypt_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ChoPGPEncryptDecrypt pgp = PGPOptions.NewPGPEncryptDecrypt();
                await pgp.EncryptFileAsync(PGPEncryptFileOptions.InputFilePath, PGPEncryptFileOptions.OutputFilePath, PGPEncryptFileOptions.PublicKeyFilePath, PGPEncryptFileOptions.Armor, PGPEncryptFileOptions.IntegrityCheck);

                MessageBox.Show("PGP Encryption successful.", Title, MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error occurred during PGP encryption. " + ex.Message, Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }