1

我在将这段代码从 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);
  }
}

谢谢

4

1 回答 1

2

无法使用 java 检查输出,因为我没有在这台电脑上安装它。我尽可能快地写了它,但我对 java 有点生疏......试试看这是否有效:

    public static long Decode(String UserId)
    {
        int FILL_CHAR_EQUAL = 1;
        int type = 0;
        if (UserId == null)
        {
            throw new ArgumentException("UserId can't be null");
        }

        if (UserId.EndsWith("="))
        {
            // new encoding
            type = 1;
        }
        else
        {
            // old encoding
            type = 2;
        }

        bool useStandardEnding = (type == FILL_CHAR_EQUAL);
        byte[] bytes = Convert.FromBase64String(UserId);
        if (bytes.Length < 40)
        {
            throw new ArgumentException(
                "Base64 decoded length of UserId should be 40 (Actual="
                    + bytes.Length + ";UserId=" + UserId + ")");
        }

        // exclusive or
        byte[] xor = new byte[] { (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
        byte[] buffer = new byte[8];
        Array.Copy(bytes, 32, buffer, 0, 8);
        Array.Copy(bytes, 0, bytes, 8, 32);
        Array.Copy(buffer, 0, bytes, 0, 8);
        // remove leading '=' and convert to int
        String str = Encoding.Default.GetString(bytes);
        int pos = str.LastIndexOf('=');
        str = str.Substring(pos + 1);
        // Added to remove the extra spaces in the userid-ebay-PayPal
        // Integration.
        str = (str == null) ? null : str.Trim();
        return long.Parse(str);
    }
于 2013-04-16T22:37:39.930 回答