| View previous topic :: View next topic |
| Author |
Message |
Robotex Master Cheater
Reputation: 0
Joined: 05 Sep 2006 Posts: 378 Location: The pizza country!
|
Posted: Sat Oct 10, 2009 11:56 am Post subject: [Help] Java bytes |
|
|
I'm trying to port a cyphering algorithm from C++ to Java, but since I'm new into Java and in Java all the types are signed, I'm having hell troubles trying to figure out how to make this work.
in C++ I have this:
| Code: |
for (unsigned int i = 0; i < 256; ++i)
{
unsigned char bkey = (i*73)^21;
keytable[bkey + 0x100] = i;
}
|
In Java I have something like this
| Code: |
for (byte i = 0; i < 256; i++)
{
bkey = i;
bkey *= (byte)73;
bkey ^= (byte)21;
_keytable[bkey + 256] = i;
} |
Obliviously it won't work because byte is signed, so it won't go above 127, plus it messes up all the rest of the program even if I don't have encrypted/decrypted anything yet!
How do I fix this?
_________________
ASM/C++ Coder
Project Speranza lead developer |
|
| Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Sat Oct 10, 2009 12:41 pm Post subject: |
|
|
Just use short or int. there's no doubt bkey will overflow, unless i < 4, and since Java only has signed integers the sign might also turn into negative, I suggest you to simply use short or int and do this:
| Code: | int bKey;
for (int i = 0; i <= 0xFF; i++)
{
bKey = ((i*73)^21) & 0x7F;
KeyTable[bKey + 0x100] = i;
} |
Or something similar.
|
|
| Back to top |
|
 |
Robotex Master Cheater
Reputation: 0
Joined: 05 Sep 2006 Posts: 378 Location: The pizza country!
|
Posted: Sat Oct 10, 2009 1:05 pm Post subject: |
|
|
I forgot to mention that _keytable is an array of bytes, will java cast i to byte without errors?
EDIT:
I am trying now to sign/unsign them before I manipulate them..
This is my current situation:
| Code: |
private int[] _keytable;
public CryptClass()
{
_keytable = new int[512];
}
public static short toUnsigned(byte b) {
return (short)(b & 0xff);
}
public static int toUnsigned(int b) {
return (int)(b & 0xff);
}
public static byte toSigned(short i) {
return (byte) i;
}
public static byte toSigned(int i) {
return (byte) i;
}
public void InitKey()
{
int bkey;
if(_keytable == null)
return;
for (int i = 0; i <= 255; i++)
{
bkey = ((i*73)^21);
_keytable[i] = bkey; <- this gives me again a bug, however if i put & 0x7F above like you wrote it doesn't do it but the key is still wrong
}
}
public int CryptOutput(int bo)
{
int b = toUnsigned(bo);
if(b == 255 || b == 0)
return bo;
return toSigned(_keytable[b]);
}
|
_________________
ASM/C++ Coder
Project Speranza lead developer |
|
| Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Sat Oct 10, 2009 7:04 pm Post subject: |
|
|
Since the range is within byte's range, you can declate i as byte, there's no problem with that.
I also forgot the main reason I used int instead of byte - so the sign bit of the byte's value will have no effect on the actual sign. just make your calculations with integers after you AND it with 0xFF, not 0x7F, my mistake.
Try this:
| Code: | int bKey; //Notice bKey is int
for (byte i = 0; i <= 0xFF; i++)
{
bKey = (i*73)^21; //Calculate offset
KeyTable[(bKey & 0xFF) + 0x100] = i; //Take low-byte of offset = bKey
} |
|
|
| Back to top |
|
 |
Robotex Master Cheater
Reputation: 0
Joined: 05 Sep 2006 Posts: 378 Location: The pizza country!
|
|
| Back to top |
|
 |
|