Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6816097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:53:06+00:00 2026-05-26T20:53:06+00:00

I tried to convert some C code to Java, but it is working slightly

  • 0

I tried to convert some C code to Java, but it is working slightly different. It is XOR encryption and for some data it returns the same results, so I know it is pretty close, but for some data it doesn’t work exactly the same (different results).

C code (runs on x86 Windows, compiled with Borland Builder):

void Crypt(unsigned char *data,long len)
{
char key[] = "X02$B:";
ULONG crypt_ptr;
long x;

   for(crypt_ptr=0,x=0;x<len;x++)
      {
      data[x] ^= key[crypt_ptr];
      if(crypt_ptr < (sizeof(key) - 2))
         key[crypt_ptr] += key[crypt_ptr + 1];
      else
         key[crypt_ptr] += key[0];
      if(!key[crypt_ptr])
         key[crypt_ptr] += (char) 1;
      if(++crypt_ptr >= sizeof(key) - 1)
         crypt_ptr = 0;
      }
}

Java Code (which runs on the Android platform if it matters):

public static void Crypt(byte[] data,int offset,int len) 
{ 
    // EDIT: Changing this to byte[] instead of char[] seems to have fixed code
    //char[] key = {'X','0','2','$','B',':'};
    byte[] key = {'X','0','2','$','B',':'};
    int size_of_key = 7;
    int crypt_ptr; 
    int x; 

    for(crypt_ptr=0,x=0;x<len;x++) 
    { 
        data[x+offset] ^= key[crypt_ptr]; 
        if(crypt_ptr < (size_of_key - 2)) 
            key[crypt_ptr] += key[crypt_ptr + 1]; 
        else 
            key[crypt_ptr] += key[0]; 
        if(key[crypt_ptr] == 0) 
            key[crypt_ptr] += (char) 1; 
        if(++crypt_ptr >= size_of_key - 1) 
            crypt_ptr = 0; 
    } 
}

I have confirmed that the data going in to each function is the same, and for the Java version I am passing in the correct offset value within the byte array. As mentioned, it works sometimes, so I don’t think it is a major/obvious issue, more like some minor issue between signed vs unsigned values. If it helps at all, the first byte that was different was at byte 125 (index 124 if zero-based) in the data. I didn’t see a pattern though, like every 125 bytes, it was pretty much random after that. The data is only 171 bytes, and I can figure out how to post as an attachment probably if needed, but I don’t think it is.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:53:07+00:00Added an answer on May 26, 2026 at 8:53 pm

    I guess it’s because char is 16-bit in java. So when you increment key key[crypt_ptr] += (char) 1 or add two chars key[crypt_ptr] += key[crypt_ptr + 1], it acts in different way from c (where char is 8-bit).

    Try to use bytes everywhere instead of chars, just use symbol codes for initialization.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to call some Java library code that returns an untyped java.util.List and
I tried to use java.io.FileReader to read some text files and convert them into
I want to convert a some code which is in Java to C#. Java
I tried many things but I always get cannot convert string to membershipuser from
I've got some code that initializes OpenGL to render to a java.awt.Canvas. The problem
I am trying to get some code working from an example I came across.
I am writing some code in VB.NET that uses a switch statement but in
I am working in Java, inserting data from HashMaps of certain types into a
As part of my Final Year Project, I need to convert some FORTRAN code
I have written some code which sends queries to google and returns the query

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.