Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions fairplay/src/components/About/About.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@
color: black;
width: 80%;
margin: auto;
background-color: rgba(
89,
75,
61,
0.8
); /* White background with decreased opacity */
border-radius: 10px; /* Rounded corners */
padding: 30px; /* Add padding */
}
2 changes: 1 addition & 1 deletion fairplay/src/components/ChatBox/Chatbot.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Chatbot = () => {
const apiEndpoint = "https://api.openai.com/v1/chat/completions";
const headers = {
"Content-Type": "application/json",
Authorization: ` Bearer ${config.OPENAI_API_KEY}`,
Authorization: ` Bearer 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`,
};

// Get the last 10 messages
Expand Down
1 change: 1 addition & 0 deletions fairplay/src/components/ChatBox/Chatbot.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

body {
font-family: "Frank Ruhl Libre", sans-serif;
color: #4d270a;
}

.chatbotInputForm input::placeholder {
Expand Down
82 changes: 82 additions & 0 deletions fairplay/src/components/CipherForm copy/CipherForm.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import React, { useState } from "react";
import FiveFiveStatic from "../FiveFiveStatic/FiveFiveStatic";
import FilterInputText from "../FilterInputText/FilterInputText";
import { encryptByPlayfairCipher } from "./encrypt";
import { decryptByPlayfairCipher } from "./decrypt";
import SubstringDisplay from "../SubstringDisplay/SubstringDisplay";

function CipherForm() {
const [plainText, setPlainText] = useState("");
const [key, setKey] = useState("");
const [cipherText, setCipherText] = useState("");
const [decryptedText, setDecryptedText] = useState("");
const [showEmptyCipher, setShowEmptyCipher] = useState(true);

const handlePlainTextChange = (e) => {
setPlainText(e.target.value.replace(/\s/g, ""));
};

const handleKeyChange = (e) => {
setKey(e.target.value);
};

const handleSubmit = (e) => {
e.preventDefault();

// Encrypt the plain text
setCipherText(
encryptByPlayfairCipher(plainText.replace(/\s/g, ""), key).toUpperCase()
);
};

const handleDecrypt = () => {
// Decrypt the cipher text
setDecryptedText(decryptByPlayfairCipher(cipherText, key).toUpperCase());
};

return (
<div>
<form onSubmit={handleSubmit}>
<div>
<div>
<label htmlFor="plainText">Plain Text:</label>
<input
type="text"
id="plainText"
value={plainText}
onChange={handlePlainTextChange}
/>
</div>
<br />
{plainText ? ( // Conditionally render FilterInputText if plainText is not empty
<FilterInputText inputString={plainText} />
) : (
<SubstringDisplay
substrings={[]}
length={0}
first={true}
encrypted={false}
/>
)}
<br />
<label htmlFor="key">Key:</label>
<input type="text" id="key" value={key} onChange={handleKeyChange} />
</div>

<br />
<FiveFiveStatic cipherText={key} />
<br />

<button type="submit">Encrypt</button>
</form>

<button onClick={handleDecrypt}>Decrypt</button>
<br />
{cipherText && <div>Encrypted Text: {cipherText}</div>}
<br />
{decryptedText && <div>Decrypted Text: {decryptedText}</div>}
</div>
);
}

export default CipherForm;
130 changes: 130 additions & 0 deletions fairplay/src/components/CipherForm copy/decrypt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
export function generateKeyTable(key, ks, keyT) {
let i,
j,
k,
flag = 0;

// a 26 character hashmap
// to store count of the alphabet
let dicty = new Array(26).fill(0);
for (i = 0; i < ks; i++) {
let r = key[i].charCodeAt(0) - 97;

if (key[i] !== "j") {
dicty[r] = 2;
}
}

dicty["j".charCodeAt(0) - 97] = 1;
i = 0;
j = 0;

for (k = 0; k < ks; k++) {
let r = key[k].charCodeAt(0) - 97;
if (dicty[r] === 2) {
dicty[r] -= 1;
keyT[i][j] = key[k];
j++;
if (j === 5) {
i++;
j = 0;
}
}
}

for (k = 0; k < 26; k++) {
if (dicty[k] === 0) {
keyT[i][j] = String.fromCharCode(k + 97);
j++;
if (j === 5) {
i++;
j = 0;
}
}
}
return keyT;
}

export function mod5(n) {
return ((n % 5) + 5) % 5;
}

export function decrypt(str, keyT, ps) {
let i;
let a = new Array(4).fill(0);
let newstr = new Array(ps);

function mod5(n) {
return ((n % 5) + 5) % 5;
}

for (i = 0; i < ps; i += 2) {
let brr = search(keyT, str[i], str[i + 1], a);
let k1 = brr[0];
let k2 = brr[1];
let k3 = brr[2];
let k4 = brr[3];
if (k1 === k3) {
newstr[i] = keyT[k1][mod5(k2 - 1)];
newstr[i + 1] = keyT[k1][mod5(k4 - 1)];
} else if (k2 === k4) {
newstr[i] = keyT[mod5(k1 - 1)][k2];
newstr[i + 1] = keyT[mod5(k3 - 1)][k2];
} else {
newstr[i] = keyT[k1][k4];
newstr[i + 1] = keyT[k3][k2];
}
}
let res = "";

for (let i = 0; i < newstr.length; i++) {
res += newstr[i];
}

return res;
}

export function search(keyT, a, b, arr) {
let i, j;

if (a === "j") a = "i";
else if (b === "j") b = "i";

for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
if (keyT[i][j] === a) {
arr[0] = i;
arr[1] = j;
} else if (keyT[i][j] === b) {
arr[2] = i;
arr[3] = j;
}
}
}
return arr;
}

// export export function to make the plain text length to be even
export function prepare(str, ptrs) {
if (ptrs % 2 !== 0) {
str += "z";
}

return [str, ptrs];
}

// export function to decrypt using Playfair Cipher
export function decryptByPlayfairCipher(str, key) {
let ps, ks;
let keyT = new Array(5).fill(null).map(() => new Array(5));

str = str.trim().toLowerCase();
key = key.trim().toLowerCase();

ps = str.length;
ks = key.length;
[str, ps] = prepare(str, ps);

let kt = generateKeyTable(key, ks, keyT);
return decrypt(str, kt, ps);
}
123 changes: 123 additions & 0 deletions fairplay/src/components/CipherForm copy/encrypt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
export function generateKeyTable(key, ks, keyT) {
let i,
j,
k,
flag = 0;

// a 26 character hashmap
// to store count of the alphabet
let dicty = new Array(26).fill(0);
for (i = 0; i < ks; i++) {
let r = key[i].charCodeAt(0) - 97;

if (key[i] !== "j") {
dicty[r] = 2;
}
}

dicty["j".charCodeAt(0) - 97] = 1;
i = 0;
j = 0;

for (k = 0; k < ks; k++) {
let r = key[k].charCodeAt(0) - 97;
if (dicty[r] === 2) {
dicty[r] -= 1;
keyT[i][j] = key[k];
j++;
if (j === 5) {
i++;
j = 0;
}
}
}

for (k = 0; k < 26; k++) {
if (dicty[k] === 0) {
keyT[i][j] = String.fromCharCode(k + 97);
j++;
if (j === 5) {
i++;
j = 0;
}
}
}
return keyT;
}

// export export function to search for the characters of a digraph
// in the key square and return their position
export function search(keyT, a, b, arr) {
let i, j;

if (a === "j") a = "i";
else if (b === "j") b = "i";

for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
if (keyT[i][j] === a) {
arr[0] = i;
arr[1] = j;
} else if (keyT[i][j] === b) {
arr[2] = i;
arr[3] = j;
}
}
}
return arr;
}

// export export function to make the plain text length to be even
export function prepare(str, ptrs) {
if (ptrs % 2 !== 0) {
str += "z";
}

return [str, ptrs];
}

// export export function for performing the encryption
export function encrypt(str, keyT, ps) {
let i;
let a = new Array(4).fill(0);
let newstr = new Array(ps);

for (i = 0; i < ps; i += 2) {
let brr = search(keyT, str[i], str[i + 1], a);
let k1 = brr[0];
let k2 = brr[1];
let k3 = brr[2];
let k4 = brr[3];
if (k1 === k3) {
newstr[i] = keyT[k1][(k2 + 1) % 5];
newstr[i + 1] = keyT[k1][(k4 + 1) % 5];
} else if (k2 === k4) {
newstr[i] = keyT[(k1 + 1) % 5][k2];
newstr[i + 1] = keyT[(k3 + 1) % 5][k2];
} else {
newstr[i] = keyT[k1][k4];
newstr[i + 1] = keyT[k3][k2];
}
}
let res = "";

for (let i = 0; i < newstr.length; i++) {
res += newstr[i];
}
return res;
}

export function encryptByPlayfairCipher(str, key) {
let ps, ks;
let keyT = new Array(5).fill(null).map(() => new Array(5));

str = str.trim().toLowerCase();
key = key.trim().toLowerCase();

ps = str.length;
ks = key.length;
[str, ps] = prepare(str, ps);

let kt = generateKeyTable(key, ks, keyT);
return encrypt(str, kt, ps);
}
Loading