Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.1 kB
2
Indexable
Never
const RUSSIAN_ALPHABET = "abcdefghijklmnopqrstuvwxyz".split('');
    // "абвгдеёжзийклмнопрстуфхцчшщьыъэюя".split('');

module.exports.encrypt = (openText, a, s) => {
    const arrayOfEncryptedLetters = openText.split('').map(openLetter => {
        const P = RUSSIAN_ALPHABET.indexOf(openLetter);
        const C = (a * P + s) % RUSSIAN_ALPHABET.length;
        return RUSSIAN_ALPHABET[C];
    })

    console.log(arrayOfEncryptedLetters.join(''));
};

module.exports.decrypt = (encryptedText, a, s) => {
    const encryptedAlphabet = RUSSIAN_ALPHABET
            .map((_, idx) => RUSSIAN_ALPHABET[(a * idx + s) % RUSSIAN_ALPHABET.length]);
    const arrayOfDecryptedLetters = encryptedText.split('').map(encryptedLetter => {
        const P = RUSSIAN_ALPHABET.indexOf(encryptedLetter);
        const temp1 = P - s < 0 ? P - s + RUSSIAN_ALPHABET.length : P - s;
        const C = (1/a) * (temp1) % RUSSIAN_ALPHABET.length;
        console.log(P, C);
        return RUSSIAN_ALPHABET[C];
    })

    console.log(arrayOfDecryptedLetters.join(''));
}