Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有人能够帮助将此代码从Java转换为.Net(vb或c#)吗_Java_.net - Fatal编程技术网

有人能够帮助将此代码从Java转换为.Net(vb或c#)吗

有人能够帮助将此代码从Java转换为.Net(vb或c#)吗,java,.net,Java,.net,我在将这段代码从java转换到.net时遇到了一些麻烦,在本例中是转换到vb.net,但如果有人能帮助我将其从c#转换到vb,我很高兴 我对开始“最终布尔使用标准结束”的那一行很满意,这是最简单的部分。。我包含了整个函数的代码,给出了整个画面。 我尝试了下一行和IF语句,但后来它开始讨论XOR,我不知道从哪里开始编码。。 我理解XOR的概念,但到目前为止,我从未真正需要编写XOR 任何帮助都将不胜感激 public class TokenDecode { /** * Method t

我在将这段代码从java转换到.net时遇到了一些麻烦,在本例中是转换到vb.net,但如果有人能帮助我将其从c#转换到vb,我很高兴

我对开始“最终布尔使用标准结束”的那一行很满意,这是最简单的部分。。我包含了整个函数的代码,给出了整个画面。 我尝试了下一行和IF语句,但后来它开始讨论XOR,我不知道从哪里开始编码。。 我理解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);
  }
}
公共类令牌解码{
/**
*方法来解码用户令牌
* 
*@param UserId
*@return long-唯一解码id
*/
公共静态长解码(最终字符串用户ID){
最终整数填充字符等于1;
int类型=0;
if(UserId==null){
抛出新的IllegalArgumentException(“UserId不能为null”);
}
if(UserId.endsWith(“=”){
//新编码
类型=1;
}否则{
//旧编码
类型=2;
}
最终布尔值useStandardEnding=(类型==填充字符等于);
byte[]bytes=Base64.decode(UserId,useStandardEnding);
如果(字节长度<40){
抛出新的IllegalArgumentException(
“用户标识的Base64解码长度应为40(实际=”
+bytes.length+“UserId=“+UserId+”);
}
//排他或
字节[]xor={(字节)0xa0,(字节)0xb2,(字节)0x91,(字节)0x20};
int-cnt=0;
对于(int i=0;i<10;i++){
对于(int j=0;j<4;j++){
字节[cnt]=(字节)(字节[cnt]^xor[j]);
cnt++;
}
xor[3]+=4;
}
//向右旋转2个实体
最终字节[]缓冲区=新字节[8];
数组复制(字节,32,缓冲区,0,8);
数组复制(字节,0,字节,8,32);
数组复制(缓冲区,0,字节,0,8);
//删除前导“=”并转换为int
String str=新字符串(字节);
最终int pos=str.lastIndexOf('=');
str=str.substring(pos+1);
//添加以删除用户标识中的额外空格
//整合。
str=(str==null)?null:str.trim();
返回Long.parseLong(str);
}
}

谢谢

我无法用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);
    }
公共静态长解码(字符串UserId)
{
int FILL_CHAR_EQUAL=1;
int类型=0;
if(UserId==null)
{
抛出新ArgumentException(“UserId不能为null”);
}
if(UserId.EndsWith(“=”)
{
//新编码
类型=1;
}
其他的
{
//旧编码
类型=2;
}
bool usesStandardEnding=(类型==填充字符相等);
byte[]bytes=Convert.FromBase64String(UserId);
如果(字节长度<40)
{
抛出新的ArgumentException(
“用户标识的Base64解码长度应为40(实际=”
+bytes.Length+“UserId=“+UserId+”);
}
//排他或
字节[]异或=新字节[]{(字节)0xa0,(字节)0xb2,(字节)0x91,(字节)0x20};
int-cnt=0;
对于(int i=0;i<10;i++)
{
对于(int j=0;j<4;j++)
{
字节[cnt]=(字节)(字节[cnt]^xor[j]);
cnt++;
}
xor[3]+=4;
}
//向右旋转2个实体
字节[]缓冲区=新字节[8];
复制(字节,32,缓冲区,0,8);
复制(字节,0,字节,8,32);
复制(缓冲区,0,字节,0,8);
//删除前导“=”并转换为int
String str=Encoding.Default.GetString(字节);
int pos=str.LastIndexOf('=');
str=str.Substring(pos+1);
//添加以删除用户ID ebay PayPal中的额外空间
//整合。
str=(str==null)?null:str.Trim();
返回long.Parse(str);
}

如果有在线转换器可以做到这一点,那么为我指明它的方向也会很有帮助。。我就是找不到一个,当人们问起的时候,每个人似乎都在说“用手来做”。:-(xor运算符在Java和C中是相同的,那么你不明白是什么?@Ads OK我想帮你。除了为你编写所有代码之外,我还能做什么?(顺便说一句:这不是网站推荐)@I4V我知道一些简单的东西,比如循环、变量声明等。如果你不想对其进行编码,那么,嗯……你能解释一下主要围绕字节数组位和XOR部分发生的事情吗?@BrianRasmussen我理解XOR的概念,但我不知道它试图实现什么,所以我不知道如何在.net中重写它