How-to send PGP encrypted emails (Pretty Good Privacy) This is something I hacked a while back for some PGP encrypted emails that I needed to process. Its working nicely for me. I know its ugly but its damn unique! I haven't seen anything out there that does what this code does. I hope you can use it somehow as I'm sure I wasn't the only one looking for an easy way to decrypt/encrypt PGP.
This is the following example uses the DOS based PGP encryption/decryption program availble from http://www.pgpi.org/ Configure the software with your PGP key by following the directions in the included files.
A self made component or a third part component like ASPExec from http://www.serverobjects.com can be used to execute the PGP decryption execututable.
An example VB component to do this task would contain the following code in a Class Module.
Dim sTest, i As Integer Dim sCommand As String Dim sPGPTXT As String
Public Property Let Command(temp As Variant) sCommand = temp End Property Public Property Get Command() As Variant Command = sCommand End Property Public Property Let PGPTXT(temp As Variant) sPGPTXT = temp End Property Public Property Get PGPTXT() As Variant PGPTXT = sPGPTXT End Property
Public Sub PGPDecrypt() sTest = Shell(sCommand, vbMinimizedNoFocus) sTest = "" i = 0 Do While Not sTest = "Exit" i = i + 1 If i > 30 Then sTest = "Exit" End If If GetFileAttributes(sPGPTXT) <> -1 Then sTest = "Exit" Sleep (500)
Loop End Sub
To call this component from within ASP:
Set oFS = CreateObject("Scripting.FileSystemObject") Response.Write "Decyrypting" Set oPGP = Server.CreateObject("NWCRPGP.PGP") oPGP.Command = "C:\inetpub\wwwroot\pgp\pgp.bat" oPGP.PGPTXT = "C:\Inetpub\wwwroot\pgp\ASP.TXT" Response.Write ".." If oFS.FileExists("C:\Inetpub\wwwroot\pgp\asp.txt") Then Set oFSTemp = oFS.GetFile("C:\Inetpub\wwwroot\pgp\asp.txt") oFSTemp.Delete Set oFSTemp = Nothing Response.Write "." End If If oFS.FileExists("C:\Inetpub\wwwroot\pgp\asp.pgp") Then Set oFSTemp = oFS.GetFile("C:\Inetpub\wwwroot\pgp\asp.pgp") oFSTemp.Delete Set oFSTemp = Nothing Response.Write "." End If Set oFSTemp = oFS.CreateTextFile("C:\Inetpub\wwwroot\pgp\asp.pgp", True) Response.Write ".." oFSTemp.Write sTemp oFSTemp.Close Set oFSTemp = Nothing Response.Write ".." oPGP.PGPDecrypt Response.Write "...." Set oFSTemp = oFS.OpenTextFile("C:\Inetpub\wwwroot\pgp\ASP.TXT") Response.Write oFSTemp.ReadAll oFSTemp.Close Set oFSTemp = Nothing Set oFSTemp = oFS.GetFile("C:\Inetpub\wwwroot\pgp\asp.txt") oFSTemp.Delete Set oFSTemp = Nothing Set oFSTemp = oFS.GetFile("C:\Inetpub\wwwroot\pgp\asp.pgp") oFSTemp.Delete Set oFSTemp = Nothing Set oFS = Nothing Set oPGP = Nothing
Contained in the pgp.bat file is: c: CD \inetpub\wwwroot\pgp pgp -v -o asp.txt -z password asp.pgp
This code is in no way in a condition for production use.
Things to think about:
Decrypting to a file is insecure because your unencrypted message will be saved, even if temporarily, to the HD which could allow access to it. Sending the PGP encrypted message directly and receiving to/from the program would be much more secure. If multiple instances of this ran they would conflict since its using the same file names.