JSFiddle - React, Tailwind, and code Playground
by Ari Viinikainen
HTML
<form>
Viesti: <input id="original" type="text" name="fname"><br>
D → Z ei salausta -
Viesti ja osoitteet <input id="s0" type="text" name="s0"><br>
C → D ROT salaus:
<input type="number" name="rot1" id="rot1" value="7" min="1" max="25"> Salattu <input id="s1" type="text" name="s1"><br>
B → C ROT salaus:
<input type="number" name="rot2" id="rot2" value="9" min="1" max="25"> Salattu <input id="s2" type="text" name="s2"><br>
A → B ROT salaus:
<input type="number" name="rot3" id="rot3" value="4" min="1" max="25"> Salattu <input id="s3" type="text" name="s3"><br>
X → A ROT salaus:
<input type="number" name="rot4" id="rot4" value="17" min="1" max="25"> Salattu <input id="s4" type="text" name="s4"><br>
<button type="button" id="nappi1" style="margin-left:13px" onclick="salaus(4);">Salaa</button>
</form>
<p id="test">t</p>
JavaScript
function salaus(maksi) {
var vc = ['X','A','B','C','D','Z'];
document.getElementById('s0').value = osoitteet(vc[vc.length-1], vc[vc.length-2], document.getElementById('original').value)
for (var i = 0; i < maksi; i++) {
var salattu = cipheri(document.getElementById('s' + i).value,document.getElementById('rot' +(i+1)).value);
document.getElementById('s' +(i+1)).value = salattu;
}
}
function osoitteet(to, from, teksti) {
return from + to + teksti;
}
function cipheri(teksti, rot) {
var modifier = 0;
var cypher = "";
for (var x = 0; x < teksti.length; x++)
{
modifier = 0;
number = parseInt(rot);
charNum = teksti.charCodeAt(x);
if (charNum >= 97 & charNum <= 122) {
modifier = 97;
cypher += String.fromCharCode(((charNum - modifier + number) % 26) + modifier);
}
else if (charNum >= 65 & charNum <= 90) {
modifier = 65;
cypher += String.fromCharCode(((charNum - modifier + number) % 26) + modifier);
}
else {
cypher += String.fromCharCode(charNum);
}
}
//document.getElementById('test').innerHTML = cypher;
return cypher;
}