Skip to content
Snippets Groups Projects
Commit 9e90b312 authored by mnj98's avatar mnj98
Browse files

Add new file

parents
Branches main
No related tags found
No related merge requests found
keyFinder 0 → 100644
//Rhino 1.7.7.1
//https://stackoverflow.com/questions/18619785/counting-frequency-of-characters-in-a-string-using-javascript
function getFrequency(string) {
var freq = {};
for (var i = 0; i < string.length; i++) {
var character = string.charCodeAt(i) - 65;
if (freq[character]) {
freq[character]++;
} else {
freq[character] = 1;
}
}
return freq;
}
function letterFromNum(num){
let x = (+num + 65);
return String.fromCharCode((x))
}
function findKey(freq, prob) {
let c1 = {};
for (var i = 0; i < 26; i++) {
let m = 0;
for (var j = 0; j < 26; j++) {
if (freq[(j + i) % 26]) {
m += (prob[j] * freq[(j + i) % 26]) / s1.length;
}
}
c1[i] = m;
}
return closest(c1, 0.066);
}
//https://stackoverflow.com/questions/8584902/get-closest-number-out-of-array
function closest(array, num) {
var i = 0;
var minDiff = 1000;
var ans;
for (i in array) {
var m = Math.abs(num - array[i]);
if (m < minDiff) {
minDiff = m;
ans = i;
}
}
return ans;
}
/*
function closest(counts, goal){
var closest = counts.reduce(function(prev, curr) {
return (Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);
});
console.log(closest);
}*/
let probabilies = [
0.082,
0.015,
0.028,
0.043,
0.127,
0.022,
0.02,
0.061,
0.07,
0.002,
0.008,
0.04,
0.024,
0.067,
0.075,
0.019,
0.001,
0.06,
0.063,
0.091,
0.028,
0.01,
0.023,
0.001,
0.02,
0.001
];
let cypher =
"KCCPKBGUFDPHQTYAVINRRTMVGRKDNBVFDETDGILTXRGUDDKOTFMBPVGEGLTGCKQRACQCWDNAWCRXIZAKFTLEWRPTYCQKYVXCHKFTPONCQQRHJVAJUWETMCMSPKQDYHJVDAHCTRLSVSKCGCZQQDZXGSFRLSWCWSJTBHAFSIASPRJAHKJRJUMVGKMITZHFPDISPZLVLGWTFPLKKEBDPGCEBSHCTJRWXBAFSPEZQNRWXCVYCGAONWDDKACKAWBBIKFTIOVKCGGHJVLNHIFFSQESVYCLACNVRWBBIREPBBVFEXOSCDYGZWPFDTKFQIYCWHJVLNHIQIBTKHJVNPIST";
console.log(cypher);
let keylen = 6;
let s1 = "";
let s2 = "";
let s3 = "";
let s4 = "";
let s5 = "";
let s6 = "";
for (var i = 0; i < cypher.length; i = i + 6) {
s1 = s1 + cypher.charAt(i);
}
for (var i = 1; i < cypher.length; i = i + 6) {
s2 = s2 + cypher.charAt(i);
}
for (var i = 2; i < cypher.length; i = i + 6) {
s3 = s3 + cypher.charAt(i);
}
for (var i = 3; i < cypher.length; i = i + 6) {
s4 = s4 + cypher.charAt(i);
}
for (var i = 4; i < cypher.length; i = i + 6) {
s5 = s5 + cypher.charAt(i);
}
for (var i = 5; i < cypher.length; i = i + 6) {
s6 = s6 + cypher.charAt(i);
}
let f1 = getFrequency(s1);
let f2 = getFrequency(s2);
let f3 = getFrequency(s3);
let f4 = getFrequency(s4);
let f5 = getFrequency(s5);
let f6 = getFrequency(s6);
console.log(letterFromNum(findKey(f1, probabilies)))
console.log(letterFromNum(findKey(f2, probabilies)))
console.log(letterFromNum(findKey(f3, probabilies)))
console.log(letterFromNum(findKey(f4, probabilies)))
console.log(letterFromNum(findKey(f5, probabilies)))
console.log(letterFromNum(findKey(f6, probabilies)))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment