@tech4teachers/

Abi's CIPHERJS Mr.B edits

Nodejs

No description

fork
loading
Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

/*function rot13(str){  
  //rotational letter substitution
  var res = [];
  //variable response
  for (var i = 0; i<str.length; i++) {
    if (str.charCodeAt(i)+13 > 122) res.push(String.fromCharCode(96+str.charCodeAt(i)-122+13));  
    //unicode   
    else res.push(String.fromCharCode(str.charCodeAt(i)+13));
    //return string from specified sequence
  }
  return res.join('');
}
rot13('coffee'); 

function rot13(str){
  var decrypt = [];
  for (var i = 0; i>str.length; i--) {
   if (str.charCodeAt(i)-13 < 122) decrypt.push(String.fromCharCode(96-str.charCodeAt(i)+122-13)); 
   else decrypt.push(String.fromCharCode(str.charCodeAt(i)+13));
  }
  return decrypt.join('');
}
rot13('coffee'); */

var key = 13;
var name = prompt("Encrypt or Decrypt?");
if (name == "Encrypt" ) {
  message=prompt("Code for Encryption here:  ");
  enciphering(message);

} 
else if (name == "Decrypt" ) {
  message = prompt("Code for Decryption here:  ");
  deciphering(message);
}
else{console.log("that is not an option...restart!!")}


function enciphering(words){ 
var key = 13;
codeEncr=words;
ciphertext = "";
var library =["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0"," ","!","?","."]    
//var re = /[a-z]/; 
//need to get capital and number to work

//NEVERMIND....SOLVED IT!!!!!.....but understand "why" -1 works...b/c of 0 being the lowest TRUE possibility for an array index number location
for(i=0; i<codeEncr.length; i++){ 
   if(library.indexOf(codeEncr.charAt(i))> -1) {
     j=library.indexOf((codeEncr.charAt(i)))
     ciphertext += library[j+17];
   } 
   else {ciphertext += codeEncr.charAt(i);
   } 
}
console.log("The translation is:  "+"\n"+ciphertext); 
}

//deciphering
function deciphering(words){ 
var key = 13;
codeDecr=words;
ciphertext = "";
var library =["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0"," ","!","?","."]    
var re = /[a-z]/; //need to get capital and number to work
for(i=0; i<codeDecr.length; i++){ 
   // var re and re.test need to go away and ONLY look in the var library

   //NEVERMIND....SOLVED IT!!!!!.....but understand "why" -1 works...b/c of 0 being the lowest TRUE possibility for an array index number location
   if(library.indexOf(codeDecr.charAt(i))> -1) {
     j=library.indexOf((codeDecr.charAt(i)))
     ciphertext += library[j-17];
   } 
   else {ciphertext += codeDecr.charAt(i);
   } 
}
console.log("The translation is:  "+"\n"+ciphertext); 
}

//["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0"," ","!","?","."]

//alert(codeEncr)
//alert(ciphertext)
//alert(codeDecr)
  








/*check if letter is uppercase
function isUpperCase(str) {
    return str === str.toUpperCase();
}

//decipher the string
let ceaserCipher = (str, key) => {
  let decipher = '';
  
  //decipher each letter
  for(let i = 0; i < str.length; i++){
    
    if(isUpperCase(str[i])){
      decipher += String.fromCharCode((str.charCodeAt(i) + key - 65) % 26 + 65);
    }else{
      //else add lowercase letters
      decipher += String.fromCharCode((str.charCodeAt(i) + key - 97) % 26 + 97);
    }
  }
}
*/
node v10.16.0