开发者

Descrypt SagePay string vb.net

I have been having some problems trying to decrypt the string returned back from SagePay.

I used their asp.net kit which included the encrypt and decrypt functions using base64 - sending the information to SagePay is not a problem but I am having a number of problems trying to descrypt the string.

Here is the function I am using to descypt:

Private Function base64Decode(ByVal strEncoded As String) As String

    Dim iRealLength As Integer
    Dim strReturn As String
    Dim iBy4 As Integer
    Dim iIndex As Integer
    Dim iFirst As Integer
    Dim iSecond As Integer
    Dim iThird As Integer
    Dim iFourth As Integer
    If Len(strEncoded) = 0 Then
        base64Decode = ""
        Exit Function
    End If

    '** Base 64 encoded strings are right padded to 3 character multiples using = signs **
    '** Work out the actual length of data without the padding here **
    iRealLength = Len(strEncoded)
    Do While Mid(strEncoded, iRealLength, 1) = "="
    开发者_开发问答    iRealLength = iRealLength - 1
    Loop

    '** Non standard extension to Base 64 decode to allow for + sign to space character substitution by **
    '** some web servers. Base 64 expects a +, not a space, so convert vack to + if space is found **
    Do While InStr(strEncoded, " ") <> 0
        strEncoded = Left(strEncoded, InStr(strEncoded, " ") - 1) & "+" & Mid(strEncoded, InStr(strEncoded, " ") + 1)
    Loop

    strReturn = ""
    '** Convert the base 64 4x6 byte values into 3x8 byte real values by reading 4 chars at a time **
    iBy4 = (iRealLength \ 4) * 4
    iIndex = 1
    Do While iIndex <= iBy4
        iFirst = arrBase64DecMap(Asc(Mid(strEncoded, iIndex + 0, 1)))
        'iFirst = CType(System.Convert.ToByte(CType(Mid(strEncoded, iIndex + 0, 1), Char)), Integer)
        iSecond = arrBase64DecMap(Asc(Mid(strEncoded, iIndex + 1, 1)))
        'iSecond = CType(System.Convert.ToByte(CType(Mid(strEncoded, iIndex + 1, 1), Char)), Integer)
        iThird = arrBase64DecMap(Asc(Mid(strEncoded, iIndex + 2, 1)))
        'iThird = CType(System.Convert.ToByte(CType(Mid(strEncoded, iIndex + 2, 1), Char)), Integer)
        iFourth = arrBase64DecMap(Asc(Mid(strEncoded, iIndex + 3, 1)))
        'iFourth = CType(System.Convert.ToByte(CType(Mid(strEncoded, iIndex + 3, 1), Char)), Integer)
        strReturn = strReturn + CType(System.Convert.ToChar(((iFirst * 4) And 255) + ((iSecond \ 16) And 3)), String) 'Chr(((iFirst * 4) And 255) + ((iSecond \ 16) And 3))
        strReturn = strReturn + CType(System.Convert.ToChar(((iSecond * 16) And 255) + ((iThird \ 4) And 15)), String) 'Chr(((iSecond * 16) And 255) + ((iThird \ 4) And 15))
        strReturn = strReturn + CType(System.Convert.ToChar(((iThird * 64) And 255) + (iFourth And 63)), String) 'Chr(((iThird * 64) And 255) + (iFourth And 63))
        iIndex = iIndex + 4
    Loop

    '** For non multiples of 4 characters, handle the = padding **
    If iIndex < iRealLength Then
        iFirst = arrBase64DecMap(Asc(Mid(strEncoded, iIndex + 0, 1)))
        iSecond = arrBase64DecMap(Asc(Mid(strEncoded, iIndex + 1, 1)))
        strReturn = strReturn & Chr(((iFirst * 4) And 255) + ((iSecond \ 16) And 3))
        If iRealLength Mod 4 = 3 Then
            iThird = arrBase64DecMap(Asc(Mid(strEncoded, iIndex + 2, 1)))
            strReturn = strReturn & Chr(((iSecond * 16) And 255) + ((iThird \ 4) And 15))
        End If
    End If

    base64Decode = strReturn
End Function

I don't think the web server is trying to encode anything as there are no + symbols within the url string and I have just glanced over the two to compair they are the same.

This returns a blank string whereas when I use the sections commented out in the first loop i get a really weired string back and when I use their simpleXor function it just returns complete nonsense.

There support is a bit useless as they "are not programmers"! So I am hoping someone can help me out who has used SagePay before.

Thanks in advance.


Managed to get it working:

Private Function base64Decode(ByVal strEncoded As String) As String 
    Dim output As String = "" 
    Dim bt64 As Byte() = System.Convert.FromBase64String(strEncoded) 

    For i As Integer = 0 To (bt64.Length - 1) 
        output &= System.Convert.ToChar(CInt(bt64(i))).ToString() 
    Next 

    Return output 
End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜