2012-12-03 2 views
0

sagepay 암호화는 VB에서만 문서화되므로 xor 암호화를 C#으로 작성하려고합니다.C# Sagepay XOR 암호화

VB에서 코드는 다음과 같습니다 지금까지 내가이 줄 무엇을하고 있는지 이해하지 못했다되어

 public static String SimpleXOR(String strIn, String strKey) 
    { 
     Int32 iInIndex, iKeyIndex; 
     String strReturn; 
     iInIndex = 1; 
     iKeyIndex = 1; 
     strReturn = ""; 

     while (iInIndex <= strIn.Length) 
     { 
      strReturn = strReturn & Strings.Chr(Strings.Asc(Strings.Mid(strIn, iInIndex, 1))^Strings.Asc(Strings.Mid(strKey, iKeyIndex, 1))); 
       iInIndex = iInIndex + 1; 
      if (iKeyIndex == strKey.Length) iKeyIndex = 0; 
      iKeyIndex = iKeyIndex + 1; 

     } 

    } 

문제로이를 변환 한

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 

strReturn = strReturn & Chr(Asc(Mid(strIn, iInIndex, 1)) Xor Asc(Mid(strKey, iKeyIndex, 1))) 

그래서 나는 VB에서 C# converter로 돌렸고 위의 것을 얻었다. 하지만 분명히 내가 알고있는 한 유효한 C# 코드가 아닙니다.

아무도 도와 줄 수 있습니까?

첫째, 현재 System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage을 잡아 :

C#에서
+2

지불 프로세서가 자체 암호화를 발명 한 경우 10 피트 극으로 만지지는 않습니다. – SLaks

+0

@SLaks SagePay는 일종의 거물입니다. –

답변

2

것 스레드의의 GetEncoding이 방법을 사용하여 새 인코딩 클래스의 인스턴스로 그를 통과 한 후 인코딩 인스턴스의 GetBytes 방법을 사용하여 바이트 배열로 문자열을 변환

public static string SimpleXOR(string strIn, string strKey) 
    { 
     if (strIn.Length == 0 || strKey.Length == 0) 
     { 
      return string.Empty; 
     } 

     int inIndex = 0; 
     int keyIndex = 0; 
     string returnString = string.Empty; 

     var currentCodePage = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage; 
     var encoding = Encoding.GetEncoding(currentCodePage); 

     var inString = encoding.GetBytes(strIn); 
     var keyString = encoding.GetBytes(strKey); 

     while (inIndex < inString.Length) 
     { 
      returnString += (char)(inString[inIndex]^keyString[keyIndex]); 

      inIndex++; 

      if (keyIndex == keyString.Length - 1) 
      { 
       keyIndex = 0; 
      } 
      else 
      { 
       keyIndex++; 
      } 
     } 

     return returnString; 
    } 

다른 간단한 방법은 C# 프로젝트에서 Microsoft.VisualBasic에 대한 참조를 추가하고 변환기에서 코드를 생성하도록하는 것입니다. Strings 클래스는 사용 가능하며 Strings.Chr, Strings.Asc 및 Strings.Mid를 수행 할 수 있습니다.

+0

정말 고마워요. – jimmy

0

, 그것은 단순히이 작업을 수행하는 내가 생각할 수있는 방법은 두 가지가 있습니다

strReturn += (char)(strIn[iInIndex] ^strKey[iKeyIndex]); 
+0

나는 그것에게 시도를 주었고 Index는 배열의 범위 밖에 있었다. – jimmy

+0

VB에서 Asc가 UTF 또는 ASCII가 아닌 현재 ANSI 코드 페이지를 기반으로 한 값을 반환하기 때문에 VB 코드와 동일한 출력을 생성하지 않습니다. http://msdn.microsoft.com/en-us/library/zew1e4wc % 28v = vs.71 % 29.aspx –

+0

내가 쓸 필요가있는 아이디어가 있습니까? – jimmy