title: Java xor byte array not returning expected result
body: <p>This is part of an assignment, I'm trying to decrypt a message from several ciphertexts, encrypted using the same OTP.</p>
  
  <p>My issue is that when I try to xor the ciphertexts, I'm receiving unexpected results.</p>
  
  <pre><code>String ciphertext1 = "315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8cc57ee59418ce7dc6bc41556bdb36bbca3e8774301fbcaa3b83b220809560987815f65286764703de0f3d524400a19b159610b11ef3e";
  String ciphertext2 = "32510ba9babebbbefd001547a810e67149caee11d945cd7fc81a05e9f85aac650e9052ba6a8cd8257bf14d13e6f0a803b54fde9e77472dbff89d71b57bddef121336cb85ccb8f3315f4b52e301d16e9f52f904";
  </code></pre>
  
  <p>I convert them to byte arrays</p>
  
  <pre><code>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 &gt; b.length) {
          for (int i = 0; i &lt; b.length; i++) {
              result = new byte[b.length];
              result[i] = (byte) (((int) a[i]) ^ ((int) b[i]));
          }
      } else {
          for (int i = 0; i &lt; 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));
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>315C4EEAA8B5F8AAF9174145BF43E1784B8FA00DC71D885A804E5EE9FA40B16349C146FB778CDF2D3AFF021DFFF5B403B510D0D0455468AEB98622B137DAE857553CCD8883A7BC37520E06E515D22C954EBA5025B8CC57EE59418CE7DC6BC41556BDB36BBCA3E8774301FBCAA3B83B220809560987815F65286764703DE0F3D524400A19B159610B11EF3E
  32510BA9BABEBBBEFD001547A810E67149CAEE11D945CD7FC81A05E9F85AAC650E9052BA6A8CD8257BF14D13E6F0A803B54FDE9E77472DBFF89D71B57BDDEF121336CB85CCB8F3315F4B52E301D16E9F52F904
  0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000054
  </code></pre>
  
  <p>My problem is the result of the xor operation, the last line of output.
  When I manually xor the first 2 bytes, i get;</p>
  
  <pre><code>315C = 0011000101011100
  3251 = 0011001001010001
  
  xor = 0000001100001101
  </code></pre>
  
  <p>So the result I'm getting is wrong. What am I doing wrong here?</p>
  
  <p>Thank you!</p>