Advertisement
hiker43

Caesar Chiper

Dec 11th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Caesar Cipher</title>
  5. <meta charset = "UTF-8">
  6. </head>
  7. <body>
  8. <script>
  9. CHARTABLE = [   'a', 'b', 'c', 'č', 'd',
  10.                 'e', 'f', 'g', 'h', 'i',
  11.                 'j', 'k', 'l', 'm', 'n',
  12.                 'o', 'p', 'r', 's', 'š',
  13.                 't', 'u', 'v', 'z', 'ž','_', 0];
  14.  
  15. PLAIN_MESSAGE = ['p','r','e','m','i','k','_',
  16.                 'v','s','e','h','_',
  17.                 'e','n','o','t','_',
  18.                 'i','z','_',
  19.                 'b','r','e','t','a','n','i','j','e','_',
  20.                 'v','_',
  21.                 'g','a','l','i','j','o','_',
  22.                 't','a','k','o','j','_',
  23.                 'c','e','a','s','a','r',0];
  24.                
  25. IMPORTANT_MESSAGE = [ 'v','n','p','b','m','e', 'g','n','u','l','m','e', 'f','b','p','u','k','z', 'b','h','e','g','n','u', 'l','k','z','b','č','j', 'e','h', 0];
  26.  
  27. key = ['a', 'm', 'f', 'o', 'r', 'a', 0];
  28.  
  29. keyLen = 6;
  30.  
  31. function getCharIndex(c)
  32. {
  33.     var i;
  34.     for(i = 0; CHARTABLE[i] != 0; i++)
  35.     {
  36.         if(CHARTABLE[i] == c) return i;
  37.     }
  38.     return -1;
  39. }
  40.  
  41. function encryptCharCaesar(c, n)
  42. {
  43.     return CHARTABLE[(getCharIndex(c)+n)%26];
  44. }
  45.  
  46. function decryptCharCaesar(c, n)
  47. {
  48.     return CHARTABLE[(26*n + getCharIndex(c)-n)%26]
  49. }
  50.  
  51. function encryptVigenere(msg)
  52. {
  53.     enc = '';
  54.     for(var i = 0, j = 0; msg[i] != 0; i++, j++)
  55.     {
  56.         enc += CHARTABLE[(getCharIndex(msg[i]) + getCharIndex(key[j % keyLen]))%26];
  57.     }
  58.     return enc;
  59. }
  60.  
  61. DECRYPTED_MESSAGE = '';
  62. for(i = 0; IMPORTANT_MESSAGE[i] != 0; i++)
  63. {
  64.     DECRYPTED_MESSAGE += decryptCharCaesar(IMPORTANT_MESSAGE[i], 22);
  65. }
  66. console.log(DECRYPTED_MESSAGE);
  67. MSG = ['p', 'r', 'i', 'm', 'i', 't', 'e', 'o', 'b', 'e', 'l', 'i', 'k', 's', 'a', 0];
  68. console.log(encryptVigenere(MSG));
  69.  
  70. </script>
  71. </body>
  72. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement