Ads
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>RSA Encryption</title>
</head>
<body>
<center>
<h1>RSA Encryption</h1>
<form id="rsaForm">
<label for="p">Enter First Prime Number:</label>
<input type="number" id="p" value="53"><br>
<label for="q">Enter Second Prime Number:</label>
<input type="number" id="q" value="59"><br>
<label for="msg">Enter Message (Cipher Text):</label>
<input type="number" id="msg" value="89"><br>
<button type="button" onclick="RSA()">Apply RSA</button>
</form>
<hr>
<div id="output"></div>
</center>
<script>
function RSA() {
var gcd = function(a, b) { return (!b) ? a : gcd(b, a % b); };
var p = parseInt(document.getElementById('p').value);
var q = parseInt(document.getElementById('q').value);
var no = parseInt(document.getElementById('msg').value);
var n = p * q;
var t = (p - 1) * (q - 1);
var e, d, ct;
for (e = 2; e < t; e++) {
if (gcd(e, t) == 1) {
break;
}
}
for (var i = 0; i < 10; i++) {
var x = 1 + i * t;
if (x % e == 0) {
d = x / e;
break;
}
}
var ctt = Math.pow(no, e).toFixed(0);
ct = ctt % n;
var dtt = Math.pow(ct, d).toFixed(0);
dt = dtt % n;
document.getElementById('output').innerHTML =
"<p>Public Key: " + n + "</p>" +
"<p>Exponent: " + e + "</p>" +
"<p>Private Key: " + d + "</p>" +
"<p>Cipher Text: " + ct + "</p>";
}
</script>
</body>
</html>