Java xor xor(byte[] source, byte[] key)

Here you can find the source of xor(byte[] source, byte[] key)

Description

Returns a new array with the xor of source with key.

License

Open Source License

Parameter

Parameter Description
source a parameter
key a parameter

Declaration

public static byte[] xor(byte[] source, byte[] key) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*w  ww. j av  a2s  .c  o m*/
     * Returns a new array with the xor of source with key.
     * If key is shorter than source, then it is reused.
     * 
     * @param source
     * @param key
     * @return
     */
    public static byte[] xor(byte[] source, byte[] key) {
        int srcLen = source.length;
        int keyLen = key.length;
        byte[] ret = new byte[srcLen];

        for (int i = 0; i < srcLen; i++) {
            int keyIndex = i % keyLen;
            ret[i] = (byte) (source[i] ^ key[keyIndex]);
        }
        return ret;
    }
}

Related

  1. xor(byte[] bytes1, byte[] bytes2)
  2. xor(byte[] data, byte[] xork)
  3. xor(byte[] first, byte[] second)
  4. xor(byte[] firstBytes, int firstOffset, byte[] secondBytes, int secondOffset, byte[] outBytes, int outOffset, int size)
  5. xor(byte[] op1, byte[] op2)
  6. xor(byte[] x, byte[] y)
  7. xor(byte[]... bytesArr)
  8. xor(char[] a, char[] b)
  9. xor(double lhs, double rhs)