Search code examples
c#encryptionbouncycastlepublic-key-encryptionpgp

decrypt pgp files from a folder and moving it - c#


I am trying to decrypt .pgp files from a location and then moving those files to another location. I looked into this article and code accordingly. In my code I am developing an application which will check to a certain location after every 100 seconds and if there are files then it will decrypt and move. but I am getting this exception The process cannot access the file 'c:\file.pgp' because it is being used by another process.

Here is my code where I am calling that class which I copied from that article.

private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        //Do the stuff you want to be done every hour;
        string sourcePath = @"files location";
        string archivePath = @"move original file after decrypting location";
        string targetPath = @"decrypted file location";
        string pubkeyPath = @"public key location\PGPPublicKey.txt";
        string privkeyPath = @"private key location\PGPPrivateKey.txt";

        string fileName = "";
        string destFile = "";

        if (System.IO.Directory.Exists(sourcePath))
        {
            string[] files = System.IO.Directory.GetFiles(sourcePath);

            // Copy the files and overwrite destination files if they already exist.
            foreach (string s in files)
            {
                PGPDecrypt test = new PGPDecrypt(s,
                                         privkeyPath,
                                         "password",
                                         targetPath + "decrypted.txt",
                                         pubkeyPath);
                FileStream fs = File.Open(s, FileMode.Open);
                test.decrypt(fs, targetPath + "decrypted.txt");

                // Use static Path methods to extract only the file name from the path.
                fileName = System.IO.Path.GetFileName(s);
                destFile = System.IO.Path.Combine(archivePath, fileName);
                System.IO.File.Move(s, archivePath);
            }
        }        
    }

Solution

  • This is I ended up and it is working

    //Decrypt
    using DidiSoft.Pgp;
     PGPLib pgp = new PGPLib();
    
                string inputFileLocation = file Location; 
                string privateKeyLocation = @"I posted my private at this location";
                string privateKeyPassword = "Decryption Password";
                string outputFile = @"Output Location";
    
                // decrypt and obtain the original file name
                // of the decrypted file
                string originalFileName =
                  pgp.DecryptFile(inputFileLocation,
                              privateKeyLocation,
                              privateKeyPassword,
                              outputFile);
    //Move decrypted file to archive
    string path = Decrypted file Location;
                string path2 = @"Archive file location" + Path.GetFileName(file); ;
                try
                {
                    if (!File.Exists(path))
                    {
                        // This statement ensures that the file is created,
                        // but the handle is not kept.
                        using (FileStream fs = File.Create(path)) { }
                    }
    
                    // Ensure that the target does not exist.
                    if (File.Exists(path2))
                        File.Delete(path2);
    
                    // Move the file.
                    File.Move(path, path2);
    
                }
                catch (Exception e)
                {
    
                }