我在将这段代码从 java 转换为 .net 时遇到了一些麻烦,在本例中为 vb.net,但如果有人可以帮助我将它从 c# 转换为 vb,我很高兴。
我对以“final boolean useStandardEnding”开头的那一行没意见,这是最简单的部分。我包含了整个函数的代码以给出整个画面。我尝试了下一行和 IF 语句,但随后它开始谈论 XOR,我只是不知道从哪里开始编码。我了解 XOR 的概念,但到目前为止从未需要实际上编码它..
任何帮助将不胜感激。
public class TokenDecode {
/**
* Method to decode the User token
*
* @param UserId
* @return long - unique decoded id
*/
public static long decode(final String UserId) {
final int FILL_CHAR_EQUAL = 1;
int type = 0;
if (UserId == null) {
throw new IllegalArgumentException("UserId can't be null");
}
if (UserId.endsWith("=")) {
// new encoding
type = 1;
} else {
// old encoding
type = 2;
}
final boolean useStandardEnding = (type == FILL_CHAR_EQUAL);
byte[] bytes = Base64.decode(UserId, useStandardEnding);
if (bytes.length < 40) {
throw new IllegalArgumentException(
"Base64 decoded length of UserId should be 40 (Actual="
+ bytes.length + ";UserId=" + UserId + ")");
}
// exclusive or
byte[] xor = { (byte) 0xa0, (byte) 0xb2, (byte) 0x91, (byte) 0x20 };
int cnt = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 4; j++) {
bytes[cnt] = (byte) (bytes[cnt] ^ xor[j]);
cnt++;
}
xor[3] += 4;
}
// rotate right 2 entities
final byte[] buffer = new byte[8];
System.arraycopy(bytes, 32, buffer, 0, 8);
System.arraycopy(bytes, 0, bytes, 8, 32);
System.arraycopy(buffer, 0, bytes, 0, 8);
// remove leading '=' and convert to int
String str = new String(bytes);
final int pos = str.lastIndexOf('=');
str = str.substring(pos + 1);
// Added to remove the extra spaces in the userid
// Integration.
str = (str == null) ? null : str.trim();
return Long.parseLong(str);
}
}
谢谢