2012-12-03 26 views
0

我想在C#中编写xor加密,因为sagepay加密仅在VB中记录。c#Sagepay XOR加密

VB代码是:

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 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; 

     } 

    } 

问题是我不明白这行做

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

所以我运行它通过一个vb到C#转换器,并得到了上述。但据我所知,这显然不是有效的C#代码。

任何人都可以帮忙吗?

+2

如果您的付款处理器发明了自己的加密功能,我不会用十英尺的杆子来触摸它。 – SLaks

+0

@SLaks SagePay是一个伟大的。 –

回答

2

有两种方法我能想到的要做到这一点:

首先,抓住当前System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage线程,传递到使用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.Chr,Strings.Asc和Strings.Mid。

+0

非常感谢你 – jimmy

0

在C#中,这将仅仅是

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

我试了一下,得到索引超出了数组的界限。 – jimmy

+0

这不会产生与VB代码相同的输出,因为VB中的Asc返回基于当前ANSI代码页而不是UTF或ASCII的值:http://msdn.microsoft.com/zh-cn/library/zew1e4wc %28v = vs.71%29.aspx –

+0

关于我需要写什么的任何想法? – jimmy