Skip to content

Commit

Permalink
fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
VrindavanSanap committed Mar 7, 2024
1 parent 4b02b99 commit fe594e8
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 29 deletions.
2 changes: 1 addition & 1 deletion crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function ceaser_cipher(string, n, keep_spaces = true, keep_non_alpha = tr
result = result.replace(/\s/g, '');
}
if (!keep_non_alpha) {
result = result.replace(/[^A-Za-z]/g, '');
result = result.replace(/[^a-zA-Z\s]/g, '');
}

return result;
Expand Down
65 changes: 37 additions & 28 deletions pages/index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
"use client"
import { useState, useEffect} from 'react'; import Head from 'next/head'
import { useState, useEffect } from 'react'; import Head from 'next/head'
import Image from 'next/image'
import {IBM_Plex_Mono, Inter} from 'next/font/google'
import { IBM_Plex_Mono, Inter } from 'next/font/google'
import styles from '@/styles/Home.module.css'
import {ceaser_cipher, sentence_score} 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"] })
const ibm_plex = IBM_Plex_Mono({
subsets: ['latin'],
weight: ["100", "200", "300", "400",
"500", "600", "700"]
})

export default function Home() {
const [message, set_message] = useState("")
const [lowest_score, set_lowest_score] = useState(10000)
const [most_likely_message, set_most_likely_message] = useState("")
const [keep_spaces, set_keep_spaces] = useState(true)
const [keep_non_alpha, set_keep_non_alpha] = useState(true)
useEffect(() => {
handle_message_change({ target: { value: "iylhrjlhzlyjpwoly"}})
console.log(keep_spaces, keep_non_alpha);
useEffect(() => {
handle_message_change({ target: { value: "iylhrjlhzlyjpwoly" } })
}, [])

function handle_message_change(event){

function handle_message_change(event) {
set_lowest_score(100000)
set_message(event.target.value)
}
}
function encrypted_messages(message) {
let enc_messages = [];
let score = 10000
let score = 10000
for (let i = 0; i < 26; i++) {
const encrypted = ceaser_cipher(message, i, keep_spaces, keep_non_alpha);
score = sentence_score(encrypted); // Limit to 2 decimal places
// console.log(score, lowest_score)
if (score < lowest_score) {
set_lowest_score(score);
set_most_likely_message(encrypted);
}
}


enc_messages.push(
<p key={i}>
Expand All @@ -45,8 +48,8 @@ export default function Home() {
{score}
</p>
);


}
return enc_messages
}
Expand All @@ -59,33 +62,39 @@ export default function Home() {
</label>
);
};
function handle_keep_spaces_changed() {
set_keep_spaces(!keep_spaces);
}
function handle_keep_non_alpha_changed() {
set_keep_non_alpha(!keep_non_alpha);
}


return (

<div className = {`${ibm_plex.className}`}>
<div className={`${ibm_plex.className}`}>
<h1>Break Ceaser Cipher</h1>

<label>
Message:
<input style = {{fontSize: 18 }}
value = {message}
onChange = {handle_message_change}/>
Message:
<input style={{ fontSize: 18 }}
value={message}
onChange={handle_message_change} />
</label>
<br/>
<br/>
<br />
<br />
<Checkbox
label=" Keep spaces: "
value={keep_spaces}
onChange={() => { set_keep_spaces(!keep_spaces) }}
onChange={handle_keep_spaces_changed}

/>
<Checkbox
label=" Keep non alpha: "
value={keep_non_alpha}
onChange={() => { set_keep_non_alpha(!keep_non_alpha) }}
onChange={handle_keep_non_alpha_changed}
/>

<p>Striped message: {ceaser_cipher(message,0,keep_spaces, keep_non_alpha)}</p>
<div>{keep_non_alpha}</div>
<p>Striped message: {ceaser_cipher(message, 0, keep_spaces, keep_non_alpha)}</p>
<p><i> The lower the score the better </i></p>
<h2>Encrypted Messages, Scores:</h2>
<p>Your message is most likely <b>{most_likely_message} </b></p>
Expand Down
47 changes: 47 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

const chars_lower = "abcdefghijklmnopqrstuvwxyz";
const chars_upper = chars_lower.toUpperCase();

export const stoi = {};
export const itos = {};
for (let i = 0; i < 52; i += 2) {
const ch_l = chars_lower[i / 2];
const ch_u = chars_upper[i / 2];
stoi[ch_l] = i;
stoi[ch_u] = i + 1;
itos[i] = ch_l;
itos[i + 1] = ch_u;
}


function ceaser_cipher(string, n, keep_spaces = true, keep_non_alpha = true) {

let result = "";
n *= 2;
for (let i = 0; i < string.length; i++) {
let c = string[i];
if (c == " ") {
result += " "
continue
}
let ri = itos[(stoi[c] + n) % 52]
if (ri !== undefined) {
result += ri;
} else {
result += c;
}
}
if (!keep_spaces) {
result = result.replace(/\s/g, '');
}
if (!keep_non_alpha) {
result = result.replace(/[^A-Za-z]/g, '');
}

return result;
}
let msg = "iylhrjlhzlyjp1 woly";
let res = ceaser_cipher(msg,19);
console.log(res);


0 comments on commit fe594e8

Please sign in to comment.