I’m having hard time reversing this algorithm:
decrypt_algorithm = function (text) {
var parser = new TextParser(text);
var decrypt_key = [16, 19, 17, 7, 20, 23, 13, 1, 24, 15, 6, 12, 0, 18, 21, 2, 9, 14, 3, 10, 5, 25, 8, 4, 22, 11];
var text_size = parser.getSize();
var text_size_without_last_part = text_size - 26;
var output = [];
if (text_size_without_last_part > 0) {
for (var m = 0; m < text_size_without_last_part; m += 26) {
var word_to_decrypt = [];
for (var k = 0; k < 26; k++) {
word_to_decrypt[k] = parser.readNextChar()
}
for (var k = 0; k < 26; k++) {
output[m + k] = word_to_decrypt[decrypt_key[k]]
}
}
var parser_position = parser.getPosition();
var last_part_size = text_size - text_size_without_last_part - 1;
if (last_part_text > 0) {
for (var k = 0; k < last_part_size; k++) {
output[text_size_without_last_part + k] = parser.readNextChar()
}
}
}
return output;
};
I don’t have any reverse-engineering knowledge, so if you can point me to some resources which explains how to “reverse” a function, it will be really helpful.
I don’t need the exact code as an answer, but some guides on how to reverse engineer it by my own is really appreciated.
Edit 1:
After taking a pause and thinking about the algorithm a bit, I understood the problem and it was easier than I thought:
output[decrypt_key[k]] = word_to_decrypt[m + k]
Changing this will revert the algorithm, solving the problem.
It is a simple permutation cipher. Essentially the
decrypt_keyis used to rearrange the outputor, rearranged:
The code
essentially takes the index into the code being “encrypted” and permutes it’s words in 26 piece chunks.
You can think of it as just scrambling the words based on the “key”, which tells you how it is being scrambled.
e.g,
which means that the first word(location 0) gets put at location 16 and the word at location 16 gets put at location 9.
Another term would be to call it a shuffle cipher. It shuffles up all the words, but in a very predictable manner.
As long as you know the
decypher_keyand the type of cipher you can recover the data quite easily just as you could with a deck of cards if you knew exactly how they were shuffled(since it’s not truly random).To make the inverse cipher you must create the opposite key. If location 0 went to 16 then that means location 16 would have to go to 0. Do this for each number in the array and form a new array called ‘encrypt_key’ and then you have a way to encrypt words that can be properly decrypted with the code you have given.
Lua code:
output: