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

このPCにJavaがインストールされていないため、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 に答える