Exemplo n.º 1
0
        public void Encrypt(EncryptionRequest req)
        {
            string cmdArgs;

            try
            {
                cmdArgs = req.Execute();
                using (System.IO.FileStream fsEncryptFile = new System.IO.FileStream(this.fullPathToPgpRoot + "\\" + req.File.Name, System.IO.FileMode.Create))
                    using (System.IO.FileStream fsPublicKeyFile = new System.IO.FileStream(this.fullPathToPgpRoot + "\\" + req.CryptionArgs.PublicKey.Name, System.IO.FileMode.Create))
                    {
                        fsEncryptFile.Write(req.File.Content, 0, req.File.Content.Length);
                        fsPublicKeyFile.Write(req.CryptionArgs.PublicKey.Content, 0, req.CryptionArgs.PublicKey.Content.Length);
                    }
            }
            catch (Exception)
            {
                throw;
            }

            if (this.ExecuteGnuGpg(cmdArgs))
            {
                System.IO.FileInfo encryptedFile = new System.IO.FileInfo(String.Format("{0}\\{1}", this.fullPathToPgpRoot, req.File.Name + ".gpg"));
                if (encryptedFile.Exists)
                {
                    var callback = this.clientCallback;
                    if (callback != null)
                    {
                        byte[] fileContent = System.IO.File.ReadAllBytes(encryptedFile.FullName);
                        callback.OnFileEncrypted(new FileCryptionEvent {
                            File = new File(encryptedFile.Name, fileContent), ExecutedCommand = cmdArgs
                        });
                    }
                }
            }
        }