...

View Full Version : Having problems using fromCharCode() and charCodeAt() for use in an encrypter



czarcalvinsk
01-01-2012, 05:12 PM
I am creating a encrypting program, which changes the ASCII value of the characters by 78 then outputting them as an alert. However, the alert isn't popping up.
I believe the problem lies in the second for loop where the fromCharCode() and getCharAt() are used.

Here is the HTML code


<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script src="encrypt.js"></script>
<script src="decrypt.js"></script>
<title>Encrypter/Decrypter</title>
</head>
<body>
<div id="big_wrapper">
<p id="encrypt">Encrypt</p>
<input type="button" value="Click to Encrypt" name="encrypt" onClick="getTxt()">
<p id="decrypt">Decrypt</p>
</div>
</body>
</html>


Here is the Javascript code:


function getTxt(){
var x=prompt("Enter the text you want to encrypt");
alert(encrypt(x));
}
function setCharAt(str,index,chr) {
if(index > str.length-1) return str;
return str.substr(0,index) + chr + str.substr(index+1);
}

function encrypt(text){

var i =0; //preparing all of the variables
var cryptarray = new Array();
for (i=0;i<1000;i++){
cryptarray[i]=null;
}

for (i=0;i<text.length;i++){
cryptarray[i]=text.fromCharCode((text.charCodeAt(i)+78)) //loops through the string, taking characters and adding 78 to their ascii value and putting them in the temp array
}

var temptext = text;
for (i=0;i<text.length;i++){
temptext.setCharAt(i)=cryptarray[i]; //converts the array into a string
}
return temptext;
}

DaveyErwin
01-01-2012, 06:18 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="daveyerwin">
<title>Untitled</title>
<script type="text/javascript">

function getTxt(){
var x=prompt("Enter the text you want to encrypt");
alert(encrypt(x));
}
function setCharAt(str,index,chr) {
if(index > str.length-1) return str;
return str.substr(0,index) + chr + str.substr(index+1);
}
function encrypt(text){
var i =0; //preparing all of the variables
var cryptarray = new Array();
for (i=0;i<1000;i++){
cryptarray[i]=null;
}
for (i=0;i<text.length;i++){
cryptarray[i]=String.fromCharCode((text.charCodeAt(i)+78)) //loops through the string, taking characters and adding 78 to their ascii value and putting them in the temp array
}
var temptext = text;
temptext=cryptarray.join(""); //converts the array into a string
return temptext;
}
</script>
</head>
<body>
<div id="big_wrapper">
<p id="encrypt">Encrypt</p>
<input type="button" value="Click to Encrypt" name="encrypt" onClick="getTxt()">
<p id="decrypt">Decrypt</p>
</div>
</body>
</html>

jmrker
01-02-2012, 04:54 AM
I assume you want to decrypt as well...


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="daveyerwin">
<title>Untitled</title>
<script type="text/javascript">
// From: http://www.codingforums.com/showthread.php?p=1176065#post1176065

function getTxt(){
var x=document.getElementById('inTxt').value;
document.getElementById('outTxt').value = encrypt(x);
}
function putTxt(){
var x=document.getElementById('outTxt').value;
document.getElementById('inTxt').value = decrypt(x);
}
function setCharAt(str,index,chr) {
if(index > str.length-1) return str;
return str.substr(0,index) + chr + str.substr(index+1);
}
function encrypt(text){
var i =0; //preparing all of the variables
var cryptarray = [];
for (i=0;i<text.length;i++){
cryptarray.push(String.fromCharCode((text.charCodeAt(i)+78)));
// loops through the string, taking characters and adding 78 to their ascii value
// and putting them in the temp array
}
var temptext = text;
temptext=cryptarray.join(""); //converts the array into a string
return temptext;
}

function decrypt(text){
var i =0; //preparing all of the variables
var cryptarray = [];
for (i=0;i<text.length;i++){
cryptarray.push(String.fromCharCode((text.charCodeAt(i)-78)));
// loops through the string, taking characters and subtracting 78 to their ascii value
// and putting them in the temp array
}
var temptext = text;
temptext=cryptarray.join(""); //converts the array into a string
return temptext;
}

</script>
</head>
<body>
<div id="big_wrapper">
<p id="encrypt">Encrypt</p>
<input type="text" id="inTxt" value="Now is the time 4 all">
<input type="button" value="Click to Encrypt" name="encrypt" onClick="getTxt()">
<button onclick="document.getElementById('inTxt').value = ''">Clear</button>

<p id="decrypt">Decrypt</p>
<input type="text" id="outTxt" value="">
<input type="button" value="Click to Decrypt" name="encrypt" onClick="putTxt()">
<button onclick="document.getElementById('outTxt').value = ''">Clear</button>
</div>
</body>
</html>

Also, there is no 1000 character limit with this method.

You could also combine the encrypt/decrypt functions into one single function.

jmrker
01-02-2012, 04:59 AM
Combined encryption/decryption ...


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="daveyerwin">
<title>Untitled</title>
<script type="text/javascript">
// From: http://www.codingforums.com/showthread.php?p=1176065#post1176065

function getTxt(){
var x=document.getElementById('inTxt').value;
document.getElementById('outTxt').value = crypt(x,true);
}
function putTxt(){
var x=document.getElementById('outTxt').value;
document.getElementById('inTxt').value = crypt(x,false);
}
function setCharAt(str,index,chr) {
if(index > str.length-1) return str;
return str.substr(0,index) + chr + str.substr(index+1);
}
function crypt(text,flag){
var i =0; //preparing all of the variables
var cryptarray = [];
for (i=0;i<text.length;i++){
if (flag) {
cryptarray.push(String.fromCharCode((text.charCodeAt(i)+78)));
// loops through the string, taking characters and adding 78 to their ascii value
// and putting them in the temp array
} else {
cryptarray.push(String.fromCharCode((text.charCodeAt(i)-78)));
// loops through the string, taking characters and subtracting 78 to their ascii value
// and putting them in the temp array
}
}
var temptext = text;
temptext=cryptarray.join(""); //converts the array into a string
return temptext;
}

</script>
</head>
<body>
<div id="big_wrapper">
<p id="encrypt">Encrypt</p>
<input type="text" id="inTxt" value="Now is the time 4 all">
<input type="button" value="Click to Encrypt" name="encrypt" onClick="getTxt()">
<button onclick="document.getElementById('inTxt').value = ''">Clear</button>

<p id="decrypt">Decrypt</p>
<input type="text" id="outTxt" value="">
<input type="button" value="Click to Decrypt" name="encrypt" onClick="putTxt()">
<button onclick="document.getElementById('outTxt').value = ''">Clear</button>
</div>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum