Advertisement
Guest User

Cryptography in JS Help?

a guest
Jul 17th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // This function is pretty simple
  3. function frequencyanalysis(text){
  4. text = text.split(" ").join("").toUpperCase();
  5. var textL = text.length;
  6. var hashtable = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
  7. var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  8. // Nested loop to find frequencies and input them into the hashtable
  9.  
  10. for (d=0; d<=25; d++) {
  11.     for (i=0; i<=textL; i++){
  12.          if (text.charAt(i) === alphabet.charAt(d)){
  13.          hashtable[d] = hashtable[d] + 1;
  14.         }
  15.     }
  16. }
  17. return hashtable;
  18. }
  19.  
  20.  
  21. // This function makes Index of Coincidence Happen | 1.0 = Random Text/Polyalphabetic Cipher 1.73 = Standard English/Monoalphabetic Cipher
  22. function indexofcoincidence(text){
  23. text = text.split(" ").join("").toUpperCase();
  24. var textL = text.length;
  25. var hashtable = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
  26. var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  27. for (d=0; d<=25; d++) {
  28.     for (i=0; i < textL; i++){
  29.         if (text.charAt(i) === alphabet.charAt(d)){
  30.         hashtable[d] = hashtable[d] + 1;
  31.         }
  32.     }
  33. }
  34.  
  35. var aa = hashtable[0]/textL;
  36. var A = aa*aa;
  37. var bb = hashtable[1]/textL;
  38. var B = bb*bb;
  39. var cc = hashtable[2]/textL;
  40. var C = cc*cc;
  41. var dd = hashtable[3]/textL;
  42. var D = dd*dd;
  43. var ee = hashtable[4]/textL;
  44. var E = ee*ee;
  45. var ff = hashtable[5]/textL;
  46. var F = ff*ff;
  47. var gg = hashtable[6]/textL;
  48. var G = gg*gg;
  49. var hh = hashtable[7]/textL;
  50. var H = hh*hh;
  51. var ii = hashtable[8]/textL;
  52. var I = ii*ii;
  53. var jj = hashtable[9]/textL;
  54. var J = jj*jj;
  55. var kk = hashtable[10]/textL;
  56. var K = kk*kk;
  57. var ll = hashtable[11]/textL;
  58. var L = ll*ll;
  59. var mm = hashtable[12]/textL;
  60. var M = mm*mm;
  61. var nn = hashtable[13]/textL;
  62.  
  63. var N = nn*nn;
  64. var oo = hashtable[14]/textL;
  65. var O = oo*oo;
  66. var pp = hashtable[15]/textL;
  67. var P = pp*pp;
  68. var qq = hashtable[16]/textL;
  69. var Q = qq*qq;
  70. var rr = hashtable[17]/textL;
  71. var R = rr*rr;
  72. var ss = hashtable[18]/textL;
  73. var S = ss*ss;
  74. var tt = hashtable[19]/textL;
  75. var T = tt*tt;
  76. var uu = hashtable[20]/textL;
  77. var U = uu*uu;
  78. var vv = hashtable[21]/textL;
  79. var V = vv*vv;
  80. var ww = hashtable[22]/textL;
  81. var W = ww*ww;
  82. var xx = hashtable[23]/textL;
  83. var X = xx*xx;
  84. var yy = hashtable[24]/textL;
  85. var Y = yy*yy;
  86. var zz = hashtable[25]/textL;
  87. var Z = zz*zz;
  88.  
  89. var Kappa = A+B+C+D+E+F+G+H+I+J+K+L+M+N+O+P+Q+R+S+T+U+V+W+X+Y+Z;
  90.  
  91. var Top = 0.027*textL;
  92. var Bottom1 = 0.038*textL + 0.065;
  93. var Bottom2 = (textL - 1)*Kappa;
  94. var KeyLength = Top/(Bottom2 - Bottom1) ;
  95.  
  96. return Kappa/0.0385;
  97. }
  98.  
  99. function keylengthfinder(text){
  100.     // Average Function Definition
  101.     Array.prototype.avg = function() {
  102. var av = 0;
  103. var cnt = 0;
  104. var len = this.length;
  105. for (var i = 0; i < len; i++) {
  106. var e = +this[i];
  107. if(!e && this[i] !== 0 && this[i] !== '0') e--;
  108. if (this[i] == e) {av += e; cnt++;}
  109. }
  110. return av/cnt;
  111. }
  112.     // Begin the Key Length Finding
  113.     var textL = text.length;
  114.     var hashtable = new Array(0,0,0,0,0,0,0,0,0,0,0,0);
  115.         for (a = 0; a <= 12; a++){ // This is the main loop, testing each key length
  116.             var stringtable = [];
  117.             for (z = 0; z <= a; z++){ // This allows each setting, ie. 1st, 4th, 7th AND 2nd, 5th, 8th to be tested
  118.                 for (i = z; i < textL; i + a){
  119.                     var string = '';
  120.                     string = string.concat(text.charAt(i)); // Join each letter of the correct place in the string
  121.                     stringtable[z] = indexofcoincidence(string);
  122.                     }
  123.                 }
  124.             hashtable[a] = stringtable.avg();
  125.         }
  126.     return hashtable;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement