3

I have this java function that decode 8bytes text string(encoded as int[]):

public static int[] decode(int[] text, int[] key) {
   int j = 0x3c; //value 60 in decimal
   int i = 0x33; //value 51 in decimal
   int[] result;

   for ( i = 0x33; ; (text[1]) ^= ((key[j]) ^ (text[4])) + i ){
       j = j - 1;
       text[7] ^= ((key[j--]) ^ (text[3])) + i;
       text[6] ^= ((key[j--]) ^ (text[2])) + i;
       text[5] ^= ((key[j]) ^ (text[1])) + i;
       j = j - 1;
       result = text;
       (text[4]) ^= ((key[j]) ^ text[0]) + i;
       if ( j <= 0 )
           break;
       j = j - 1;
       text[0] ^= ((key[j--]) ^ (text[7])) + i;
       text[3] ^= ((key[j]) ^ (text[6])) + i;
       text[2] ^= ((text[4]) ^ (text[5])) + i;
       j = j - 1;
       //System.out.println("i " +i+" - j "+j);
   }
return result;
}

but if I wanted to do the reverse, how can build the encode function? how do I build the inverse method(from decoded text to encode text)?

4

For

a = b ^ c

Since the XOR operation is trivial to reverse when any 2 values in a, b, c are known, this will only require you to just reverse the order in which operations were done. I had to do this in C since I don't use Java.

int *encode(int *text, int *key) {
  int j = 0x0;
  int i = 0x33; // value 51 in decimal

  for (i = 0x33;;) {
    text[4] ^= ((key[j++]) ^ (text[0])) + i;
    text[5] ^= ((key[j++]) ^ (text[1])) + i;
    text[6] ^= ((key[j++]) ^ (text[2])) + i;
    text[7] ^= ((key[j++]) ^ (text[3])) + i;
    text[1] ^= ((key[j++]) ^ (text[4])) + i;
    if (j >= 0x3c)
      break;
    text[2] ^= ((text[4]) ^ (text[5])) + i;
    text[3] ^= ((key[j++]) ^ (text[6])) + i;
    text[0] ^= ((key[j++]) ^ (text[7])) + i;
  }
  return text;
}

Here's the full code that works.

#include <stdio.h>

int *decode(int *text, int *key) {
  int j = 0x3c; // value 60 in decimal
  int i = 0x33; // value 51 in decimal

  for (i = 0x33;;) {
    text[1] ^= ((key[j--]) ^ (text[4])) + i;
    text[7] ^= ((key[j--]) ^ (text[3])) + i;
    text[6] ^= ((key[j--]) ^ (text[2])) + i;
    text[5] ^= ((key[j--]) ^ (text[1])) + i;
    text[4] ^= ((key[j--]) ^ (text[0])) + i;
    if (j <= 0)
      break;
    text[0] ^= ((key[j--]) ^ (text[7])) + i;
    text[3] ^= ((key[j--]) ^ (text[6])) + i;
    text[2] ^= ((text[4]) ^ (text[5])) + i;
  }
  return text;
}

int *encode(int *text, int *key) {
  int j = 0x0;
  int i = 0x33; // value 51 in decimal

  for (i = 0x33;;) {
    text[4] ^= ((key[j++]) ^ (text[0])) + i;
    text[5] ^= ((key[j++]) ^ (text[1])) + i;
    text[6] ^= ((key[j++]) ^ (text[2])) + i;
    text[7] ^= ((key[j++]) ^ (text[3])) + i;
    text[1] ^= ((key[j++]) ^ (text[4])) + i;
    if (j >= 0x3c)
      break;
    text[2] ^= ((text[4]) ^ (text[5])) + i;
    text[3] ^= ((key[j++]) ^ (text[6])) + i;
    text[0] ^= ((key[j++]) ^ (text[7])) + i;
  }
  return text;
}

int key[] = {0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
             0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
             0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
             0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
             0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
             0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41};
int text[] = {0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68};

int main(int argc, char **argv) {
  int i, *d, *e;
  for (i = 0; i < 8; i++) {
    printf("%x::", text[i]);
  }
  putchar(10);
  d = decode(text, key);
  for (i = 0; i < 8; i++) {
    printf("%x::", d[i]);
  }
  putchar(10);
  e = encode(text, key);
  for (i = 0; i < 8; i++) {
    printf("%x::", e[i]);
  }
  putchar(10);
}

This produces the following output

61::62::63::64::65::66::67::68::
65a::3e9::64a::bd6::5c2::11::1a9::85e::
61::62::63::64::65::66::67::68::

I was able to get back the original array after a decode and encode.

  • thanks @sudhackar, i've understand the procedure. The code is ok. int *encode(int *text, int *key) { int j = 0x0; int i = 0x33; // value 51 in decimal for (i = 0x33;;) { text[4] ^= ((key[j++]) ^ (text[0])) + i; text[5] ^= ((key[j++]) ^ (text[1])) + i; text[6] ^= ( – Mark Last Jr Sep 18 '18 at 10:56
  • @MarkLastJr If this is the solution you're looking for mark accepted and close it. – sudhackar Sep 18 '18 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.