如何解密simpleXor Base64编码的字符串

时间:2019-04-15 13:37:54

标签: vb.net encryption base64 password-encryption

我是这种加密技术的新手,而且我正在研究以前的工程师代码。可以找到加密的作品,但是我想要一个解密器,我不确定如何启动或启动

有一个PasswordEncryption类,如下所示

Public Class PasswordEncryption
    Public Shared arrBase64EncMap(64) As String
    Public Shared arrBase64DecMap(127) As Integer

    Const BASE_64_MAP_INIT As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

    Public Shared Function EncryptionPassword() As String
        Return "F2oWrB4sA3feEh1tz"
    End Function

    Public Shared Function simpleXor(ByVal strIn As String, ByVal strKey As String) As String
        Dim iInIndex As Integer
        Dim iKeyIndex As Integer
        Dim strReturn As String
        If Len(strIn) = 0 Or Len(strKey) = 0 Then
            simpleXor = ""
            Exit Function
        End If

        iInIndex = 1
        iKeyIndex = 1
        strReturn = ""

        '** Step through the plain text source XORing the character at each point with the next character in the key **
        '** Loop through the key characters as necessary **
        Do While iInIndex <= Len(strIn)
            strReturn = strReturn & Chr(Asc(Mid(strIn, iInIndex, 1)) Xor Asc(Mid(strKey, iKeyIndex, 1)))
            iInIndex = iInIndex + 1
            If iKeyIndex = Len(strKey) Then iKeyIndex = 0
            iKeyIndex = iKeyIndex + 1
        Loop

        simpleXor = strReturn
    End Function

    Public Shared Function base64Encode(ByVal strPlain As String) As String
        Dim iLoop As Integer
        Dim iBy3 As Integer
        Dim strReturn As String
        Dim iIndex As Integer
        Dim iFirst As Integer
        Dim iSecond As Integer
        Dim iiThird As Integer

        If strPlain.Length = 0 Then
            base64Encode = ""
            Exit Function
        End If

        '** Set up Base64 Encoding and Decoding Maps for when we need them ** 
        For iLoop = 0 To Len(BASE_64_MAP_INIT) - 1
            arrBase64EncMap(iLoop) = Mid(BASE_64_MAP_INIT, iLoop + 1, 1)
        Next
        For iLoop = 0 To Len(BASE_64_MAP_INIT) - 1
            arrBase64DecMap(Asc(arrBase64EncMap(iLoop))) = iLoop
        Next

        '** Work out rounded down multiple of 3 bytes length for the unencoded text **
        iBy3 = (strPlain.Length \ 3) * 3
        strReturn = ""

        '** For each 3x8 byte chars, covert them to 4x6 byte representations in the Base64 map **
        iIndex = 1
        Do While iIndex <= iBy3
            iFirst = Asc(Mid(strPlain, iIndex + 0, 1))
            iSecond = Asc(Mid(strPlain, iIndex + 1, 1))
            iiThird = Asc(Mid(strPlain, iIndex + 2, 1))
            strReturn = strReturn & arrBase64EncMap((iFirst \ 4) And 63)
            strReturn = strReturn & arrBase64EncMap(((iFirst * 16) And 48) + ((iSecond \ 16) And 15))
            strReturn = strReturn & arrBase64EncMap(((iSecond * 4) And 60) + ((iiThird \ 64) And 3))
            strReturn = strReturn & arrBase64EncMap(iiThird And 63)
            iIndex = iIndex + 3
        Loop

        '** Handle any trailing characters not in groups of 3 **
        '** Extend to multiple of 3 characters using = signs as per RFC **
        If iBy3 < strPlain.Length Then
            iFirst = Asc(Mid(strPlain, iIndex + 0, 1))
            strReturn = strReturn & arrBase64EncMap((iFirst \ 4) And 63)
            If (strPlain.Length Mod 3) = 2 Then
                iSecond = Asc(Mid(strPlain, iIndex + 1, 1))
                strReturn = strReturn & arrBase64EncMap(((iFirst * 16) And 48) + ((iSecond \ 16) And 15))
                strReturn = strReturn & arrBase64EncMap((iSecond * 4) And 60)
            Else
                strReturn = strReturn & arrBase64EncMap((iFirst * 16) And 48)
                strReturn = strReturn & "="
            End If
            strReturn = strReturn & "="
        End If

        '** Return the encoded result string **
        base64Encode = strReturn
    End Function

End Class

我有一个简单的表单,其中有两个按钮“加密”和“解密”以及几个文本框。

“加密”按钮的click事件执行以下操作

 Private Sub btnEncrypt_Click(sender As Object, e As EventArgs) Handles btnEncrypt.Click
        If tbxPassword.Text IsNot Nothing Then
            Dim PasswordEncryption As PasswordEncryption = New PasswordEncryption()
            Dim strXOR As String = PasswordEncryption.simpleXor(Strings.Left(tbxPassword.Text.ToString().Trim, 20), PasswordEncryption.EncryptionPassword)
            Dim encryptedPassword = PasswordEncryption.base64Encode(strXOR)
            tbxResult.Text = encryptedPassword
        Else
            MessageBox.Show("No action can be performed")
        End If

    End Sub

我在“解密”按钮中有以下代码,但它没有带回我的原始字符串

Private Sub btnDecrypt_Click(sender As Object, e As EventArgs) Handles btnDecrypt.Click


        If tbxPassword.Text IsNot Nothing Then
            Dim PasswordEncryption As PasswordEncryption = New PasswordEncryption()
            Dim strXOR As String = PasswordEncryption.simpleXor(Strings.Left(tbxPassword.Text.ToString().Trim, 20), PasswordEncryption.EncryptionPassword)
            Dim decryptedPassword = PasswordEncryption.base64Encode(strXOR)
            tbxResult.Text = decryptedPassword
        Else
            MessageBox.Show("No action can be performed")
        End If

我需要实现什么才能取回原始字符串。谢谢你们!

1 个答案:

答案 0 :(得分:0)

好的,我已经从以下链接中弄清楚了

https://www.codingforums.com/archive/index.php/t-28425.html

我已经包含了解码方法,根据MarkL的评论,我首先对字符串进行了Base64Decode解码,然后执行了simpleXOR,然后又得到了原始字符串。

我的解密方法如下

 Private Sub btnDecrypt_Click(sender As Object, e As EventArgs) Handles btnDecrypt.Click


        If tbxPassword.Text IsNot Nothing Then
            Dim PasswordEncryption As PasswordEncryption = New PasswordEncryption()
            ' Dim strXOR As String = PasswordEncryption.simpleXor(Strings.Left(tbxPassword.Text.ToString().Trim, 20), PasswordEncryption.EncryptionPassword)
            Dim base64DecodedValue = PasswordEncryption.base64Decode(tbxPassword.Text)

            tbxResult.Text = PasswordEncryption.simpleXor(Strings.Left(base64DecodedValue.Trim, 20), PasswordEncryption.EncryptionPassword)
        Else
            MessageBox.Show("No action can be performed")
        End If

    End Sub