Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-01-2012, 04:12 PM   PM User | #1
czarcalvinsk
New to the CF scene

 
Join Date: Jan 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
czarcalvinsk is an unknown quantity at this point
Having problems using fromCharCode() and charCodeAt() for use in an encrypter

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
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:
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;
}
czarcalvinsk is offline   Reply With Quote
Old 01-01-2012, 05:18 PM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 814
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Code:
<!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>
DaveyErwin is offline   Reply With Quote
Old 01-02-2012, 03:54 AM   PM User | #3
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
Lightbulb

I assume you want to decrypt as well...
Code:
<!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 is offline   Reply With Quote
Old 01-02-2012, 03:59 AM   PM User | #4
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
Lightbulb

Combined encryption/decryption ...
Code:
<!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>
jmrker is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:33 PM.


Advertisement
Log in to turn off these ads.