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
Reply

Join Date: Dec 2007
Posts: 65
Reputation: CodeBoy101 is an unknown quantity at this point 
Solved Threads: 0
CodeBoy101's Avatar
CodeBoy101 CodeBoy101 is offline Offline
Junior Poster in Training

Need help with substitution cipher in Javascript

 
0
  #1
Jun 23rd, 2009
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:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1.  
  2. // This script takes user input and returns it encrypted and in plain text
  3.  
  4.  
  5. // Validate the input
  6. function validateInput (form) {
  7. if (document.CipherForm.strString.value == "") {
  8. alert("Please enter something to encrypt!");
  9. document.CipherForm.strString.focus();
  10. return false;
  11. }
  12. }
  13.  
  14. function flipLetter (chrLetter) {
  15. // Executes a reflection cipher on the character passed from encryptString()
  16. // substituting one letter for its alphabetic inverse
  17. switch(chrLetter) {
  18. case "a": chrLetter = "z"; break;
  19. case "b": chrLetter = "y"; break;
  20. case "c": chrLetter = "x"; break;
  21. case "d": chrLetter = "w"; break;
  22. case "e": chrLetter = "v"; break;
  23. case "f": chrLetter = "u"; break;
  24. case "g": chrLetter = "t"; break;
  25. case "h": chrLetter = "s"; break;
  26. case "i": chrLetter = "r"; break;
  27. case "j": chrLetter = "q"; break;
  28. case "k": chrLetter = "p"; break;
  29. case "l": chrLetter = "o"; break;
  30. case "m": chrLetter = "n"; break;
  31. case "n": chrLetter = "m"; break;
  32. case "o": chrLetter = "l"; break;
  33. case "p": chrLetter = "k"; break;
  34. case "q": chrLetter = "j"; break;
  35. case "r": chrLetter = "i"; break;
  36. case "s": chrLetter = "h"; break;
  37. case "t": chrLetter = "g"; break;
  38. case "u": chrLetter = "f"; break;
  39. case "v": chrLetter = "e"; break;
  40. case "w": chrLetter = "d"; break;
  41. case "x": chrLetter = "c"; break;
  42. case "y": chrLetter = "b"; break;
  43. case "z": chrLetter = "a"; break;
  44. }
  45. return chrLetter;
  46. }
  47.  
  48. function encryptString (strPlain) {
  49. // Convert the string passed to this function from the form to lower case
  50. strPlain = strPlain.toLowerCase();
  51. document.CipherForm.strPlain.value = strPlain;
  52.  
  53. // Run the reflection cipher on each letter in the string
  54. var strCipher = "";
  55. for (var i = 0; i < strPlain.length; i++) {
  56. strCipher = strCipher + flipLetter(strPlain.charAt(i));
  57. }
  58.  
  59. // Set the input value to the encrypted string
  60. document.CipherForm.strCipher.value = strCipher;
  61. document.CipherForm.strCipher.focus();
  62. }
  63.  
  64.  
  65. //FOR DECRYPTION
  66.  
  67. // Validate the input
  68. function validateInput2 (form) {
  69. if (document.CipherForm2.strString2.value == "") {
  70. alert("Please enter something to decrypt!");
  71. document.CipherForm2.strString2.focus();
  72. return false;
  73. }
  74. }
  75.  
  76. function deflipLetter (chrLetter) {
  77. // Executes a reflection cipher on the character passed from encryptString()
  78. // substituting one letter for its alphabetic inverse
  79. switch(chrLetter) {
  80. case "a": chrLetter = "z"; break;
  81. case "b": chrLetter = "y"; break;
  82. case "c": chrLetter = "x"; break;
  83. case "d": chrLetter = "w"; break;
  84. case "e": chrLetter = "v"; break;
  85. case "f": chrLetter = "u"; break;
  86. case "g": chrLetter = "t"; break;
  87. case "h": chrLetter = "s"; break;
  88. case "i": chrLetter = "r"; break;
  89. case "j": chrLetter = "q"; break;
  90. case "k": chrLetter = "p"; break;
  91. case "l": chrLetter = "o"; break;
  92. case "m": chrLetter = "n"; break;
  93. case "n": chrLetter = "m"; break;
  94. case "o": chrLetter = "l"; break;
  95. case "p": chrLetter = "k"; break;
  96. case "q": chrLetter = "j"; break;
  97. case "r": chrLetter = "i"; break;
  98. case "s": chrLetter = "h"; break;
  99. case "t": chrLetter = "g"; break;
  100. case "u": chrLetter = "f"; break;
  101. case "v": chrLetter = "e"; break;
  102. case "w": chrLetter = "d"; break;
  103. case "x": chrLetter = "c"; break;
  104. case "y": chrLetter = "b"; break;
  105. case "z": chrLetter = "a"; break;
  106. }
  107. return chrLetter;
  108. }
  109.  
  110. function decryptString (strPlain) {
  111. // Convert the string passed to this function from the form to lower case
  112. strPlain2 = strPlain2.toLowerCase();
  113. document.CipherForm2.strPlain2.value = strPlain2;
  114.  
  115. // Run the reflection cipher on each letter in the string
  116. var strCipher2 = "";
  117. for (var i = 0; i < strPlain2.length; i++) {
  118. strCipher2 = strCipher2 + deflipLetter(strPlain2.charAt(i));
  119. }
  120.  
  121. // Set the input value to the encrypted string
  122. document.CipherForm2.strCipher2.value = strCipher2;
  123. document.CipherForm2.strCipher2.focus();
  124. }

All helpful suggestions are welcome, thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 119
Reputation: JugglerDrummer is an unknown quantity at this point 
Solved Threads: 15
JugglerDrummer's Avatar
JugglerDrummer JugglerDrummer is offline Offline
Junior Poster

Re: Need help with substitution cipher in Javascript

 
0
  #2
Jun 23rd, 2009
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.
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC