Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.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 解密字符串时出现数字格式异常_Java_Android_Encryption - Fatal编程技术网

Java 解密字符串时出现数字格式异常

Java 解密字符串时出现数字格式异常,java,android,encryption,Java,Android,Encryption,我正试图使用以下方法对加密字符串进行解密,但遇到了异常 我正试图将加密字符串发送到下面的方法,但它无法获取字节[],将字符串转换为字节[]时出现数字格式异常 我的解密方法: public static String decrypt(String seed, String encrypted) throws Exception { byte[] seedByte = seed.getBytes(); System.arraycopy(seedByte, 0, key, 0, ((s

我正试图使用以下方法对加密字符串进行解密,但遇到了异常

我正试图将加密字符串发送到下面的方法,但它无法获取字节[],将字符串转换为字节[]时出现数字格式异常

我的解密方法:

public static String decrypt(String seed, String encrypted) throws Exception {

   byte[] seedByte = seed.getBytes();

   System.arraycopy(seedByte, 0, key, 0, ((seedByte.length < 16) ? seedByte.length : 16));

   String base64 = new String(Base64.decode(encrypted, 0));

  byte[] rawKey = getRawKey(seedByte);

   byte[] enc = toByte(base64);

   byte[] result = decrypt(rawKey, enc);

   return new String(result);

  }
我真的不明白为什么我会犯这个错误


请提出建议。

我认为这里的问题是对字节数组的解析:

result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue();
您可以尝试这种方法(考虑4字节整数,并且希望将十六进制字符串拆分为两位整数):

byte[][]结果=新字节[len][4];
int[]数字=新的int[len];
对于(int j=0;j>(-1)*j*8+24))&0xFF);
}
}

实现中的问题是将字符串拆分为几个整数,并将整数的第一个字节分配给字节数组[0],然后将第二个整数的第一个字节分配给字节数组[1]。知道长度为4的整个字节数组用于存储单个整数。

十六进制字符串中似乎有一些无效字节。在将其放入字节数组之前尝试验证它,例如,执行以下操作:

    if(new Scanner(hexString.substring(2 * i, 2 * i + 2), 16).hasNextInt())
         result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue()    

这里没有IDE,但这可能是一个字符集问题吗?我不知道该怎么办。你能给我一些建议尝试编码/解码这个包:sun.misc.BASE64Decoder sun.misc.base64encoder我需要在这里使用hexString请建议,我应该在哪里使用hexStringresult[I][j]=(字节)((数字[I]>>(-1)*j*8+24))&0xFF);我在这行遇到了错误请告诉我它们是什么,我似乎无法一眼就找到它们。我发现,如果我的加密字符串包含像/,+这样的特殊字符,我想这就是为什么我会遇到错误的原因。它进入了这个if条件。请建议任何其他解决方案encodecodeaes.java:226行到底对应什么?结果[i]=Integer.valueOf(hexString.substring(2*i,2*i+2),16).byteValue();这是指那条线
result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue();
byte[][] result = new byte[len][4];
int[] numbers = new int[len];
for(int j = 0; j < len ; j++){
  numbers[j] = Integer.parseInt(hexString.substring(2 * j, 2 * j + 2);
}

for (i = 0; i < len; i++){
  result[i][3] = (byte) numbers[i] & 0xFF);//No need for shifting here
  for(j = 0; j < len-1; j++){
    result[i][j] = (byte) ((numbers[i]  >> ((-1)*j*8+24)) & 0xFF);
  }
}
    if(new Scanner(hexString.substring(2 * i, 2 * i + 2), 16).hasNextInt())
         result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue()