943,650 Members | Top Members by Rank

Ad:
Jun 23rd, 2009
0

Need help with substitution cipher in Javascript

Expand Post »
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.
Similar Threads
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
CodeBoy101 is offline Offline
71 posts
since Dec 2007
Jun 23rd, 2009
0

Re: Need help with substitution cipher in Javascript

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.
Reputation Points: 14
Solved Threads: 22
Junior Poster
JugglerDrummer is offline Offline
138 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Trouble getting odd/even script to work
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: To make textarea and textbox identical





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC