GPG Encryption in .NET
The following VB.NET code may be used in conjunction with GPG4Win to GPG encrypt a file using a .NET web application prior to sending to a specified recipient. This approach calls the GPG4Win command line utility to import a key and encrypt a file via the System.Diagnostics namespace.
This code works with GPG4Win version 3.1.10.
- First, download and install GPG4Win on the server where this code will run
- You may need to add an Imports System.Diagnostics and/or System.IO statement(s) at top of page.
- The directory where you write the file will have to have the correct permissions based on your application pool settings.
- Your application pool user must have write permission on the target directory.
- Be sure your source directory is private/not exposed to the web.
- This produces an encrypted copy of the original file with .gpg appended.
- Be sure to delete the unencrypted version (if needed) once the encryption is done.
Dim strFileFolder as String = "C:\Files\" 'The directory of the file to be encrypted - be sure this is a private directory
Dim strFileName as String = "MySpreadSheet.csv" 'The name of the file to be encrypted
Dim strKeyFile as String = "C:\Keys\KEYFILE.asc" 'The complete path to the public key file provided by the recipient.
Dim strRecipient as String = "RecipientName" 'should match the name or email in the GPG4Win Keychain entry
Dim strFilePath as String = strFileFolder & strFileName '
Dim gpgImportError As String = ""
Dim gpgEncryptError As String = ""
'call the gpg4win process
Dim gpg = New Diagnostics.Process()
gpg.StartInfo.FileName = "gpg"
gpg.StartInfo.UseShellExecute = False
gpg.StartInfo.RedirectStandardError = True
'set import key location args
gpg.StartInfo.Arguments = " --import " & strKeyFile
'import the key
gpg.Start()
gpgImportError = gpg.StandardError.ReadToEnd()
gpg.WaitForExit()
'show confirmation or output if there is an error
If gpgImportError <> "" Then
Response.Write("GPG IMPORT ERROR - " & gpgImportError & "|")
Else
Response.Write("OK - KEY IMPORTED|")
End If
'set encryption args --trust-model always ensure no user feedback/prompt.
'Use this setting with caution, and only if you are certain of the integrity of the key
'since no output is specified, it will encrypt into the same directory and append a .gpg
Dim strArgs = "--encrypt --trust-model always --recipient " & strRecipient & " " & strFilePath
'Encrypt the file.
gpg.StartInfo.Arguments = strArgs
gpg.Start()
gpgEncryptError = gpg.StandardError.ReadToEnd()
gpg.WaitForExit()
'output the args - to call from a command line, call gpg with these args
Response.Write("gpg " & strArgs & "|")
If gpgEncryptError <> "" Then
Response.Write("GPG ENCRYPT ERROR - " & gpgEncryptError & "|")
Else
Response.Write("OK - ENCRYPTED|")
End If
Response.Write("ENCRYPT COMPLETED AT: " & DateTime.Now() & "|")
gpg = Nothing
'uncomment below to delete the unencrypted source file
'IO.File.Delete(strFilePath)
'Response.Write("SOURCE FILE DELETED|")
'You should be left with just the encrypted gpg file like "C:\Files\MySpreadSheet.csv.gpg"
|