I want to apply xor on each byte separately and store them in a specific byte in a certain memory location.
For example I have a string "0", I'm converting it to ASCII and then need to apply xor on it with a multi-byte key e.g "243", the resulting data must be in a single byte. Is this even possible?
private byte[] xorWithKey(byte[] a, byte[] key) {
byte[] out = new byte[a.length];
for (int i = 0; i < a.length; i++) {
out[i] = (byte) (a[i] ^ key[i%key.length]);
}
return out;
}
Right now I'm using this snippet which takes bytes array and returns a byte array, this is working fine, but I want to achieve this on a single byte like:
private byte xorWithKey(byte a, byte[] key)
You have an array of keys and can encrypt an input byte
with many number of keys based upon your choice of choosing keys from array.
You can use below approach if you want to encrypt input byte
with all the keys present in array, where encryption function uses all keys for encryption by taking xor iteratively
private byte xorWithKey(byte a, byte[] key) {
byte out=a;
for (int i = 0; i < key.length; i++) {
out = (byte) (out ^ key[i]);
}
return out;
}