Encrypt using reapeting XOR

Viewed 42

i have to encrypy a string using repeating XOR with the KEY:"ICE". I think that i made a correct algorith to do it but the solution of the problem has 5 byte less then my calculated Hex string, why? Until this 5 bytes more the string are equals.

Did i miss something how to do repeating XOR?

public class ES5 {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String str1 = "Burning 'em, if you ain't quick and nimble";
        String str2 = "I go crazy when I hear a cymbal";
        String correct1 = "0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a2622632427276527";

        byte[] cr = Encript(str1.getBytes(StandardCharsets.UTF_8),"ICE");

        String cr22 = HexFormat.of().formatHex(cr);
        System.out.println(cr22);
        System.out.println(correct1);

    }
    private static byte doXOR(byte b, byte b1) {
        return (byte) (b^b1);
    }

    private static byte[] Encript(byte[] bt1, String ice) {
        int x = 0;
        byte[] rt = new byte[bt1.length];
        for (int i=0;i< bt1.length;i++){
            rt[i] = doXOR(bt1[i],(byte) (ice.charAt(x) & 0x00FF));
            x++;
            if(x==3)x=0;
        }
        return  rt;
    }
}
1 Answers

Hmmm. The String contains characters, and XOR works on bytes. That's why the first thing is to run String.getBytes() to receive a byte array.

Here, depending on the characters and their encoding the amount of bytes can be more than the amount of characters. You may want to print and compare the numbers already.

Then you perform XOR on the bytes, which may bring you into a completely different area for characters - so you cannot rely on new String(byte[]) at all. Instead you have to create a HEX string representation of the byte[].

Finally compare this HEX string with the value in correct. To me that string already looks like a HEX representation, so do not apply HEX again.

Related