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

use*_*258 1 java xor

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

我的问题是,当我尝试xor the ciphertexts时,我收到了意想不到的结果.

String ciphertext1 = "315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8cc57ee59418ce7dc6bc41556bdb36bbca3e8774301fbcaa3b83b220809560987815f65286764703de0f3d524400a19b159610b11ef3e";
String ciphertext2 = "32510ba9babebbbefd001547a810e67149caee11d945cd7fc81a05e9f85aac650e9052ba6a8cd8257bf14d13e6f0a803b54fde9e77472dbff89d71b57bddef121336cb85ccb8f3315f4b52e301d16e9f52f904";
Run Code Online (Sandbox Code Playgroud)

我将它们转换为字节数组

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));
}
Run Code Online (Sandbox Code Playgroud)

输出:

315C4EEAA8B5F8AAF9174145BF43E1784B8FA00DC71D885A804E5EE9FA40B16349C146FB778CDF2D3AFF021DFFF5B403B510D0D0455468AEB98622B137DAE857553CCD8883A7BC37520E06E515D22C954EBA5025B8CC57EE59418CE7DC6BC41556BDB36BBCA3E8774301FBCAA3B83B220809560987815F65286764703DE0F3D524400A19B159610B11EF3E
32510BA9BABEBBBEFD001547A810E67149CAEE11D945CD7FC81A05E9F85AAC650E9052BA6A8CD8257BF14D13E6F0A803B54FDE9E77472DBFF89D71B57BDDEF121336CB85CCB8F3315F4B52E301D16E9F52F904
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000054
Run Code Online (Sandbox Code Playgroud)

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

315C = 0011000101011100
3251 = 0011001001010001

xor = 0000001100001101
Run Code Online (Sandbox Code Playgroud)

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

谢谢!

Dun*_*nes 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]));
}
Run Code Online (Sandbox Code Playgroud)

把它移到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;
}
Run Code Online (Sandbox Code Playgroud)