I've been attempting to create sort of a One Time Pad encrption in my free time in order to learn a bit. My idea was to convert the input(which shall be encrypted) into a String of bits. Then I have a password (also in a String of bits) and the input gets encrypted with XOR. e.g. pw= 101001 , input= 11001, then enc= 0110. My problem now is:
how does binary.append((val&128)==0 ? 0 : 1);
work?
I guess I can rewrite this as
if(val&128)==0{
binary.append(0);
}else{
binary.append(1);
}
But how can 2 Numbers ( val&128 ) equal to one number (0) ? This is my code:
String s ="foo";
byte[] bytes = s.getBytes();
StringBuilder binary = new StringBuilder();
for(byte[] b : bytes){
int val = b;
for(int i=0; i<8; i++){
binary.append((val&128)==0 ? 0 : 1);
val <<= 1;
}
}
System.out.println(s + " to binary: " + binary)
Thanks for help:)