Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[Help] Java bytes

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Robotex
Master Cheater
Reputation: 0

Joined: 05 Sep 2006
Posts: 378
Location: The pizza country!

PostPosted: Sat Oct 10, 2009 11:56 am    Post subject: [Help] Java bytes Reply with quote

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
View user's profile Send private message
Deltron Z
Expert Cheater
Reputation: 1

Joined: 14 Jun 2009
Posts: 164

PostPosted: Sat Oct 10, 2009 12:41 pm    Post subject: Reply with quote

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
View user's profile Send private message
Robotex
Master Cheater
Reputation: 0

Joined: 05 Sep 2006
Posts: 378
Location: The pizza country!

PostPosted: Sat Oct 10, 2009 1:05 pm    Post subject: Reply with quote

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
View user's profile Send private message
Deltron Z
Expert Cheater
Reputation: 1

Joined: 14 Jun 2009
Posts: 164

PostPosted: Sat Oct 10, 2009 7:04 pm    Post subject: Reply with quote

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
View user's profile Send private message
Robotex
Master Cheater
Reputation: 0

Joined: 05 Sep 2006
Posts: 378
Location: The pizza country!

PostPosted: Sun Oct 11, 2009 4:43 am    Post subject: Reply with quote

Thanks a lot, it works as a charm now Wink
_________________

ASM/C++ Coder
Project Speranza lead developer
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites