1

I'm currently working on a small challenge, trying to figure out how an unnamed encryption algorithm works. The original algorithm looks like this:

public final String a(byte[] original)
{
    this.a = original.length;
    byte[] solution = new byte[8];

    int i = 0;

    int base = 13;

    for (int si = 0; si < 8; si++)
    {
        for (int oi = 0; oi < a; oi++)
        {
            byte current = original[oi];

            solution[i] = ((byte)(solution[i] + (current ^ base)));
            base = (base ^ i) + solution[i];

            i = (i + 1) % 8;
        }
    }
    char[] result = new char[8];
    for (int n = 0; n < 8; n++) {
        result[n] = ((char)((solution[n] & 0x3F) + 48));
    }
    return String.valueOf(result);
}

So every string that gets passed to this function as a byte[] array will be encoded into a 8-char somewhat cryptic text. I've found out other things about this:

  • The encoded characters in the char[] result always have literals with values between 48 and 111 (0x3F + 48).
  • When decoding, the first step would be subtracting 48 and then undo the & operation. Since 0x3F equals the binary representation 111111, the value of the original byte is one of 4 possibilities:
    • 00xxxxxx: the missing 2 bits were both zero.
    • 01xxxxxx: the lower addressed bit of both was one.
    • 10xxxxxx: the higher addressed bit of both was one.
    • 11xxxxxx: both of them were one.

Meaning, it could be one of four characters. I initially thought about reversing the algorithm, but I'm asking you if this is even possible for this kind of algorithm. I tried it and came this far:

public static String b(String encrypted) {

    byte[][] matrix = new byte[4][20];
    byte[] word = encrypted.getBytes();

    for(int i = 0; i < 4; i++) {

        for(int j = 0; j < word.length; j++) {

            byte tmp = (byte)(word[i] - 48);
            matrix[i][j] = (byte)(tmp + i);
        }
    }

}

I currently subtract 48 and insert all 4 possibilities into a 2D-array. But im stuck solving the nested for loop, especially the variables i and base are hard to find out. The only information I have is the encrypted word and the fact that the original word was 20 literals long at MAX (Hence the [4][20] dimensions).

The encryption doesn't look familiar to me, which leaves me no options to look for the name of this algorithm.

If it is possible to reverse this algorithm, what would my next step be?

CRoemheld
  • 889
  • 7
  • 26
  • 3
    This is not encryption. This is a hash function. – selbie Aug 04 '17 at 01:02
  • @selbie I'm sorry, you are right, I just had to read the exact difference between both of them on https://stackoverflow.com/questions/4948322/fundamental-difference-between-hashing-and-encryption-algorithms :) – CRoemheld Aug 04 '17 at 01:07
  • Try to convert encrypted string into char array and convert every single character into according to your conversion criteria. So you would reverse it. – Tehmina Aug 04 '17 at 01:24
  • @Tehmina That's what I'm trying to do. I posted all my doings and research in the question, but reversing isn't possible (for now/ever), since I don't know how to reverse the nested for loops. – CRoemheld Aug 04 '17 at 01:27

1 Answers1

2

No, that obviously can't be reversible in the general case.

There are effectively 40 bits of information in the output (eight bytes, at 5 bits each -- & 0x1F limits each one to five bits). This means that there are only 240 possible outputs; there are far more possible inputs than that.

If there are some constraints on the input -- for instance, if its length is known to be short -- it might be possible to make some inferences about that. However, you haven't stated any constraints, so…

  • Okay, so there definitiely no way to find the correct one. How about the nested for-loop? Is this one reversible? I understand that there are too many possibilities, however it would be a help in this specific situation to fill in the matrix with the possible values. – CRoemheld Aug 04 '17 at 01:11
  • Working out the "possible values" is unlikely to be helpful in analyzing this algorithm. –  Aug 04 '17 at 01:15