| | |
Need help with substitution cipher in Javascript
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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:
All helpful suggestions are welcome, thanks in advance.
Right now this is what I have:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
// 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.
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.
92% of all statistics are made up on the spot.
If you found a post helpful, please click the "give XXX reputation" link, to show your appreciation to those who helped you. Thanks! :D
If you found a post helpful, please click the "give XXX reputation" link, to show your appreciation to those who helped you. Thanks! :D
![]() |
Similar Threads
- HTML, CSS, JavaScript Developer (Web Development Job Offers)
- Chars becoming negative. (C++)
- Python challenge help (Python)
- URL Encoding and/or string substitution (JSP)
- JavaScript / AJAX Guru (Software Development Job Offers)
- Are you a PHP and Javascript junkie? It's time you met Emma. (Software Development Job Offers)
- Help With Caesar Cipher/Shift (Pascal and Delphi)
- Cryptography Fun (Python)
- Javascript Useful? (IT Professionals' Lounge)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Trouble getting odd/even script to work
- Next Thread: To make textarea and textbox identical
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
ajax ajaxcode ajaxhelp ajaxjspservlets animate api automatically blackjack browser bug calendar captchaformproblem checkbox child class close cookies createrange() cursor dependent disablefirebug dom dropdown editor element engine events explorer ext file firehose flash form forms game google gxt hiddenvalue highlightedword html htmlform ie8 iframe image() images internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jump libcurl math matrixcaptcha media microsoft mp3 mysql object onerror onmouseoutdivproblem onreadystatechange parent passing paypal pdf php player post progressbar rated regex runtime scroll search security session shopping size software sql star stars stretch synchronous text textarea twitter unicode validation web website window windowsxp wysiwyg xspf \n





