<!-- JavaScript:Strings code snippet 7. -->
<!-- A form to encrypt a string by manipulating the Unicode values. -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- Code source: http://www.justfigures.co.uk/ -->
<!-- A resource for web developers using XHTML, CSS, JavaScript, PHP -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>View code snippet page</title>
<script language="JavaScript" type="text/javascript">
//<![CDATA[
function nowEncipher(aString)
{
// create an array to hold the cipher text
var subArray = ['s','c','h','n','i','t','z','e','l','m','o','p','q','r','u','v','w','x','y','a','b','d','f','g','j','k'];
var result;
result = '';
for (var position = 0; position < aString.length; position = position + 1)
{
// returns a space in the outputString if the inputString contains a space
if (aString.charCodeAt(position) == 32)
{
result = result + ' '
}
/* ignore all non-alpha characters so that the value 'undefined' is not
returned in the outputString */
else
{
if ((aString.charCodeAt(position) > 96) && (aString.charCodeAt(position) < 123))
/* take 97 from the Unicode value to match the correct index value
for the substitution cipher text held in subArray, example 'a' will be 's */
{
result = result + subArray[aString.charCodeAt(position)-97]
}
}
}
return result
}
/* function 'doEncipher()' prepares the string for encryption by
converting to lowercase all alpha characters */
function doEncipher()
{
var givenString,encodedString;
document.encipher.outputString.value ='';
givenString = document.encipher.inputString.value;
givenString = givenString.toLowerCase();
// note that toLowerCase leaves non-alpha characters unchanged
// call the function 'nowEncipher()'
encodedString = nowEncipher(givenString);
// output the encrypted text to the form
document.encipher.outputString.value = encodedString;
// reset focus
document.encipher.inputString.focus();
}
//]]>
</script>
</head>
<body>
<form name="encipher" id="encipher">
Enter String:<br />
<input type="text" size="100" name="inputString" value='' /><br />
<input type="button" value="Encipher" onclick="doEncipher(document.encipher.inputString.value)" /><br />
Result:<br />
<input type="text" size="100" name="outputString" value='' /><br />
</form>
</body>
</html>