PGP Decode Component
The following code example is the counterpart to
the encoding component shown in the preceding subsection. The Decoding
component is used on the receive side of BizTalk and is used when
encrypted messages are received into BizTalk.
Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Collections
Imports System.Diagnostics
Imports System.Drawing
Imports System.IO
Imports System.Reflection
Imports Microsoft.BizTalk.Component.Interop
Imports ABC.BizTalk.PipelineComponents.PGPUtilities
Namespace Microsoft.Utility.PipelineGnuPG
<ComponentCategory(CategoryTypes.CATID_PipelineComponent),_
ComponentCategory(CategoryTypes.CATID_Decoder),_
System.Runtime.InteropServices.Guid("AEE2E180-8E4F-426d-9E39-C314E09F977E")> _
Public Class PGPDecodeComponent
Implements IBaseComponent, Microsoft.BizTalk.Component.Interop.IComponent,_
Microsoft.BizTalk.Component.Interop.IPersistPropertyBag, IComponentUI
' Component information
#Region "IBaseComponent"
<Browsable(False)> _
Public ReadOnly Property Name() As String
Get
Return "PGP decoder"
End Get
End Property
<Browsable(False)> _
Public ReadOnly Property Version() As String
Get
Return "1.0"
End Get
End Property
<Browsable(False)> _
Public ReadOnly Property Description() As String
Get
Return "PGG Decode Pipeline Component"
End Get
End Property
<Browsable(False)> _
Public ReadOnly Property Icon() As System.IntPtr
Get
Return (CType(resourceManager.GetObject("IconBitmap"),_
Bitmap)).GetHicon()
End Get
End Property
#End Region
Private resourceManager As System.Resources.ResourceManager = New_
System.Resources.ResourceManager("ABC.BizTalk.PipelineComponents",_
System.Reflection.Assembly.GetExecutingAssembly())
' Property: Passphrase
Private _passphrase As String
Public Property Passphrase() As String
Get
Return _passphrase
End Get
Set(ByVal value As String)
_passphrase = value
End Set
End Property
' Property: GnuPGBinDir
Private _PGPBinDirectory As String
Public Property PGPBinDirectory() As String
Get
Return _PGPBinDirectory
End Get
Set(ByVal value As String)
_PGPBinDirectory = value
End Set
End Property
Private Function Decode(ByVal inStream As System.IO.Stream) As Stream
Dim inFile As String = Path.GetTempFileName()
Dim outFile As String = Path.ChangeExtension(inFile, "txt")
Try
DumpStreamToFile(inStream, inFile)
Dim GPG As GnuPGWrapper = New GnuPGWrapper(_PGPBinDirectory)
Dim GPGCommand As GnuPGCommand = GPG.Command
GPGCommand.Command = Commands.Decrypt
GPGCommand.InputFile = inFile
GPGCommand.OutputFile = outFile
GPGCommand.Passphrase = _passphrase
'TODO: support encrypted passphrase, no passphrase is a security
'risk
GPG.Execute(Nothing)
outStream = ReadFileToMemoryStream(outFile)
Catch ex As Exception
System.Diagnostics.Debug.WriteLine(ex)
Throw ex
Finally
If File.Exists(inFile) Then
File.Delete(inFile)
End If
If File.Exists(outFile) Then
File.Delete(outFile)
End If
End Try
Return outStream
End Function
#Region "IPersistPropertyBag Members"
Public Sub InitNew()
End Sub
Public Sub GetClassID(<System.Runtime.InteropServices.Out()> ByRef _
classID As Guid)
classID = New Guid("4FC12033-D0BD-4298-BB31-FBDBA72F5961")
End Sub
Public Sub Load(ByVal propertyBag As IPropertyBag, ByVal _
errorLog As Integer)
Dim text As String
text = CStr(ReadPropertyBag(propertyBag, "Passphrase"))
If Not text Is Nothing Then
_passphrase = text
End If
text = CStr(ReadPropertyBag(propertyBag, "PGPBinDirectory"))
If Not text Is Nothing Then
_PGPBinDirectory = text
End If
End Sub
Public Sub Save(ByVal propertyBag As IPropertyBag, ByVal clearDirty As_
Boolean, ByVal saveAllProperties As Boolean)
Dim val As Object
val = CObj(_passphrase)
WritePropertyBag(propertyBag, "Passphrase", val)
val = CObj(_PGPBinDirectory)
WritePropertyBag(propertyBag, "PGPBinDirectory", val)
End Sub
#End Region
#Region "IComponent Members"
Public Function Execute(ByVal pContext As IPipelineContext, ByVal pInMsg As_
Microsoft.BizTalk.Message.Interop.IBaseMessage) As_
Microsoft.BizTalk.Message.Interop.IBaseMessage
Try
If Not pInMsg Is Nothing Then
Dim originalStream As Stream =_
pInMsg.BodyPart.GetOriginalDataStream()
pInMsg.BodyPart.Data = Decode(originalStream)
pContext.ResourceTracker.AddResource(pInMsg.BodyPart.Data)
End If
Catch ex As Exception
System.Diagnostics.Debug.WriteLine("Exception caught in_
ABC.BizTalk.PipelineComponents.PGPDecodeComponent::Execute:"& ex.Message)
Throw ex
End Try
Return pInMsg
End Function
#End Region
#Region "IComponentUI Members"
'<summary>
'The Validate method is called by the BizTalk Editor during the build
'of a BizTalk project.
'</summary>
'<param name="obj">An Object containing the configuration
properties.</param>
'<returns>The IEnumerator enables the caller to enumerate through a_
'collection of strings containing error messages. These error messages
appear
'as compiler error messages. To report successful property validation, the
'method should return an empty enumerator.</returns>
Public Function Validate(ByVal projectSystem As Object) As IEnumerator
' example implementation:
' ArrayList errorList = new ArrayList();
' errorList.Add("This is a compiler error");
' return errorList.GetEnumerator();
Return Nothing
End Function
#End Region
#Region "Utility Functions"
Public Shared Sub DumpStreamToFile(ByVal fromStream As Stream, ByVal_
toFilename As String)
Dim file As FileStream = Nothing
Try
file = New FileStream(toFilename, System.IO.FileMode.Create)
Dim tmpBuff As Byte() = New Byte(4095) {}
Dim bytesRead As Integer = 0
bytesRead = file.Read(tmpBuff, 0, tmpBuff.Length)
memStream.Write(tmpBuff, 0, bytesRead)
Do While bytesRead <> 0
bytesRead = fromStream.Read(tmpBuff, 0, tmpBuff.Length)
file.Write(tmpBuff, 0, bytesRead)
Loop
file.Close()
file = Nothing
Finally
If Not file Is Nothing Then
file.Close()
End If
End Try
End Sub
Public Shared Function ReadFileToMemoryStream(ByVal fromFilename _
As String) As MemoryStream
Dim file As FileStream = Nothing
Try
file = New FileStream(fromFilename, System.IO.FileMode.Open)
Dim memStream As MemoryStream = New MemoryStream()
Dim tmpBuff As Byte() = New Byte(4095) {}
Dim bytesRead As Integer = 0
bytesRead = file.Read(tmpBuff, 0, tmpBuff.Length)
memStream.Write(tmpBuff, 0, bytesRead)
Do While bytesRead <> 0
bytesRead = file.Read(tmpBuff, 0, tmpBuff.Length)
memStream.Write(tmpBuff, 0, bytesRead)
Loop
file.Close()
file = Nothing
memStream.Position = 0
Return memStream
Finally
If Not file Is Nothing Then
file.Close()
End If
End Try
End Function
Public Shared Function ReadPropertyBag(ByVal pb As_
Microsoft.BizTalk.Component.Interop.IPropertyBag, ByVal propName As String) _
As Object
Dim val As Object = Nothing
Try
pb.Read(propName, val, 0)
Catch e1 As ArgumentException
Return val
Catch ex As Exception
Throw New ApplicationException(ex.Message)
End Try
Return val
End Function
''' <summary>
''' Writes property values into a property bag.
''' </summary>
''' <param name="pb">Property bag.</param>
''' <param name="propName">Name of property.</param>
''' <param name="val">Value of property.</param>
Public Shared Sub WritePropertyBag(ByVal pb As_
Microsoft.BizTalk.Component.Interop.IPropertyBag, ByVal propName As String, ByVal _
val As Object)
Try
pb.Write(propName, val)
Catch ex As Exception
Throw New ApplicationException(ex.Message)
End Try
End Sub
#End Region
End Class
End Namespace