1

I have a key for a cipher in the form "XY XY+1 XY+2 XY+3 XY+4 XY+5 FF FF" where XY is an unknown byte, for example, XY could be 00000000 so XY+1 is 00000001. Also, FF is a constant so always 11111111.

l have an addBinary() method which simply adds 1 to any binary string I give it, however, I'm finding it hard to generate all binary strings composing of "xxxx... 11111111 11111111".

I also found this printB() method on StackOverflow which generates the strings but just not sure how to hard code the FF's into it.

static void printB()
    {
        for(int i = 0; i < Math.pow(2,8); i++)
        {
            String format="%0"+8+"d";
            System.out.printf(format,Integer.valueOf(Integer.toBinaryString(i)));
            System.out.println();
        }
    }

Any help on how to generate this strings would be appreciated

Vijayanath Viswanathan
  • 8,027
  • 3
  • 25
  • 43
  • 2
    Not clear what you're asking here. Do you want a method such that `assertEquals(makeString(0b10), "00000010 00000011 00000100 00000101 00000110 00000111 11111111 11111111")` ? – slim Oct 09 '17 at 14:47
  • 1
    Your first paragraph gives no reason why you would want to generate a string for all 256 8 bit numbers. – slim Oct 09 '17 at 15:15
  • All 8-bit binaries are obtained by starting at `0000 0000` and then repeatedly **adding** `1`. Bitwise-add can, for example, be found [here](https://stackoverflow.com/q/4895173/2411243) at StackOverflow. Alternatively use `Integer` with a **radix** of `2` (if performance does not matter that much), as seen [here](https://stackoverflow.com/a/8548599/2411243) at SO. This approach allows you to simply do `value++`. – Zabuzard Oct 09 '17 at 16:16

2 Answers2

0

If you want to have the binary number to be prefixed with 0-s you have to do a bit of work. Here I used a StringBuilder filled with 0s, replacing from the end the binary representation without 0 padding.

for (int i = 0; i <= 0xFF; i++) {
    StringBuilder builder = new StringBuilder("00000000");
    String binary = Integer.toBinaryString(i);
    builder.replace(8 - binary.length(), 8, binary);
    System.out.println(builder);
}
M. le Rutte
  • 3,525
  • 3
  • 18
  • 31
0

I recommend not working with strings of "1" and "0" except for formatting output. Internally you should store your "key" as a byte[8], so:

byte[] key = new byte[8];
for(int i=0; i<6; i++) {
   key[i] = (byte) x + i;
}
key[6] = (byte) 0xff;
key[7] = (byte) 0xff;

As a first shot at converting this to a printable string, you just need:

String result = IntStream.range(0,8)
    .map(i -> key[i])
    .mapToObj(n -> intToBinaryString(n))
    .collect(Collectors.joining(" "));

... and then you need an intToBinaryString() method. There are plenty of SO answers describing how to do this - for example: Print an integer in binary format in Java

slim
  • 40,215
  • 13
  • 94
  • 127
  • 2
    I put "key" in quotes because I really hope this isn't going to be used for serious crypto. It's 64 bits long, but it only contains 8 bits of information. – slim Oct 09 '17 at 15:17