Advertisement
pawn007

khonguy

Jul 27th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <script>
  4.  
  5. function caesar(){
  6. var num = parseInt(document.getElementById("num").value)
  7. var txt = document.getElementById("txt").value;
  8. var x = [];
  9. if(num>=-26 && num<=26){
  10. for(i=0;i<txt.length;i++){
  11. x[i] = txt.charCodeAt(i)+num;
  12. if(txt.charCodeAt(i)>=65 && txt.charCodeAt(i)<=90){
  13.  
  14. if(x[i]>=65&&x[i]<=90){
  15. x[i]=x[i];
  16. }
  17. else if(x[i]>90){
  18. x[i]=x[i]-26;
  19. }
  20. else if(x[i]<65){
  21. x[i]=x[i]+26;
  22. }
  23.  
  24. }
  25. if(txt.charCodeAt(i)==32){
  26. x[i]=txt.charCodeAt(i);
  27. }
  28.  
  29. if(txt.charCodeAt(i)>=97 && txt.charCodeAt(i)<=122){
  30.  
  31. if(x[i]>=97&&x[i]<=122){
  32. x[i]=x[i];
  33. }
  34. else if(x[i]>122){
  35. x[i]=x[i]-26;
  36. }
  37. else if(x[i]<97){
  38. x[i]=x[i]+26;
  39. }
  40.  
  41. }
  42.  
  43.  
  44. }
  45.  
  46.  
  47. }
  48. else{
  49. document.write("Number must be from -26 to 26, try again :)")
  50. }
  51.  
  52. document.write(String.fromCharCode.apply(null, x))
  53.  
  54. }
  55.  
  56.  
  57. function decaesar(){
  58. var num = parseInt(document.getElementById("num").value)*-1
  59. var txt = document.getElementById("txt").value;
  60. var x = [];
  61. if(num>=-26 && num<=26){
  62. for(i=0;i<txt.length;i++){
  63. x[i] = txt.charCodeAt(i)+num;
  64. if(txt.charCodeAt(i)>=65 && txt.charCodeAt(i)<=90){
  65.  
  66. if(x[i]>=65&&x[i]<=90){
  67. x[i]=x[i];
  68. }
  69. else if(x[i]>90){
  70. x[i]=x[i]-26;
  71. }
  72. else if(x[i]<65){
  73. x[i]=x[i]+26;
  74. }
  75.  
  76. }
  77. if(txt.charCodeAt(i)==32){
  78. x[i]=txt.charCodeAt(i);
  79. }
  80.  
  81. if(txt.charCodeAt(i)>=97 && txt.charCodeAt(i)<=122){
  82.  
  83. if(x[i]>=97&&x[i]<=122){
  84. x[i]=x[i];
  85. }
  86. else if(x[i]>122){
  87. x[i]=x[i]-26;
  88. }
  89. else if(x[i]<97){
  90. x[i]=x[i]+26;
  91. }
  92.  
  93. }
  94.  
  95.  
  96. }
  97.  
  98.  
  99. }
  100. else{
  101. document.write("Number must be from -26 to 26, try again :)")
  102. }
  103.  
  104. document.write(String.fromCharCode.apply(null, x))
  105.  
  106. }
  107. </script>
  108. <head>
  109. <title>Caesar Cipher</title>
  110. </head>
  111. <body>
  112. The shift (number from -26 to 26): <Br>
  113. <input type="tel" id="num"><br><br>
  114. Text to encode / decode: <br>
  115. <input type="text" id="txt"><br><br>
  116.  
  117. <button onclick="caesar()">Encode</button>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
  118. <button onclick="decaesar()">Decode</button>
  119.  
  120.  
  121. </body>
  122. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement