Y
Y
Yeldos Adetbekov2016-12-06 23:24:00
JavaScript
Yeldos Adetbekov, 2016-12-06 23:24:00

How to write a decoder if the algorithm is known?

Hello, I'm curious how to make decoders based on an existing algorithm?
I am doing this for informational purposes.
There is an encoder code, I want to get, as it were, an inverse function (Just like in math analysis)

function shifr(text) {
            var alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";//АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяӘІҢҒҮҰҚӨҺәіңғүұқөһ";
            var key = getkey();// 'nx9MwEJN2KZ6e1NPiCAaHTxn9QX0iXcjLUoY9fdpc6J';
            var len = alphabet.length;
            var table = new Array();
            for (var i = 0; i < len; i++) {
                table[i] = alphabet.substring(i) + alphabet.substring(0, i);
            }
            var word = text;
            var shfr = "";
            while (key.length < word.length)
                key += key;
            for (var i = 0; i < word.length; i++) {
                if (alphabet.indexOf(word[i]) > -1) {
                    shfr = shfr + table[alphabet.indexOf(key[i])][alphabet.indexOf(word[i])];
                    }
                else
                    shfr = shfr + word[i];
            }
            return shfr;
        }

Here's how it works:
"0" -> "W"
"00" -> "Wg"
"000" -> "WgE"
"0000" -> "WgEM"
I noticed that everything works statically, the getkey function is not bound to anything (everything is static)
Please help me figure it out.)))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lander, 2016-12-06
@usdglander

On vskidku, it is necessary to generate simply the return shift table.

for (var i = 0; i < len; i++) {
    table[i] = alphabet.substring(0, i) + alphabet.substring(i);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question