Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This function is pretty simple
- function frequencyanalysis(text){
- text = text.split(" ").join("").toUpperCase();
- var textL = text.length;
- 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);
- var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- // Nested loop to find frequencies and input them into the hashtable
- for (d=0; d<=25; d++) {
- for (i=0; i<=textL; i++){
- if (text.charAt(i) === alphabet.charAt(d)){
- hashtable[d] = hashtable[d] + 1;
- }
- }
- }
- return hashtable;
- }
- // This function makes Index of Coincidence Happen | 1.0 = Random Text/Polyalphabetic Cipher 1.73 = Standard English/Monoalphabetic Cipher
- function indexofcoincidence(text){
- text = text.split(" ").join("").toUpperCase();
- var textL = text.length;
- 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];
- var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- for (d=0; d<=25; d++) {
- for (i=0; i < textL; i++){
- if (text.charAt(i) === alphabet.charAt(d)){
- hashtable[d] = hashtable[d] + 1;
- }
- }
- }
- var aa = hashtable[0]/textL;
- var A = aa*aa;
- var bb = hashtable[1]/textL;
- var B = bb*bb;
- var cc = hashtable[2]/textL;
- var C = cc*cc;
- var dd = hashtable[3]/textL;
- var D = dd*dd;
- var ee = hashtable[4]/textL;
- var E = ee*ee;
- var ff = hashtable[5]/textL;
- var F = ff*ff;
- var gg = hashtable[6]/textL;
- var G = gg*gg;
- var hh = hashtable[7]/textL;
- var H = hh*hh;
- var ii = hashtable[8]/textL;
- var I = ii*ii;
- var jj = hashtable[9]/textL;
- var J = jj*jj;
- var kk = hashtable[10]/textL;
- var K = kk*kk;
- var ll = hashtable[11]/textL;
- var L = ll*ll;
- var mm = hashtable[12]/textL;
- var M = mm*mm;
- var nn = hashtable[13]/textL;
- var N = nn*nn;
- var oo = hashtable[14]/textL;
- var O = oo*oo;
- var pp = hashtable[15]/textL;
- var P = pp*pp;
- var qq = hashtable[16]/textL;
- var Q = qq*qq;
- var rr = hashtable[17]/textL;
- var R = rr*rr;
- var ss = hashtable[18]/textL;
- var S = ss*ss;
- var tt = hashtable[19]/textL;
- var T = tt*tt;
- var uu = hashtable[20]/textL;
- var U = uu*uu;
- var vv = hashtable[21]/textL;
- var V = vv*vv;
- var ww = hashtable[22]/textL;
- var W = ww*ww;
- var xx = hashtable[23]/textL;
- var X = xx*xx;
- var yy = hashtable[24]/textL;
- var Y = yy*yy;
- var zz = hashtable[25]/textL;
- var Z = zz*zz;
- 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;
- var Top = 0.027*textL;
- var Bottom1 = 0.038*textL + 0.065;
- var Bottom2 = (textL - 1)*Kappa;
- var KeyLength = Top/(Bottom2 - Bottom1) ;
- return Kappa/0.0385;
- }
- function keylengthfinder(text){
- // Average Function Definition
- Array.prototype.avg = function() {
- var av = 0;
- var cnt = 0;
- var len = this.length;
- for (var i = 0; i < len; i++) {
- var e = +this[i];
- if(!e && this[i] !== 0 && this[i] !== '0') e--;
- if (this[i] == e) {av += e; cnt++;}
- }
- return av/cnt;
- }
- // Begin the Key Length Finding
- var textL = text.length;
- var hashtable = new Array(0,0,0,0,0,0,0,0,0,0,0,0);
- for (a = 0; a <= 12; a++){ // This is the main loop, testing each key length
- var stringtable = [];
- for (z = 0; z <= a; z++){ // This allows each setting, ie. 1st, 4th, 7th AND 2nd, 5th, 8th to be tested
- for (i = z; i < textL; i + a){
- var string = '';
- string = string.concat(text.charAt(i)); // Join each letter of the correct place in the string
- stringtable[z] = indexofcoincidence(string);
- }
- }
- hashtable[a] = stringtable.avg();
- }
- return hashtable;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement