2011-01-25 3 views
0

J'ai eu quelques problèmes en essayant de décrypter la chaîne renvoyée par SagePay. J'ai utilisé leur kit asp.net qui comprenait les fonctions crypter et décrypter en utilisant base64 - envoyer les informations à SagePay n'est pas un problème, mais j'ai un certain nombre de problèmes pour essayer de déchiffrer la chaîne.Descrypt SagePay chaîne vb.net

Voici la fonction que je me sers de 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 

Je ne pense pas que le serveur Web tente de tout encoder car il n'y a pas de + symboles dans la chaîne url et je viens jeté un coup d'œil sur les deux pour comparer, ils sont les mêmes. Cela renvoie une chaîne vide alors que lorsque j'utilise les sections commentées dans la première boucle, j'obtiens une chaîne vraiment rétrécie et quand j'utilise leur fonction simpleXor, elle renvoie simplement un non-sens complet.

Le support est un peu inutile car ils "ne sont pas des programmeurs"! J'espère donc que quelqu'un pourra m'aider à utiliser SagePay auparavant.

Merci d'avance.

Répondre

1

a réussi à le faire fonctionner:

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