我正在尝试在 C# 中编写 xor 加密,因为 sagepay 加密仅记录在 VB 中。
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# 代码。
任何人都可以帮忙吗?