Octarine-Phaneron / Ceasar-Cipher

A simple JS Ceasar Cipher

Home Page:https://raw.githack.com/Octarine-Phaneron/Ceasar-Cipher/master/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JavaScript Simple Ceasar Cipher

Try it HERE

I just tried to make a JS ceasar cipher, it might not be the best way to do it. Here is the code :

window.onload = function(){

  // Get html elements
  let clearTextArea = document.getElementById("clear");
  let encryptedTextArea = document.getElementById("encrypted");
  let shiftValueInput = document.getElementById("shiftValue");

  // init default values
  // set ASCII constants
  const a = 97;
  const z = 122;
  const A = 65;
  const Z = 90;
  let shiftValue = 0;
  let clearText = "";
  let encryptedText = "";

  // starts the loop to check if anything's changed every 1.5s.
  let loop = setInterval(check, 1500);

  /* Checks the value given by the user, if it's a number
   * sets it as is, if it's not, sets 0 */
  function setShiftValue(){
    if( isNaN( parseInt(shiftValueInput.value) ) ){
      return 0;
    }
    return parseInt(shiftValueInput.value);
  }

  /* Checks if the text has changed, if it has,
   * gets the new text and sends it to the encrypt function  */
  function check(){
    // Removes diacritics
    normalizedClearText = clearTextArea.value.normalize('NFD').replace(/[\u0300-\u036f]/g, "");
    if( clearText != normalizedClearText || shiftValue != setShiftValue() ){
        shiftValue = setShiftValue();
        clearText = normalizedClearText
        encryptedText = encrypt(clearText);
        encryptedTextArea.value = encryptedText;
    }
  }
    /* For each letter of the clear text, checks what it's ASCII code is. then shifts its value accordingly.
     * Returns encrypted text */
  function encrypt(clearText){
      let tempEncrypted = "";
      let charAscii;
      for(let i = 0; i < clearText.length; i++){
        char = clearText.charAt(i);
        // if char i is a lowercase letter
        if( char.charCodeAt(0) >= a && char.charCodeAt(0) <= z ){
          char = shift( char, a, z );
        }
        // else if it is an uppercase letter
        else if( char.charCodeAt(0) >= A && char.charCodeAt(0) <= Z ){
          char = shift( char, A, Z);
        }
        // Other chars aren't encrypted
        tempEncrypted += char;
      }
      return tempEncrypted;
  }

    /* For a given char ASCII code, returns a char
    *  equals to the shifted ASCII code value.
    *  if the shifted value is out of the defined boundaries
    * ( <minValue || >maxValue), the value is set by looping
    * through the given range ( if min=10 & max=20, 25
    * becomes 14). */
  function shift( char, minValue, maxValue ){
      charCode = char.charCodeAt(0)
      charCode += shiftValue;
      while(charCode > maxValue){
        charCode = minValue + (charCode - maxValue) - 1;
      }
      while(charCode < minValue){
        charCode = maxValue - (minValue - charCode) + 1;
      }
      return String.fromCharCode(charCode);
  }

}

About

A simple JS Ceasar Cipher

https://raw.githack.com/Octarine-Phaneron/Ceasar-Cipher/master/


Languages

Language:JavaScript 43.4%Language:CSS 40.0%Language:HTML 16.6%