Albert Torres
Albert Torres

Reputation: 173

How to get rid of the original file path instead of name in PGP encryption with ChoPGP library?

I tried PGP encrypting a file in ChoPGP library. At the end of the process it shows embedded file name along with the whole original file name path.

But I thought it will show only the filename without the whole path. Which I intend to work on and figure out a way to do so?

Doing the following:

            using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
        {
            pgp.EncryptFile(@"\\appdevtest\c$\appstest\Transporter\test\Test.txt",
                @"\\appdevtest\c$\appstest\Transporter\test\OSCTestFile_ChoPGP.gpg",
                @"\\appdevtest\c$\appstest\Transporter\pgpKey\PublicKey\atorres\atorres_publicKey.asc", 
                true, 
                true);
        }

enter image description here

enter image description here

Upvotes: 0

Views: 410

Answers (2)

Moises Hernandez
Moises Hernandez

Reputation: 11

I had the same problem, and I found this example of using Bouncy Castle's C# API.

And this is my code:

        private static string PGPencrypt(string fileName, string clearData, short supplierId)
    {

        var inputFile = TempPath + fileName;
        File.WriteAllText(inputFile, clearData);

        var outputFile = fileName + ".pgp";
        var encryptionKey = File.ReadAllBytes(Path.Combine(AppPath, suppliers[supplierId]["public_key"]));
        CryptoHelper.EncryptPgpFile(inputFile, outputFile, encryptionKey);
        if (production)
        {
            System.IO.File.Delete(inputFile);
        }

        return TempPath + outputFile;
    }
    private static string PGPdecrypt(string fileName)
    {
        var decryptionKey = File.ReadAllBytes(Path.Combine(AppPath, suppliers[0]["private_key"]));
        string decryptedData = CryptoHelper.DecryptPgpDataFile(TempPath + fileName, decryptionKey, "");

        var txtFilePath = TempPath + Path.GetFileNameWithoutExtension(fileName);
        File.WriteAllText(txtFilePath, decryptedData);

        return txtFilePath;
    }

Upvotes: 0

Nickolay Olshevsky
Nickolay Olshevsky

Reputation: 14160

Looking at this line from the ChoPGP sources: https://github.com/Cinchoo/ChoPGP/blob/7152c7385022823013324408e84cb7e25d33c3e7/ChoPGP/ChoPGPEncryptDecrypt.cs#L221

You may find out that it uses internal function GetFileName, which ends up with this for the FileStream: return ((FileStream)stream).Name;.

And this one, according to documentation, Gets the absolute path of the file opened in the FileStream..

So you should either make fork of ChoPGP and modify this line to extract just filename, or submit a pull request to the ChoPGP. Btw, it's just a wrapper around the BouncyCastle so you may use that instead.

Upvotes: 1

Related Questions