Skip to content

Commit

Permalink
#4 basic caesar cipher setup
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewNobes committed May 22, 2022
1 parent 4ce4a77 commit b4db346
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/ciphers/caesar-cipher/caesarEncipher.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
export const caesarEncipher = (key, message) => {
console.log(key);
console.log(message);
let cipherText = '';

[...message].forEach((letter) => {
const letterASCIIValue = letter.charCodeAt(0) - 97;
let shiftedASCIIValue = letterASCIIValue + key;
console.log(shiftedASCIIValue);
if (shiftedASCIIValue > 25) {
shiftedASCIIValue = shiftedASCIIValue - 26;
}
const cipheredLetter = String.fromCharCode(shiftedASCIIValue + 97);
cipherText += cipheredLetter;
});
console.log(cipherText);
return cipherText;
};
7 changes: 5 additions & 2 deletions src/routes/caesar-cipher.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
<textarea id="message" name="message" />
<p>Please select a key to shift by:</p>
<input type="number" id="key" name="key" min="0" max="25" />
<p>Please note, shifting by a key greater than 25 will act as a shift from 0.</p>
<p>Result</p>
<textarea id="output" readonly />
<button on:click={() => caesarEncipher(key.value, message.value)}>Encrypt</button>
<textarea readonly id="ciphertext" name="ciphertext" />
<button on:click={() => (ciphertext.value = caesarEncipher(parseInt(key.value), message.value))}
>Encrypt</button
>
</div>

0 comments on commit b4db346

Please sign in to comment.