Skip to content

Commit

Permalink
added score mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
VrindavanSanap committed Dec 25, 2023
1 parent 21ad492 commit de7e3ae
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 14 deletions.
44 changes: 44 additions & 0 deletions crypto.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
const chars = "abcdefghijklmnopqrstuvwxyz";
import { chisquare } from "jstat"
import {add, dotDivide, inv, pow, map, sum, multiply, square, subtract, divide} from "mathjs"
let expected_prob = [0.08167, 0.01492, 0.02782, 0.04253, 0.12702, 0.02228, 0.02015,
0.06094, 0.06966, 0.00153, 0.00772, 0.04025, 0.02406, 0.06749, 0.07507,
0.01929, 0.00095, 0.05987, 0.06327, 0.09056, 0.02758, 0.00978, 0.02360,
0.00150, 0.01974, 0.00074]

export const stoi = {};
export const itos = {};
Expand All @@ -7,6 +13,7 @@ for (let i = 0; i < chars.length; i++) {
stoi[ch] = i;
itos[i] = ch;
}

export function ceaser_cipher(string, n = 0){
string = string.replace(/[^a-zA-Z]/g, '').toLowerCase();
let result = "";
Expand All @@ -16,4 +23,41 @@ export function ceaser_cipher(string, n = 0){
}
return result;
}
export function word_freq(string) {
/*
Given a string, return frequencies as a list
*/
let frequencies = new Array(26).fill(0);

for (let i = 0; i < string.length; i++) {
let char = string[i];

if (/[a-zA-Z]/.test(char)) {
char = char.toLowerCase();
let index = char.charCodeAt(0) - 'a'.charCodeAt(0);
frequencies[index]++;
}
}
return frequencies;
}

export function chi_squared(expected, observed){
let epsilon = 0.001
return sum(dotDivide(map(subtract(expected, observed), square) ,
add(expected,epsilon)))
}

export function sentence_score(string){
/*
Given string return score of its likliness to be a english sentence
*/
let len = string.length

let freq = word_freq(string)
let exp_freq = multiply(expected_prob , len)
let chi_squared_value= chi_squared(exp_freq, freq)
const p_value = chisquare.cdf(chi_squared_value, 25);

return p_value
}
console.log(sentence_score(""))
102 changes: 102 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
"lint": "next lint"
},
"dependencies": {
"jstat": "^1.9.6",
"mathjs": "^12.2.1",
"next": "14.0.4",
"react": "^18",
"react-dom": "^18",
"next": "14.0.4"
"react-dom": "^18"
}
}
29 changes: 17 additions & 12 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Head from 'next/head'
import Image from 'next/image'
import {IBM_Plex_Mono, Inter} from 'next/font/google'
import styles from '@/styles/Home.module.css'
import {ceaser_cipher} from "../crypto.js"
import {ceaser_cipher, sentence_score} from "../crypto.js"

const inter = Inter({ subsets: ['latin'] })
const ibm_plex = IBM_Plex_Mono({ subsets: ['latin'], weight:["100", "200", "300", "400", "500", "600", "700"] })
Expand All @@ -13,34 +13,39 @@ export default function Home() {
function handle_message_change(event){
set_message(event.target.value)
}
function encrypted_messages(message){
function encrypted_messages(message) {
let enc_messages = [];
for (let i = 0; i<26; i++){
for (let i = 0; i < 26; i++) {
const encrypted = ceaser_cipher(message, i);
const score = sentence_score(encrypted).toFixed(2); // Limit to 2 decimal places

enc_messages.push(
<p key={i}>
+{i + " "}
{ceaser_cipher(message, i)}
+{i + " "}
{encrypted + " "}
{score}
</p>
)
);


}
return enc_messages

}
return (

<div className = {`${ibm_plex.className}`}>
<h1>Break Ceaser Cipher</h1>
<label>
Message:
<input style={ {fontSize: 18 }}value={message} onChange={handle_message_change}/>
<input style = {{fontSize: 18 }}
value = {message}
onChange = {handle_message_change}/>
</label>
<p>Striped message: {ceaser_cipher(message)}</p>
<h2>Encrypted Messages:</h2>
<ul>
{
encrypted_messages(message)
}
</ul>
{encrypted_messages(message)}
</ul>
</div>
)
}

0 comments on commit de7e3ae

Please sign in to comment.