K
K
kenthex2019-11-29 19:16:25
JavaScript
kenthex, 2019-11-29 19:16:25

How to process arrays?

The problem of multiloop polyalphabetic substitution is given. Given plain text and 2 keys, I can't figure out how to process arrays with this text and keys to get the index of the new character. Screenshot of an example below.
5de1435b63f73613046020.png
Alphabet:
5de14436f1776531502107.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey c0re, 2019-11-29
@kenthex

const al   = "АБВГДЕЁЖЗИКЛМНОПРСТУФХЦЧШЩЫЬЭЮЯ "; // Алфавит
let key1 = "ПЕРВЫЙ"; // первый ключ
let key2 = "БУКВА"; // второй ключ
let open_str = "ЭТО СТРОКА ОТКРЫТОГО ИСХОДНОГО ТЕКСТА"; // кодируемый текст

// т.к. в алфавите нет букв Й и Ъ, то производим соответствующую замену:
key1 = key1.replace("Й","И").replace("Ъ","Ь");
key2 = key2.replace("Й","И").replace("Ъ","Ь");
open_str = open_str.replace("Й","И").replace("Ъ","Ь");

// кодируем текст
let encoded_str = ([...open_str]).map( (e,i) => al[(al.indexOf(e) + al.indexOf(key1[i%key1.length]) + al.indexOf(key2[i%key2.length])) % 32] ).join("");

console.log(encoded_str);

in fact, a string can be accessed as an array - str[index]
about .map look here - https://developer.mozilla.org/en-US/docs/Web/JavaS...
if you need it right here on ARRAYS, then just make the corresponding variables "arrays" the
string into an array can be split like this str.split("") or [...str]
according to the script
al - string / array of letters of the alphabet
al.indexOf(char) - get the index of the letter in alphabet
by choosing a character from the key - because. the key is iterated, then we take the remainder of dividing the index of the encoded character by the length of the key, this will be the character from the key - key1[i% key1.length], then we get its code alphabetically - al.indexOf(key1[i% key1. length])
and you have a lot of errors in the table, for example, in the third line - the final character is not AND, but Z
, etc.

X
xmoonlight, 2019-11-29
@xmoonlight

As I understand it:
There is an alphabet with matching codes: 0 - A, 1 - B, 2 - C, 3 - D, etc.
mod is the remainder of the sum divided by 32 (also "mod", sometimes denoted by a percent sign: "% ").
We add the numbers, divide, get the remainder, look at the letter based on the alphabet.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question