-2

I am tasked with finding the secret message and I believe that I will have to XOR the different ciphertexts, but I don't understand how to store a ciphertext in a variable in Java.

I could not find out how to store a ciphertext message. I thought that I would do so by declaring it a string (obviously wrong)

String firstmessage = "315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8cc57ee59418ce7dc6bc41556bdb36bbca3e8774301fbcaa3b83b220809560987815f65286764703de0f3d524400a19b159610b11ef3e"

How can I store this value in Java?

MichaelGofron
  • 1,310
  • 4
  • 15
  • 29
  • 1
    Do you know that String literals are placed between double quotes? besides of that your need to convert you cipher text to some bits, i.e. you need to store it in a byte array `byte[]`. – A4L Jul 21 '14 at 20:42
  • How can this be stored as a byte array? By declaring byte[] firstmessage = byte[]. How many elements are in the array? And how would you initialize the elements in the array, using a for loop? – MichaelGofron Jul 21 '14 at 20:45
  • If your input is a hex string then every character represents 4 bits, a `byte` can hold 8 bits. So for a text of the length `n` you will need a byte array of the length `n/2`, see this [question](http://stackoverflow.com/a/140861/1113392) for how to do the conversion. – A4L Jul 21 '14 at 20:49
  • Well, you need a semicolon at the end. But the above should store the value ... as a String literal. One would need to know what you intend to do with it after that. – Hot Licks Jul 22 '14 at 01:10
  • Writing a simple character hex to byte array converter would be a good educational experience for you. – Hot Licks Jul 22 '14 at 01:14

1 Answers1

0

Strangely enough, Java still does not pack hexadecimal decoder in one of the main packages handling bytes etc. The best way is to use one of the hexadecimal decoders from Guava, the Apache Commons Codec or an internal class within the Bouncy Castle provider (the "lightweight API").

Of course, if you want to you can also use the internal DatatypeConverter.parseHexBinary() method, but it is in a rather unrelated XML package in javax. Otherwise, you can use additional methods defined here.

Community
  • 1
  • 1
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • Well, he doesn't appear to need a hex ENcoder, and Long.parseLong(String,int) will parse character hex into long values just fine. One simply needs to build a loop around it. – Hot Licks Jul 22 '14 at 01:13
  • @HotLicks "I could not find out how to store a ciphertext message"...to XOR. How the heck do you come to the conclusion that that should be a hex decoder for long values? +simple typo, thanks for the downvote. – Maarten Bodewes Jul 22 '14 at 07:07