Text Manipulation [VB .Net]

Forum rules
- Ask questions about or share code from languages other than Source Engine scripting.
- Start new topic titles with the code language in brackets (i.e. [C#], [C++], [PHP], etc.)
User avatar
Blake
500+ Posts
500+ Posts
Posts: 588
Joined: Mon Jul 05, 2010 8:51 pm
Location: Ontario, Canada

Text Manipulation [VB .Net]

Post by Blake »

MD5 Hashing

Code: Select all

    Public Function EncryptMD5(ByVal toEncrypt As String, ByVal key As String, ByVal useHashing As Boolean) As String
        Dim keyArray As Byte()
        Dim toEncryptArray As Byte() = UTF8Encoding.UTF8.GetBytes(toEncrypt)

        If useHashing = True Then
            Dim hashmd5 As New MD5CryptoServiceProvider()
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key))
        Else
            keyArray = UTF8Encoding.UTF8.GetBytes(key)
        End If

        Dim tdes As New TripleDESCryptoServiceProvider()
        tdes.Key = keyArray
        tdes.Mode = CipherMode.ECB
        tdes.Padding = PaddingMode.PKCS7

        Dim cTransform As ICryptoTransform = tdes.CreateEncryptor()
        Dim resultArray As Byte() = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length)

        Return (Convert.ToBase64String(resultArray, 0, resultArray.Length))
    End Function

    Public Function DecryptMD5(ByVal toDecrypt As String, ByVal key As String, ByVal useHashing As Boolean) As String
        Dim keyArray As Byte()
        Dim toEncryptArray As Byte() = Convert.FromBase64String(toDecrypt)

        If useHashing = True Then
            Dim hashmd5 As New MD5CryptoServiceProvider()
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key))
        Else
            keyArray = UTF8Encoding.UTF8.GetBytes(key)
        End If

        Dim tdes As New TripleDESCryptoServiceProvider()
        tdes.Key = keyArray
        tdes.Mode = CipherMode.ECB
        tdes.Padding = PaddingMode.PKCS7

        Dim cTransform As ICryptoTransform = tdes.CreateDecryptor()
        Dim resultArray As Byte() = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length)

        Return UTF8Encoding.UTF8.GetString(resultArray)
    End Function
Binary

Code: Select all

    Public Function EncryptBinary(ByVal Crypt As String)
        Dim Val As String = Nothing
        Dim Result As New System.Text.StringBuilder
        For Each Character As Byte In System.Text.ASCIIEncoding.ASCII.GetBytes(Crypt)
            Result.Append(Convert.ToString(Character, 2).PadLeft(8, "0"))
            Result.Append(" ")
        Next
        Val = Result.ToString.Substring(0, Result.ToString.Length - 1)
        Return Val
    End Function

    Public Function DecryptBinary(ByVal Crypt As String)
        Dim Val As String = Nothing
        Dim Characters As String = System.Text.RegularExpressions.Regex.Replace(Crypt, "[^01]", "")
        Dim ByteArray((Characters.Length / 8) - 1) As Byte
        For Index As Integer = 0 To ByteArray.Length - 1
            ByteArray(Index) = Convert.ToByte(Characters.Substring(Index * 8, 8), 2)
        Next
        Val = System.Text.ASCIIEncoding.ASCII.GetString(ByteArray)
        Return Val
    End Function
Hexadecimal Colors (HTML) to RGB & RGB To Hexadecimal Colors (HTML)

Code: Select all

    Private Function HexColorToRGB(ByVal R As String, ByVal G As String, ByVal B As String)
        Return Val("&H" & R) & " " & Val("&H" & G) & " " & Val("&H" & B)
    End Function

    Private Function RGBToHexColor(ByVal R As Byte, ByVal G As Byte, ByVal B As Byte)
        Dim HexR, HexB, HexG As Object
        Dim sTemp As String
        On Error GoTo ErrorHandler
        HexR = Hex(R)
        If Len(HexR) < 2 Then HexR = "0" & HexR
        HexG = Hex(G)
        If Len(HexG) < 2 Then HexG = "0" & HexG
        HexB = Hex(B)
        If Len(HexB) < 2 Then HexB = "0" & HexB
        Return "#" & HexR & HexG & HexB
ErrorHandler:
    End Function
ASCII - Needs work Because there is no generic way to input a string (Needs Regex)

Code: Select all

    Public Function EncryptASCII(ByVal Crypt As String)
        Dim Result As String = Nothing
        Dim ConvertCharToASCII As String
        Dim Text As String = Crypt
        Dim iASCII As Integer
        For iASCII = 0 To Text.Length - 2
            ConvertCharToASCII = AscW(Text.Chars(iASCII))
            Result = Result + ConvertCharToASCII + " "
        Next iASCII
        ConvertCharToASCII = AscW(Text.Chars(iASCII))
        Result = Result + ConvertCharToASCII
        Return Result
    End Function

    Public Function DecryptASCII(ByVal Crypt As String)
        Dim Result As String = Nothing
        Dim NumberSplit() As String
        NumberSplit = Crypt.Split(" ")
        Dim iString As Integer
        For iString = 0 To NumberSplit.Length() - 1
            Result = Result + ChrW(NumberSplit(iString))
        Next iString
        Return Result
    End Function
Hexadecimal - Needs work Because there is no generic way to input a string (Needs Regex)

Code: Select all

    Private Function EncryptHex(ByVal Crypt As String)
        Dim HexResult As String = Nothing
        Dim Text As String = Crypt
        Dim ConvertCharToHex As String
        Dim iHex As Integer
        For iHex = 0 To Text.Length - 2
            ConvertCharToHex = Conversion.Hex(AscW(Text.Chars(iHex)))
            HexResult = HexResult + ConvertCharToHex + " "
        Next iHex
        ConvertCharToHex = Conversion.Hex(AscW(Text.Chars(iHex)))
        HexResult = HexResult + ConvertCharToHex
        Return HexResult.ToLower()
    End Function

    Private Function DecryptHex(ByVal Crypt As String)
        Dim Result As String = Nothing
        Dim Temp As String = Nothing
        Dim NumberSplit() As String
        NumberSplit = Crypt.Split(" ")
        Dim iString As Integer
        For iString = 0 To NumberSplit.Length() - 1
            Temp = ChrW(Val("&H" & NumberSplit(iString)))
            Result = Result + Temp
        Next iString
        Return Result
    End Function

Character Effects (Strike Out Text, Letter Flare)

Code: Select all

    Private Function StrikeText(ByVal Crypt As String)
        Dim Result As String = Nothing
        Dim tempString As String = Nothing
        Dim i As Integer = 0
        Dim Text As String = Crypt
        For i = 0 To Text.Length - 2
            tempString = ChrW(822) + Text.Chars(i)
            Result = Result + tempString
        Next i
        tempString = ChrW(822) + Text.Chars(i) + ChrW(822)
        Result = Result + tempString
        Return Result
    End Function


    Private Function LetterTrail(ByVal Crypt As String)
        Dim Result As String = Nothing
        Dim tempString As String = Nothing
        Dim i As Integer = 0
        Dim Text As String = Crypt
        For i = 0 To Text.Length - 1
            tempString = Text.Chars(i)
            Result = Result + ChrW(3670) + ChrW(1756) + ChrW(1763) + tempString
        Next i
        Return Result
    End Function
Unkown wrote: Life’s too short to worry about the little things.
Albert Einstein wrote: You do not really understand something unless you can explain it to your grandmother.