Hello all. I''m currently working on a a page which should be able to run a substitution cipher in order to do both encryption and decrption in Java script on the same page. But the problem is only the encryption part works.

Right now this is what I have:

// This script takes user input and returns it encrypted and in plain text


// Validate the input
function validateInput (form) { 
	if (document.CipherForm.strString.value == "") { 
	   alert("Please enter something to encrypt!"); 
	   document.CipherForm.strString.focus(); 
	   return false; 
	}
}

function flipLetter (chrLetter) { 
	//  Executes a reflection cipher on the character passed from encryptString()   
	//  substituting one letter for its alphabetic inverse
	switch(chrLetter) {
		case "a": chrLetter = "z"; break;
		case "b": chrLetter = "y"; break;
		case "c": chrLetter = "x"; break;
		case "d": chrLetter = "w"; break;
		case "e": chrLetter = "v"; break;
		case "f": chrLetter = "u"; break;
		case "g": chrLetter = "t"; break;
		case "h": chrLetter = "s"; break;
		case "i": chrLetter = "r"; break;
		case "j": chrLetter = "q"; break;
		case "k": chrLetter = "p"; break;
		case "l": chrLetter = "o"; break;
		case "m": chrLetter = "n"; break;
		case "n": chrLetter = "m"; break;
		case "o": chrLetter = "l"; break;
		case "p": chrLetter = "k"; break;
		case "q": chrLetter = "j"; break;
		case "r": chrLetter = "i"; break;
		case "s": chrLetter = "h"; break;
		case "t": chrLetter = "g"; break;
		case "u": chrLetter = "f"; break;
		case "v": chrLetter = "e"; break;
		case "w": chrLetter = "d"; break;
		case "x": chrLetter = "c"; break;
		case "y": chrLetter = "b"; break;
		case "z": chrLetter = "a"; break;
	}
	return chrLetter;
}

function encryptString (strPlain) {
	// Convert the string passed to this function from the form to lower case
	strPlain = strPlain.toLowerCase();
	document.CipherForm.strPlain.value = strPlain;
	
	// Run the reflection cipher on each letter in the string
	var strCipher = "";
	for (var i = 0; i < strPlain.length; i++) { 
		strCipher = strCipher + flipLetter(strPlain.charAt(i));
    }
   
	// Set the input value to the encrypted string
	document.CipherForm.strCipher.value = strCipher;
	document.CipherForm.strCipher.focus();
}


//FOR DECRYPTION

// Validate the input
function validateInput2 (form) { 
	if (document.CipherForm2.strString2.value == "") { 
	   alert("Please enter something to decrypt!"); 
	   document.CipherForm2.strString2.focus(); 
	   return false; 
	}
}

function deflipLetter (chrLetter) { 
	//  Executes a reflection cipher on the character passed from encryptString()   
	//  substituting one letter for its alphabetic inverse
	switch(chrLetter) {
		case "a": chrLetter = "z"; break;
		case "b": chrLetter = "y"; break;
		case "c": chrLetter = "x"; break;
		case "d": chrLetter = "w"; break;
		case "e": chrLetter = "v"; break;
		case "f": chrLetter = "u"; break;
		case "g": chrLetter = "t"; break;
		case "h": chrLetter = "s"; break;
		case "i": chrLetter = "r"; break;
		case "j": chrLetter = "q"; break;
		case "k": chrLetter = "p"; break;
		case "l": chrLetter = "o"; break;
		case "m": chrLetter = "n"; break;
		case "n": chrLetter = "m"; break;
		case "o": chrLetter = "l"; break;
		case "p": chrLetter = "k"; break;
		case "q": chrLetter = "j"; break;
		case "r": chrLetter = "i"; break;
		case "s": chrLetter = "h"; break;
		case "t": chrLetter = "g"; break;
		case "u": chrLetter = "f"; break;
		case "v": chrLetter = "e"; break;
		case "w": chrLetter = "d"; break;
		case "x": chrLetter = "c"; break;
		case "y": chrLetter = "b"; break;
		case "z": chrLetter = "a"; break;
	}
	return chrLetter;
}

function decryptString (strPlain) {
	// Convert the string passed to this function from the form to lower case
	strPlain2 = strPlain2.toLowerCase();
	document.CipherForm2.strPlain2.value = strPlain2;
	
	// Run the reflection cipher on each letter in the string
	var strCipher2 = "";
	for (var i = 0; i < strPlain2.length; i++) { 
		strCipher2 = strCipher2 + deflipLetter(strPlain2.charAt(i));
    }
   
	// Set the input value to the encrypted string
	document.CipherForm2.strCipher2.value = strCipher2;
	document.CipherForm2.strCipher2.focus();
}

All helpful suggestions are welcome, thanks in advance.

in encryption you switch a to z, so in decryption z must switch to a.

Oh and to make it easier and save you some typing, put all the letters a to z in an array, and search the array for the character, and replace it with the character at the found index+1. :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.