RC5 implementation

key to byte array

Page 1 of 1

6 Replies - 4906 Views - Last Post: 21 December 2009 - 11:11 AM Rate Topic: -----

#1 dc333  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 19-August 08

RC5 implementation

Posted 19 December 2009 - 01:53 PM

Hi.

We were given a project to implement the RC5 encryption algorithm in java. I've been stuck for hours now on just getting the 128 key in string format and transferring it into a byte array I can work with to preform bit-wise operations, as required by the algorithm. First I tried to just turn the string to a binary number:

Long hexNum = Long.parseLong(keyString, 16);
System.out.println(Long.toBinaryString(hexNum));



but then remembered that the string represents a 128 bits unsigned integer (can be even longer), so the Long variable doesn't really help.
I know it seems like I haven't done much, but I've been sitting on this for hours and haven't made any progress (maybe I didn't get the idea of the algorithm?), so if any of you can help, please do.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: RC5 implementation

#2 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: RC5 implementation

Posted 19 December 2009 - 02:18 PM

Why not use BigInteger?

BigInteger hexNum = new BigInteger(keyString, 16);
System.out.println(hexNum.toString(2));


Was This Post Helpful? 0
  • +
  • -

#3 dc333  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 19-August 08

Re: RC5 implementation

Posted 19 December 2009 - 02:33 PM

First of all, thanks for taking the time to reply :)

Second, I thought of using BigInteger, but then there's the whole unsigned problem...
Any thoughts on that?

Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: RC5 implementation

Posted 19 December 2009 - 03:02 PM

View Postdc333, on 19 Dec, 2009 - 08:33 PM, said:

Any thoughts on that?



Not really, I think you'll have to deal with signed-ness issues yourself, as Java doesn't have any unsigned data types (apart from char which represents a 16-bit unsigned integer)
Was This Post Helpful? 0
  • +
  • -

#5 dc333  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 19-August 08

Re: RC5 implementation

Posted 19 December 2009 - 03:05 PM

Ok, thanks man, I really appreciate it :)
Was This Post Helpful? 0
  • +
  • -

#6 dc333  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 19-August 08

Re: RC5 implementation

Posted 19 December 2009 - 07:55 PM

So, after a few more hours of reading, I managed to do this:

key = new byte[keySize/2];
for(int i=0; i<keySize; i+=2){
	key[i/2] = Byte.parseByte(keyString.substring(i,i+2),16);
	System.out.println(key[i/2]);
}



Only to realize again that since java doesn't have unsigned bytes, I can't represent all 2 hex characters. So I read about using short with logic-and operations to do that (also here). The thing is that when you do that, you add 0's at the beginning of the number, which is fine when you only want to deal with that one number, but I have an array of bytes which together represent one big 128 bit unsigned integer. Using bit-wise operations on the bytes become irrelevant like that.
SO... does anyone have any bright ideas ('comn, people I'm loosing it here :))
Thanks!
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: RC5 implementation

Posted 21 December 2009 - 11:11 AM

Quote

Second, I thought of using BigInteger, but then there's the whole unsigned problem...
Any thoughts on that?


That's not a problem. The number will be interpreted as positive in the absence of any preceding minus sign and therefore is unsigned
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1