Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Cryptography HW2 Program
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mnj98
Cryptography HW2 Program
Commits
9e90b312
Commit
9e90b312
authored
4 years ago
by
mnj98
Browse files
Options
Downloads
Patches
Plain Diff
Add new file
parents
Branches
main
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
keyFinder
+131
-0
131 additions, 0 deletions
keyFinder
with
131 additions
and
0 deletions
keyFinder
0 → 100644
+
131
−
0
View file @
9e90b312
//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)))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment