Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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;
    }

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    966
    Thanks
    19
    Thanked 211 Times in 209 Posts
    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>

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 498 Times in 492 Posts

    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.

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 498 Times in 492 Posts

    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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •