1

I want to make new custom encryption engine with javascript, but i have a problem when i make a decryption function. In my decryption function I don't understand how to switch 3 characters to 1 character. In the decryption function section, 3 characters from the case I do not want are changing to the characters that are returned.

If you need my full code, i can share in here.

So please help me to solve this problem. Sorry for my bad English :)

<body>
	<h3>Encrypt and Decrypt</h3>
	<!-- Encrypt -->
<!-- 	<input placeholder="Ketikan sesuatu disini, pasti bisa:v" id="input"><br>
	<button onclick="encrypt()">Encrypt</button> -->
	<!-- Decrypt -->
	<br><input placeholder="Ketikan sesuatu disini, pasti bisa:v" id="input2"><br>
	<button onclick="decrypt()">Decrypt</button>
	<!-- Result -->
	<div id="result"></div>
	<!-- Enginenya -->
	<script>
		function encrypt(){
			var rawtext = document.getElementById("input").value;
			var temptext = "";
			for(i = 0; i < rawtext.length; i++){
				temptext += enc(rawtext[i]);
			}
			document.getElementById("result").innerHTML = temptext;
		}
		function decrypt(){
			var rawtext = document.getElementById("input2").value;
			var temptext = "";
			for(i = 0; i < rawtext.length; i++){
				temptext += dec(rawtext[i]);
			}
			document.getElementById("result").innerHTML = temptext;
		}
		function enc(x){
			switch(x){
				case " " :
				return " ";
				break;

				case "A" :
				return "+/=";
				break;

				case "B" :
				return "36=";
				break;
			}
		}
		function dec(x){
			switch(x){
				case "+/=" :
				return "A";
				break;

				case "36=" :
				return "B";
				break;
			}
		}
	</script>
</body>

1
  • Obligatory warning that rolling your own crypto is a terrible idea. Commented Feb 27, 2019 at 22:55

3 Answers 3

1

You are looping through singular chars & passing them to dec(), eg. If you enter "+/=", you are actually calling dec('+') then dec('/') then dec('=')

When decrypting the entered value, you'll have to split them into groups of 3 & then pass those.

function decrypt(){
    var rawtext = document.getElementById("input2").value;
    var temptext = "";

    for(i = 0, charsLength = rawtext.length; i < charsLength; i += 3){
        temptext += dec(rawtext.substring(i, i + 3));
    }

    document.getElementById("result").innerHTML = temptext;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You could take three caracters for decrypting an encrypted string.

while (i < rawtext.length) {
    temptext += dec(rawtext.slice(i, i += 3)); // take from index i and increment i by 3
}

function encrypt() {
    var rawtext = document.getElementById("input").value,
        temptext = "",
        i;
    for (i = 0; i < rawtext.length; i++) {
        temptext += enc(rawtext[i]);
    }
    document.getElementById("result").innerHTML = temptext;
}

function decrypt() {
    var rawtext = document.getElementById("input2").value,
        temptext = "",
        i = 0;
    while (i < rawtext.length) {
        temptext += dec(rawtext.slice(i, i += 3));
    }
    document.getElementById("result").innerHTML = temptext;
}

function enc(x) {
    switch (x) {
        case " ":
            return " ";
        case "A":
            return "+/=";
        case "B":
            return "36=";
    }
}

function dec(x) {
    switch (x) {
        case "+/=":
            return "A";
        case "36=":
            return "B";
    }
}
<h3>Encrypt and Decrypt</h3>
<input placeholder="Ketikan sesuatu disini, pasti bisa:v" id="input"><br>
<button onclick="encrypt()">Encrypt</button>
<!-- Decrypt -->
<br><input placeholder="Ketikan sesuatu disini, pasti bisa:v" id="input2"><br>
<button onclick="decrypt()">Decrypt</button>
<!-- Result -->
<div id="result"></div>

Comments

0

Looks like you're iterating over the text to decrypt character by character, but then your dec function expects three characters. This never happens, so dec() returns undefined.

Example:

decrypt("36=") -> 
    dec("3") + dec("6") + dec("=") ->
    undefined + undefined + undefined
    undefinedundefinedundefined

You should change your decrypt function to avoid this. Additionally, some pointers:

  • You don't initialise i in your encrypt/decrypt function
  • There's no need for a break; after a return in your case statements as return will end the execution.

Edit: Here's an example with map since some other answers had some with for loops. And also because I suspected it could be done with one line (and I was right!)

<body>
	<h3>Encrypt and Decrypt</h3>
	<br><input placeholder="Ketikan sesuatu disini, pasti bisa:v" id="input2"><br>
	<button onclick="decrypt()">Decrypt</button>
	<div id="result"></div>
	<script>
		function decrypt(){
			document.getElementById("result").innerHTML = document.getElementById("input2").value.match(/.{1,3}/g).map(dec).join('');
		}
		function dec(x){
			switch(x){
				case "+/=" :
				  return "A";
				case "36=" :
				  return "B";
                default:
                  return "?";
			}
		}
	</script>
</body>

1 Comment

Thanks a lot dude, for helping :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.