this a simple interface for the command line encryptor GPG, and can be used to sign and encrypt attachments
Imports System.IO
Imports System.Diagnostics
Module modEcryption
Friend Function Encrypt(ByVal Filename As String, _
ByVal KeyName As String, _
Optional ByVal OutputFile As String = "", _
Optional ByVal Sign As String = "", _
Optional ByVal Armoured As Boolean = False _
) As String
'declare the command arguments and the comand operator
Dim args As String = ""
Dim comop As String = ""
'if producing a Armoured Test file add the armoured option
If Armoured Then
comop &= "a"
End If
'if the file is to be signed then add the sign command and the password
If Not Sign = "" Then
comop &= "s"
args &= " --passphrase " & """" & Sign & """"
End If
'if the output file ahas been specified add it to the command
If Not OutputFile = "" Then
args &= " -o " & """" & OutputFile & """"
End If
'add the encrypt command to command operator
comop &= "e"
'create the command arguments
args &= " -r """ & KeyName & """ -" & comop & " """ & Filename & """"
'create a process and configure it so that output is captured by VB
Dim pro As New System.Diagnostics.Process
pro.StartInfo = New ProcessStartInfo("C:\Program Files\GNU\GnuPG\gpg.exe", args)
pro.StartInfo.UseShellExecute = False
pro.StartInfo.RedirectStandardOutput = True
pro.StartInfo.RedirectStandardError = True
'Encrypt the document
pro.Start()
pro.WaitForExit()
'if it returned errors
args = pro.StandardError().ReadToEnd
If Not args = "" Then
Throw New System.Security.Cryptography.CryptographicException(args)
End If
'dump the output and return it for processing to the parent process
args = pro.StandardOutput().ReadToEnd
Return args
End Function
Friend Function Decrypt(ByVal Filename As String, _
ByVal Password As String, _
Optional ByVal OutputFile As String = "" _
) As String
'create the cammand arguments
Dim args As String = ""
'add the password
args &= " --passphrase " & Password
'if an output file is provided add it
If Not OutputFile = "" Then
args &= " -o " & OutputFile
End If
'add the decryppt command
args &= " -d " & Filename
'create a process and configure it so that output is captured by VB
Dim pro As New System.Diagnostics.Process
pro.StartInfo = New ProcessStartInfo("C:\Program Files\GNU\GnuPG\gpg.exe", args)
pro.StartInfo.UseShellExecute = False
pro.StartInfo.RedirectStandardOutput = True
pro.StartInfo.RedirectStandardError = True
'Encrypt the document
pro.Start()
pro.WaitForExit()
'if it returned errors
args = pro.StandardError().ReadToEnd
If Not args = "" Then
Throw New System.Security.Cryptography.CryptographicException(args)
End If
'dump the output and return it for processing to the parent process
args = pro.StandardOutput().ReadToEnd
Return args
End Function
End Module
Hells_Weapon [ Hells_Weapon ( at ) yahoo dot co dot uk ],
2007-04-12 06:57:10
#