【问题标题】:decrypt pgp files from a folder and moving it - c#从文件夹中解密 pgp 文件并移动它 - c#
【发布时间】:2016-07-28 20:57:08
【问题描述】:

我正在尝试从某个位置解密 .pgp 文件,然后将这些文件移动到另一个位置。我查看了这个article 并相应地编码。在我的代码中,我正在开发一个应用程序,它将在每 100 秒后检查到某个位置,如果有文件,那么它将解密并移动。但我得到了这个例外The process cannot access the file 'c:\file.pgp' because it is being used by another process.

这是我调用从那篇文章中复制的类的代码。

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);
            }
        }        
    }

【问题讨论】:

    标签: c# encryption bouncycastle public-key-encryption pgp


    【解决方案1】:

    你在哪里得到错误。如果您在移动时遇到错误,可能是因为您的文件流未关闭。解密后移动前用 fs.Close(); 关闭文件流

    【讨论】:

    • 我在这条线上遇到了异常FileStream fs = File.Open(s, FileMode.Open);
    【解决方案2】:

    我相信您遇到的问题是由于文件未关闭引起的,当您使用 foreach 循环进行循环时,第一次迭代可能有效。不过下一次,因为从来没关过,所以还在用。

    尝试添加

    fs.Close();
    

    在foreach循环结束时

    【讨论】:

    • fs.Close() 不起作用,因为我在 FileStream fs = File.Open(s, FileMode.Open); 上遇到了这个异常
    • 您可能还需要在fs.Close();之前添加FileClose();
    • 之前没有要关闭的文件
    【解决方案3】:

    这是我最终的结果,它正在工作

    //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)
                {
    
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多