XOR operation with two strings in java

前端 未结 7 547
有刺的猬
有刺的猬 2020-11-28 05:48

How to do bitwise XOR operation to two strings in java.

7条回答
  •  独厮守ぢ
    2020-11-28 06:15

    This is the code I'm using:

    private static byte[] xor(final byte[] input, final byte[] secret) {
        final byte[] output = new byte[input.length];
        if (secret.length == 0) {
            throw new IllegalArgumentException("empty security key");
        }
        int spos = 0;
        for (int pos = 0; pos < input.length; ++pos) {
            output[pos] = (byte) (input[pos] ^ secret[spos]);
            ++spos;
            if (spos >= secret.length) {
                spos = 0;
            }
        }
        return output;
    }
    

提交回复
热议问题