locked
Encrypting with GPG4Win RRS feed

  • Question

  • User1122355199 posted

    Hello everyone and thanks for your help in advance.  I need to generate an encrypted file from a webpage using PGP.  I installed GPG4Win on the production server and have it working correctly for encryption/decryption through the Kleopatra interface.  I implemented a solution to decrypt the files using a wrapper for command line interface based on an article found at :  http://www.consolidata.co.uk/technical/decrypting-files-with-gpg4win-using-c/.  I need to implement a solution to encrypt the files, but am running into problems.  I need to encrypt the file using a trading partners public key which I have.  I tried the following:

            Dim gpgImportError As String = ""
            Dim gpgEncryptError As String = ""
    
            Dim keyPath As String = "E:\PublicKey.asc"
    
            ' Create a process to run Gpg4win.
            Dim gpg = New Process()
            gpg.StartInfo.FileName = "gpg2"
            gpg.StartInfo.UseShellExecute = False
            gpg.StartInfo.RedirectStandardError = True
    
            ' Import the key.
            gpg.StartInfo.Arguments = Convert.ToString(" --import ") & keyPath
    
            gpg.Start()
            gpgImportError = gpg.StandardError.ReadToEnd()
            gpg.WaitForExit()
    
            ' Encrypt the file.
            gpg.StartInfo.Arguments = "gpg2 --encrypt " & Chr(34) & "E:\infile.txt" & Chr(34) & " --output " & Chr(34) & "E:\outfile.txt.gpg" & Chr(34)
    
            gpg.Start()
            gpgEncryptError = gpg.StandardError.ReadToEnd()
            gpg.WaitForExit()

    But this does not seem to be working.  I also know of an article http://www.codeproject.com/Articles/457453/PGP-Encryption-with-Csharp that implements another third party software, but I haven't tried this yet since I'm not exactly sure what I am doing.  Any help would be appreciated.

    Saturday, December 19, 2015 11:03 PM

Answers

  • User1122355199 posted

    So I figured it out.  The correct code is:

            Dim gpg = New Process()
            gpg.StartInfo.FileName = "gpg2"
            gpg.StartInfo.UseShellExecute = False
            gpg.StartInfo.RedirectStandardError = True
    
            'Encrypt the file.
            gpg.StartInfo.Arguments = "--encrypt --trust-model always --recipient recipientonpublickey filetobeencrypted"
    
            gpg.Start()
            gpgImportError = gpg.StandardError.ReadToEnd()
            gpg.WaitForExit()

    The --trust-model always switch is used to override the message "There is no assurance this key belongs to the names user" message requiring keyboard intervention with a Y/N selection.  Obviously, you need to be certain of the sender.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 20, 2015 2:54 AM