I am currently working on an assignment to convert a Perl/PHP based intra-net project to an ASP.NET web application.

The complexity of the project has been significant as it involves the management of an internal Windows 2003 Active Directory setup for a large governmental organization, integrated with a proprietary ticketing system, and a Sql Server database. The (sometimes cryptic) non object-oriented PHP/Perl code together with a severely limited testing environment have only made the task that more difficult.

Anyways, I had to create VB.NET equivalent for some PHP functions while working on the migration. I am posting the functions here in the hope that they might be useful to someone googling around.

Currently, the PHP functions converted include md5, base64_encode, base64_decode, chunk_split, and mail.
I might add to this list of converted functions as and when I convert more of them depending upon requirements of my assignment.

So, here are the converted functions:

 

{syntaxhighlighter brush: vb;fontsize: 100; first-line: 1; }Public Module PhpToVb
Public Function md5(ByVal input As String) As String
Dim x As New System.Security.Cryptography.MD5CryptoServiceProvider()
Dim bs As Byte() = System.Text.Encoding.UTF8.GetBytes(Input)
bs = x.ComputeHash(bs)
Dim s As New System.Text.StringBuilder()
For Each b As Byte In bs
s.Append(b.ToString(“x2”).ToLower())
Next
Dim password As String = s.ToString()
Return password
End Function

Public Function base64_encode(ByVal input As String) As String
Dim bytesToEncode As Byte()
bytesToEncode = Encoding.UTF8.GetBytes(input)

Dim encodedText As String
encodedText = Convert.ToBase64String(bytesToEncode)

Return (encodedText)
End Function

Public Function base64_decode(ByVal input As String) As String
Dim decodedBytes As Byte()
decodedBytes = Convert.FromBase64String(input)

Dim decodedText As String
decodedText = Encoding.UTF8.GetString(decodedBytes)

Return (decodedText)
End Function

Public Function chunk_split(ByVal input As String, Optional ByVal chunkLen As Integer = 76, Optional ByVal sep As String = vbCr & vbLf) As String

If (chunkLen < 1) Then
Throw New ArgumentException(“Invalid value for chunklen, must be greater than zero.”)
End If

Return (String.Join(sep, System.Text.RegularExpressions.Regex.Split(input, “,{0,” & chunkLen & “}”, RegexOptions.Multiline)))
End Function

Public Sub mail(ByVal [to] As String, ByVal subject As String, ByVal body As String, ByVal headers As String)
Dim split = headers.Split(vbCrLf)

Dim mail As New System.Net.Mail.MailMessage

For Each header As String In split
Dim index As Integer = header.IndexOf(“:”)
Dim key As String = header.Substring(0, index)
Dim val As String = header.Substring(index + 1).Trim()

mail.Headers.Add(key, val)
Next

mail.Subject = subject
mail.Body = body

mail.To.Add([to])
Dim client As New System.Net.Mail.SmtpClient()

client.Send(mail)

End Sub
End Module{/syntaxhighlighter}