0

So, I am implementing a DES (Data Encryption Standard) in Java and need to declare the key as a binary value of 64 bits and do not know how to do this. Could anyone help me?

I would like to say something like this: int = K 000100110011010001010111011110011001101110111100110111111110001;

However, the IDE always beeps error saying that it is a very large integer.

Jason C
  • 38,729
  • 14
  • 126
  • 182
Elnisky
  • 13
  • 4

2 Answers2

2

If you are using Java 7 or higher, you can specify binary constants by prepending "0b" to your value, e.g.:

long value = 0b000100110011010001010111011110011001101110111100110111111110001L;

If that language feature is not available to you, you can use decimal, octal, or hexadecimal notation, however. If you're just declaring constants, you can use an online tool such as http://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html, or perhaps your favorite calculator, to convert to a radix that Java recognizes.

For example:

long value = 691913582662545393L;

Or:

long value = 0x99A2BBCCDDE6FF1L;

You may want to clarify by describing what that value means in a comment:

// binary: 000100110011010001010111011110011001101110111100110111111110001
long value = 0x99A2BBCCDDE6FF1L;

As you may have noticed from the above examples, you will need to use a long and add the L suffix to your constant. In Java, int is 32-bit and long is 64-bit. The L suffix specifies a long literal, without it, it is an int and the value would be too large.

P.S. Integer literals starting with "0" (but not "0x" or "0b") are interpreted as octal. The constant you originally attempted to specify was interpreted as a very large octal number.

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • 1
    Using the hexa notation it doesn't works `private static long K = 0x133457799BBCDFF1;` It says that integer number is too large. I'm following this link: http://orlingrabbe.com/des.htm Thanks! – Elnisky Nov 20 '13 at 00:41
  • @Elnisky You appear to have missed what I wrote about the `L` suffix. – Jason C Nov 20 '13 at 01:16
2

Put 0b in front of your number to tell Java it's in binary (only for Java 7 and up). Otherwise, convert to hex.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152