1

I am doing a this project on the page about:blank on chrome, meaning there is no other code except for JavaScript involved.

I am trying to make an encryption algorithm with the knowledge that I have. Here is the code so far:

function main() {
    var input = prompt("Enter a string")
    if (input.substring(0, 1) == "a") {
      var _01 = "a"
      var _01new = _01.replace("a", "1")
    }
    else if (input.substring(0, 1) == "b") {
      var _01 = "b"
      var _01new = _01.replace("b", "2")
    }
    else if (input.substring(0, 1) == "c") {
      var _01 = "c"
      var _01new = _01.replace("c", "3")
    }
    else if (input.substring(0, 1) == "d") {
      var _01 = "d"
      var _01new = _01.replace("d", "4")
    }
    else if (input.substring(0, 1) == "e") {
      var _01 = "e"
      var _01new = _01.replace("e", "5")
    }
    else if (input.substring(0, 1) == "f") {
      var _01 = "f"
      var _01new = _01.replace("f", "6")
    }
    else if (input.substring(0, 1) == "g") {
      var _01 = "g"
      var _01new = _01.replace("g", "7")
    }
    else if (input.substring(0, 1) == "h") {
      var _01 = "h"
      var _01new = _01.replace("h", "8")
    }
    else if (input.substring(0, 1) == "i") {
      var _01 = "i"
      var _01new = _01.replace("i", "9")
    }
    else if (input.substring(0, 1) == "j") {
      var _01 = "j"
      var _01new = _01.replace("j", "10")
    }
    else if (input.substring(0, 1) == "k") {
      var _01 = "k"
      var _01new = _01.replace("k", "11")
    }
    else if (input.substring(0, 1) == "l") {
      var _01 = "l"
      var _01new = _01.replace("l", "12")
    }
    else if (input.substring(0, 1) == "m") {
      var _01 = "m"
      var _01new = _01.replace("m", "13")
    }
    else if (input.substring(0, 1) == "n") {
      var _01 = "n"
      var _01new = _01.replace("n", "14")
    }
    else if (input.substring(0, 1) == "o") {
      var _01 = "o"
      var _01new = _01.replace("o", "15")
    }
    else if (input.substring(0, 1) == "p") {
      var _01 = "p"
      var _01new = _01.replace("p", "16")
    }
    else if (input.substring(0, 1) == "q") {
      var _01 = "q"
      var _01new = _01.replace("q", "17")
    }
    else if (input.substring(0, 1) == "r") {
      var _01 = "r"
      var _01new = _01.replace("r", "18")
    }
    else if (input.substring(0, 1) == "s") {
      var _01 = "s"
      var _01new = _01.replace("s", "19")
    }
    else if (input.substring(0, 1) == "t") {
      var _01 = "t"
      var _01new = _01.replace("t", "20")
    }
    else if (input.substring(0, 1) == "u") {
      var _01 = "u"
      var _01new = _01.replace("u", "21")
    }
    else if (input.substring(0, 1) == "v") {
      var _01 = "v"
      var _01new = _01.replace("v", "22")
    }
    else if (input.substring(0, 1) == "w") {
      var _01 = "w"
      var _01new = _01.replace("w", "23")
    }
    else if (input.substring(0, 1) == "x") {
      var _01 = "x"
      var _01new = _01.replace("x", "24")
    }
    else if (input.substring(0, 1) == "y") {
      var _01 = "y"
      var _01new = _01.replace("y", "25")
    }
    else if (input.substring(0, 1) == "z") {
      var _01 = "z"
      var _01new = _01.replace("z", "26")
    }
    else (input.substring(0, 1) == " ")
    {
      var _01 = " "
      var _01new = _01.replace(" ", "27")
    }

    alert(_01new)
}  

main()

I know that there is a lot of code, and I am going to simplify it, but first I am trying to get this to work.

What I am trying to do, is when the user enters a string (1 letter currently) it will find the value of the first letter, detect what it is, and set the value of _01 to whatever the user put in, then use _01.replace to replace the the letter with its assigned value (currently the letters numerical value).

CDspace
  • 2,639
  • 18
  • 30
  • 36
Kyle Garner
  • 25
  • 1
  • 4
  • 6
    You haven't asked a question. – isherwood Jan 12 '18 at 17:51
  • 1
    `_01.replace("a", "1")` why don't you simply `var _01new = 1` ? :) (I still don't know that's you idea behind such algorithm...) – Roko C. Buljan Jan 12 '18 at 17:56
  • 1
    You may want to google "switch case javascript". – connexo Jan 12 '18 at 17:57
  • Why don't you just replace all of that code with `rot13`? https://stackoverflow.com/a/41627755/362536 – Brad Jan 12 '18 at 17:59
  • Btw, doing 27 `if...else if...else` is the point where even the dumbest guy must have noticed that you actually **need** an algorithm, not that you *have* one. – connexo Jan 12 '18 at 18:00
  • 1
    I already see a **problem you'll soon encounter** . If you encode `mm` to `1313` tell me, how would you possibly know the decode characters? it could be decoded as `acac` ... (and that's only one out of 4) I suggest you to go back to the whiteboard. – Roko C. Buljan Jan 12 '18 at 18:00
  • `else (input.substring(0, 1) == " ")`.... else statements do not have a condition part, take that out and try again or change it to `else if`. That is going to act as the code for the else block. The rest is just going to act like a normal block of code and going to be executed no matter what – Patrick Evans Jan 12 '18 at 18:01
  • @Brad He could as well replace that code by `void(0)` with regard to encryption value. – connexo Jan 12 '18 at 18:01
  • @RokoC.Buljan he should go for powers of 2... – Eduardo Rocha Jan 12 '18 at 18:05
  • @EduardoRocha or use an Array? ... The point is clear :) – Roko C. Buljan Jan 12 '18 at 18:06
  • @RokoC.Buljan I see... seems good to me :p – Eduardo Rocha Jan 12 '18 at 18:08
  • @RokoC.Buljan When I actually make the characters random, I plan on starting every variable with a capital and then random lowercase letters for example, Wgfb = a and Wfbg = b. – Kyle Garner Jan 12 '18 at 18:15

1 Answers1

1

Your logic will fail for multiple characters.
Your idea of encode(/decode) can only work exclusively if and only if a user enters one character.

What if a user enters say "mm" as string. It's encoded as "1313" and you'll never know how to decode it correctly (well, unless you use array [13,13] or some other ciphering sorcery ;) )
I'll leave this to you.

Now, the answer/suggestion for one character - to - number...

// Create an Object literal with mapped character replacements
var map = {
  "a" : 1,  // Or use some other fancy UTF8 characters or shuffle the order
  "b" : 2,
  "c" : 3,
  // and so on...
  "y" : 25,
  "z" : 26,
  " " : 27
}

function main() {

    var input = prompt("Enter a character!");
    var encrypted = -1; // Fallback number. (If no replacement found in our `map`)

    // check if `map` has this "character" property
    if ( map.hasOwnProperty(input[0]) ) {
        // get the value from our map,
        // where `input[0]` represents the first character
        encrypted = map[input[0]];
    }

    alert( encrypted ); // Will alert the replacement number or log -1 as fallback
}  

main()
TRY USING ONLY ONE OF: "a b c y z (space)"

Here's Something much simpler - but not much secure as-well ;) You could convert a string to base64 using btoa()MDN - and than when you want to read the "secret message" you can simply do btoa()MDN over the encoded string

function main() {

    var input = prompt("Enter a string!").trim();
    var encode = btoa( input ); // String to base64
    var decode = "";
    
    if ( !encode )  return; // Do nothing if nothing valid was entered
    
    alert( encode );
    
    // Whenever you want to reveal your secret message:
    decode = atob( encode );
    alert( "Hey I can also decode! Here you go:\n"+ decode);

}  

main()

I see your interest toward this kind of stuff... so google, learn about cyphers and other encryption algorhitms. Start by exploring the basic ones:

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313