Java xor字节数组未返回预期结果

时间:2014-06-30 09:32:09

标签: java xor

这是作业的一部分,我正在尝试解密来自几个密文的消息,使用相同的OTP加密。

我的问题是,当我尝试对密文进行编辑时,我收到了意想不到的结果。

String ciphertext1 = "315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8cc57ee59418ce7dc6bc41556bdb36bbca3e8774301fbcaa3b83b220809560987815f65286764703de0f3d524400a19b159610b11ef3e";
String ciphertext2 = "32510ba9babebbbefd001547a810e67149caee11d945cd7fc81a05e9f85aac650e9052ba6a8cd8257bf14d13e6f0a803b54fde9e77472dbff89d71b57bddef121336cb85ccb8f3315f4b52e301d16e9f52f904";

我将它们转换为字节数组

public static byte[] hexStringToByteArray(String s) {
    HexBinaryAdapter adapter = new HexBinaryAdapter();
    byte[] bytes = adapter.unmarshal(s);
    return bytes;
}

public static byte[] xor(byte[] a, byte[] b) {
    byte[] result = null;
    if (a.length > b.length) {
        for (int i = 0; i < b.length; i++) {
            result = new byte[b.length];
            result[i] = (byte) (((int) a[i]) ^ ((int) b[i]));
        }
    } else {
        for (int i = 0; i < a.length; i++) {
            result = new byte[a.length];
            result[i] = (byte) (((int) a[i]) ^ ((int) b[i]));
        }
    }
    return result;
}

public static void main(String[] args){
    byte[] bytes1 = hexStringToByteArray(ciphertext1);
    byte[] bytes2 = hexStringToByteArray(ciphertext2);
    byte[] cipher1 = xor(bytes1, bytes2);
    System.out.println(javax.xml.bind.DatatypeConverter.printHexBinary(bytes1));
    System.out.println(javax.xml.bind.DatatypeConverter.printHexBinary(bytes2));
    System.out.println(javax.xml.bind.DatatypeConverter.printHexBinary(cipher1));
}

输出:

315C4EEAA8B5F8AAF9174145BF43E1784B8FA00DC71D885A804E5EE9FA40B16349C146FB778CDF2D3AFF021DFFF5B403B510D0D0455468AEB98622B137DAE857553CCD8883A7BC37520E06E515D22C954EBA5025B8CC57EE59418CE7DC6BC41556BDB36BBCA3E8774301FBCAA3B83B220809560987815F65286764703DE0F3D524400A19B159610B11EF3E
32510BA9BABEBBBEFD001547A810E67149CAEE11D945CD7FC81A05E9F85AAC650E9052BA6A8CD8257BF14D13E6F0A803B54FDE9E77472DBFF89D71B57BDDEF121336CB85CCB8F3315F4B52E301D16E9F52F904
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000054

我的问题是xor操作的结果,即输出的最后一行。 当我手动xor前2个字节时,我得到;

315C = 0011000101011100
3251 = 0011001001010001

xor = 0000001100001101

所以我得到的结果是错的。我在这里做错了什么?

谢谢!

3 个答案:

答案 0 :(得分:7)

每次迭代for循环时,都在重新创建字节数组:

for (int i = 0; i < b.length; i++) {
  result = new byte[b.length];  // <---- wipes out previous values
  result[i] = (byte) (((int) a[i]) ^ ((int) b[i]));
}

将其移到for循环之外。

以下是经过更正的简化版本。请注意,如果您使用Math.min

,您将不需要两个循环
public static byte[] xor(byte[] a, byte[] b) {
  byte[] result = new byte[Math.min(a.length, b.length)];

  for (int i = 0; i < result.length; i++) {
    result[i] = (byte) (((int) a[i]) ^ ((int) b[i]));
  }

  return result;
}

答案 1 :(得分:0)

您正在循环的每次迭代中创建一个新的字节数组:

 for (int i = 0; i < b.length; i++) {
     result = new byte[b.length];
     result[i] = (byte) (((int) a[i]) ^ ((int) b[i]));
 }

(和其他分支类似)。 JVM将新数组填充为0,然后使用适当的值替换(在每次迭代中)其中一个元素。因此,只有最后一次XOR操作的结果实际存在于您的方法返回的字节数组中。

答案 2 :(得分:0)

/** Return XOR of two byte array of different or same size. */
    public static byte[] xor(byte[] data1, byte[] data2) {
        // make data2 the largest...
        if (data1.length > data2.length) {
            byte[] tmp = data2;
            data2 = data1;
            data1 = tmp;
        }
        for (int i = 0; i < data1.length; i++) {
            data2[i] ^= data1[i];
        }
        return data2;
    }