Skip to content

Commit 0f15ed1

Browse files
committed
added RSA Solver
1 parent 7b05683 commit 0f15ed1

4 files changed

Lines changed: 379 additions & 0 deletions

File tree

Projects/RSA SOLVER/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# RSA SOLVER
2+
3+
This website aims to solve RSA encryption problems.
4+
5+
## ALGORITHM
6+
7+
1. First we take 2 prime numbers, p and q, and calculate their product n = p * q.
8+
2. Then we calculate the totient of n, which is (p-1) * (q-1).
9+
3. We then take a number e, which is coprime to the totient of n.
10+
4. We then calculate the modular multiplicative inverse of e and the totient of n, which is d. or e * d = 1 mod totient(n)
11+
5. We then take the message, m, and encrypt it using the public key (e, n) to get the ciphertext, c. or c = m^e mod n
12+
6. We then decrypt the ciphertext, c, using the private key (d, n) to get the message, m. or m = c^d mod n
13+
14+
## ALGORITHM
15+
16+
1. First we take 2 prime numbers, p and q, and calculate their product n = p * q.
17+
2. Then we calculate the totient of n, which is (p-1) * (q-1).
18+
3. We then take a number e, which is coprime to the totient of n.
19+
4. We then calculate the modular multiplicative inverse of e and the totient of n, which is d. or e * d = 1 mod totient(n)
20+
5. We then take the message, m, and encrypt it using the public key (e, n) to get the ciphertext, c. or c = m^e mod n
21+
6. We then decrypt the ciphertext, c, using the private key (d, n) to get the message, m. or m = c^d mod n
22+
23+
24+
25+
## Features
26+
27+
- [x] RSA Encryption
28+
- [x] RSA Decryption
29+
- [x] RSA Key Generation
30+
31+
## Screenshots
32+
33+
- ### Key genration
34+
![image](https://github.com/dhruvmillu/Dev-Geeks/assets/75222710/bed099d6-4bf4-499e-b5c1-db5053590599)
35+
36+
- ### Encryption
37+
![image](https://github.com/dhruvmillu/Dev-Geeks/assets/75222710/0d6fe5fd-1e51-45fa-8e0a-38674eeafabb)
38+
39+
- ### Decryption
40+
![image](https://github.com/dhruvmillu/Dev-Geeks/assets/75222710/1b3fec4c-e656-4432-aba3-7dd6258e9164)

Projects/RSA SOLVER/index.html

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!DOCTYPE html>
2+
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
3+
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
4+
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
5+
<!--[if gt IE 8]> <html class="no-js"> <![endif]-->
6+
<html>
7+
<head>
8+
<meta charset="utf-8"/>
9+
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
10+
<title></title>
11+
<meta name="description" content=""/>
12+
<meta name="viewport" content="width=device-width, initial-scale=1"/>
13+
<link rel="stylesheet" href="style.css"/>
14+
</head>
15+
<body>
16+
17+
<div>
18+
<h1 class="title">RSA SOLVER</h1>
19+
</div>
20+
<div class="container">
21+
<h2 class="title">Enter your values</h2>
22+
<div class="innerContainer">
23+
<div style="margin-bottom:100px;margin-top:40px;">
24+
<p class='label-text'>enter 2 prime numbers</p>
25+
<p class='label-text'>p = </p>
26+
<input type="number" id="p" name="p" placeholder="p"/>
27+
<p class='label-text'>q = </p>
28+
<input type="number" id="q" name="q" placeholder="q"/>
29+
30+
<button id="genBtn">Generate Keys</button>
31+
</div>
32+
<div>
33+
<p class='label-text'>public key = </p>
34+
<input type="text" id="e" name="e" placeholder="e" disabled/>
35+
<p class='label-text'>private key = </p>
36+
<input type="text" id="d" name="d" placeholder="d" disabled/>
37+
</div>
38+
</div>
39+
</div>
40+
<div class="container">
41+
<h3>
42+
Choose your operation
43+
</h3>
44+
<div>
45+
<select id="op" name="op" placeholder="Operation">
46+
<option value="encrypt">Encrypt</option>
47+
<option value="decrypt">Decrypt</option>
48+
</select>
49+
50+
51+
</div>
52+
<div class="container">
53+
<h2 class="title" id="opTitle">Encrypt</h2>
54+
<div class="innerContainer">
55+
<div>
56+
<p class='label-text'>enter message</p>
57+
<input type="number" name="plain-text" id="input"/>
58+
<button type="button" id="opBtn">Encrypt</button>
59+
</div><div>
60+
<p class='label-text' id="result">Cipher text = </p>
61+
<input type="number" name="cipher-text" id="output" disabled/>
62+
</div>
63+
</div>
64+
</div>
65+
66+
<script src="script.js" async defer></script>
67+
</body>
68+
</html>

Projects/RSA SOLVER/script.js

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
2+
// variables
3+
4+
var pInput = document.getElementById("p");
5+
var qInput = document.getElementById("q");
6+
var eInput = document.getElementById("e");
7+
var dInput = document.getElementById("d");
8+
var pTextInput = document.getElementById("input");
9+
var cTextInput = document.getElementById("output");
10+
var genBtn = document.getElementById("genBtn");
11+
var op = document.getElementById("op");
12+
var opBtn = document.getElementById("opBtn");
13+
14+
var p = 0;
15+
var q = 0;
16+
var n = 0;
17+
var phi = 0;
18+
var e = 0;
19+
var d = 0;
20+
var isEncryption = true;
21+
22+
23+
24+
25+
26+
27+
28+
//setters
29+
function setP(val) {
30+
p = val;
31+
}
32+
33+
function setQ(val) {
34+
q = val;
35+
}
36+
37+
function setN(val) {
38+
n = val;
39+
}
40+
41+
function setPhi(val) {
42+
phi = val;
43+
}
44+
45+
function setE(val) {
46+
e = val;
47+
}
48+
49+
function setD(val) {
50+
d = val;
51+
}
52+
53+
54+
//event listeners
55+
56+
genBtn.addEventListener("click", function () {
57+
p = parseInt(pInput.value);
58+
q = parseInt(qInput.value);
59+
generateKeys();
60+
eInput.value = "("+e+","+n+")";
61+
dInput.value = "("+d+","+n+")";
62+
});
63+
64+
op.addEventListener("click", function (event) {
65+
if(event.target.value == "encrypt"){
66+
isEncryption = true;
67+
document.getElementById("opTitle").innerHTML = "Encrypt";
68+
opBtn.innerHTML = "Encrypt";
69+
document.getElementById("result").innerHTML = "Cipher text = ";
70+
}else{
71+
isEncryption = false;
72+
document.getElementById("opTitle").innerHTML = "Decrypt";
73+
opBtn.innerHTML = "Decrypt";
74+
document.getElementById("result").innerHTML = "Plain text = ";
75+
}
76+
});
77+
78+
opBtn.addEventListener("click", function () {
79+
if(isEncryption){
80+
encrypt();
81+
}else{
82+
decrypt();
83+
}
84+
});
85+
86+
87+
//checkprime - function to check if a number is prime
88+
function checkprime(n) {
89+
if (n === 2) {
90+
return true;
91+
} else if (n % 2 === 0 || n === 1 || n === 0) {
92+
return false;
93+
} else {
94+
for (let i = 3; i <= Math.sqrt(n); i += 2) {
95+
if (n % i === 0) {
96+
return false;
97+
}
98+
}
99+
return true;
100+
}
101+
}
102+
103+
//gcd - function to find gcd of two numbers
104+
function gcd(a, b) {
105+
if (b === 0) {
106+
return a;
107+
} else {
108+
return gcd(b, a % b);
109+
}
110+
}
111+
112+
113+
114+
// generateKeys - function to generate keys using p and q
115+
116+
function generateKeys() {
117+
if (p && q) {
118+
if (checkprime(p) && checkprime(q)) {
119+
let nz = p * q; // n = p*q
120+
let phiz = (p - 1) * (q - 1); // phi = (p-1)*(q-1)
121+
setN(nz);
122+
setPhi((p - 1) * (q - 1));
123+
124+
let ez = 2; // e = 2
125+
while (ez < phiz) {
126+
if (gcd(ez, phiz) === 1) {
127+
// e and phi must be coprime
128+
break;
129+
} else {
130+
ez++;
131+
}
132+
}
133+
134+
setE(ez);
135+
136+
let dz = 1;
137+
while (dz < phiz) {
138+
if ((ez * dz) % phiz === 1) {
139+
// d = (1/e)mod(phi)
140+
break;
141+
} else {
142+
dz++;
143+
}
144+
}
145+
setD(dz);
146+
console.log("p: " + p);
147+
console.log("q: " + q);
148+
console.log("n: " + n);
149+
console.log("phi: " + phi);
150+
console.log("e: " + e);
151+
console.log("d: " + d);
152+
153+
} else {
154+
alert("p and q must be prime");
155+
}
156+
} else {
157+
alert("p and q cannot be empty");
158+
}
159+
}
160+
161+
162+
163+
//binarymod - function to calculate (b^e)mod(m)
164+
function binarymod(b, e, m) {
165+
let r = 1;
166+
b = b % m;
167+
while (e > 0) {
168+
if (e % 2 == 1) {
169+
r = (r * b) % m;
170+
}
171+
e = Math.floor(e / 2);
172+
b = (b * b) % m;
173+
}
174+
return r;
175+
}
176+
177+
178+
function encrypt() {
179+
console.log("encrypt")
180+
let m = parseInt(pTextInput.value);
181+
let c = binarymod(m, e, n);
182+
cTextInput.value = c;
183+
}
184+
185+
function decrypt() {
186+
let c = parseInt(pTextInput.value);
187+
let m = binarymod(c, d, n);
188+
console.log(c,m)
189+
cTextInput.value = m;
190+
}

Projects/RSA SOLVER/style.css

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;600;700;800;900&display=swap');
2+
3+
:root {
4+
margin: 0 auto;
5+
font-family: 'Orbitron';
6+
padding:79px;
7+
color:white;
8+
9+
}
10+
11+
body{
12+
background-color: #1b1b1b;
13+
}
14+
15+
.container{
16+
display: flex;
17+
flex-direction: column;
18+
align-items: center;
19+
}
20+
21+
.title{
22+
font-size:39px;
23+
line-height: 49px;
24+
margin-bottom: 30px;
25+
}
26+
27+
.innerContainer{
28+
display:flex;
29+
flex-direction:column ;
30+
justify-content: center;
31+
align-items: center;
32+
background-color: #3b3b3b;
33+
padding:1.75rem;
34+
border-radius: 23px;
35+
font-size: 28px;
36+
}
37+
38+
input{
39+
background-color: #1c1c1c;
40+
max-width: 150px;
41+
border: none;
42+
padding: 1.25rem 0.75rem;
43+
border-radius: 9px;
44+
outline: none;
45+
margin-right: 20px;
46+
font-size: 28px;
47+
color:white;
48+
49+
}
50+
51+
input[type="text"]{
52+
max-width: 300px;
53+
margin-right: 0px;
54+
}
55+
56+
.label-text{
57+
display: inline-block;
58+
margin-left:30px;
59+
margin-right:20px ;
60+
}
61+
62+
button {
63+
border-radius: 8px;
64+
border: 1px solid transparent;
65+
padding: 0.6em 1.2em;
66+
font-size: 1em;
67+
font-weight: 500;
68+
font-family: inherit;
69+
background-color: #1a1a1a;
70+
cursor: pointer;
71+
transition: border-color 0.25s;
72+
color:white;
73+
}
74+
button:hover {
75+
border-color: #646cff;
76+
}
77+
button:focus,
78+
button:focus-visible {
79+
outline: 4px auto -webkit-focus-ring-color;
80+
}
81+

0 commit comments

Comments
 (0)