How to encrypt string method "Rail fence"?

str="echotexture";

the <pre> e K I n
 t e s W f a
 o t t a R in
 t W o
 </pre>
June 27th 19 at 15:17
1 answer
June 27th 19 at 15:19
Solution
For starters a separate exercise from the index letters in the source string to get the index of the row in which to put it.

We denote the number of rows variable rows. This period "saw" from the top peak to the next top-peak is 2 * (rows - 1). The remainder of the division in this period will mark the position inside a "tooth":
balance: 0 1 2 3 4 5
 line: 0 1 2 3 2 1 – it wants eventually to get


To calculate "reflected" – when he went down and then began to go up – need to adjust this time and make the intersection of the axis y = 0 and then the reflection is easily done by taking the absolute value of the Math.abs():
y : 3 2 1 0 -1 -2
abs: 3 2 1 0 1 2


Modulo is always positive and increasing: 0 1 2 3 4 5and need to get out of it 3 2 1 0 -1 -2. Multiply by -1 and add 3: Math.abs(rows - 1 - ost)

Total code like this will give the correct number of rows where to insert a character, receiving the input i is the index of the letter in the source string:
var i, ost, row, rows=4, period = 2 * (rows - 1); // 0 0 0 | 6 0 0
for(i = 0; i < 10; i++) { // 1 1 1 | 7 1 1
 ost = i % period; // 2 2 2 | 8 2 2
 row = rows - 1 - Math.abs(rows - 1 - ost); // 3 3 3 | 9 3 3
 console.log( i, ost row); // 4 4 2 | 10 4 2
} // 5 5 1 | 11 5 1


Assembly will look something like this:
function encrypt( text, n) {
 var i, ost, period = 2 * (n-1), r, row, out = [];
 text = text.replace(/\s/g,""); // remove the spaces
 for(r = 0; r < period; r++) out[r] = ""; // empty string
 for(i = 0; i<text.length; 1 i++) { ost="i" % period; row="n" math.abs(n. ost) out[row] +="text.substr(i,1);" } return out.join(""); function test( text, rows) var pre="document.createElement("pre");" pre.innerhtml="; encrypt(text" rows); document.body.appendchild(pre); test("this is encrypted text", 4); eqinterface< code></text.length;>
br><a href="https://jsfiddle.net/sergiks/obj0sxo9/5/" rel="nofollow" target="_blank">jsFiddle</a><br><br><b>Upd. </b>just rewrote this. Encryption and decryption are based on the same "card", which depends only on the number of letters in the text and number of rows. So I made one of f th that create this card. And slightly different short the functions of encryption and decryption. <a href="https://jsfiddle.net/sergiks/Lo7bds4h/" rel="nofollow" target="_blank">Fiddl form</a>. Code the encryption/decryption like this:<pre><code class="javascript">function makeMap( len, n) { var i, pip, period = 2 * ( n - 1); var rows = Array.apply( null, Array( n)).map( function(){ return []}); for( i = 0; i < len; i++) { pip = i % period; r = pip < ( n - 1) ? pip : period - pip; rows[ r].push( i); } return Array.concat.apply( null, rows); } function decrypt( text, n) { var map = makeMap( text.length, n); return text.split(").reduce(function(p,c,i,a){ return p + a[map.indexOf(i)]},"); } function encrypt( text, n) { var map = makeMap( text.length, n); return text.split(").reduce(function(p,c,i,a){ return p + a[map[i]]},"); }</code></pre>
and can you somehow contact in VK? - daphnee95 commented on June 27th 19 at 15:22
: now can. In General, write. - Lorena commented on June 27th 19 at 15:25
: you can link? - daphnee95 commented on June 27th 19 at 15:28
: https://vk.com/serge.sokolov - Kameron_Hilpert34 commented on June 27th 19 at 15:31

Find more questions by tags JavaScript