-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripty.js
134 lines (116 loc) · 3.37 KB
/
scripty.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const isPrime= (prime)=>{
let i, j;
j = (Math.floor(Math.sqrt(prime)));
for ( i = 2; i <= j; i++){
if ( prime % i == 0 ){
return false;
}
}
return true;
}
const calculateE= (t)=>{
// e ( 1 < e < t )
let e;
for ( e = 2; e < t; e++ ){
if (greatestCommonDivisor( e, t ) == 1 ){
return e;
}
}
return -1;
}
const greatestCommonDivisor= (e,t)=>{
while ( e > 0 ){
let myTemp;
myTemp = e;
e = t % e;
t = myTemp;
}
return t;
}
const calculateD= (e,t)=>{
// d · e = 1 (mod f(n))
let d;
let k = 1;
while ( 1 ){
k = k + t;
if ( k % e == 0){
d = (k / e);
return d;
}
}
}
const encrypt= (i,e,n)=>{
// i = i.charCodeAt();
// console.log("*",i)
let current, result;
current = i - 97;
result = 1;
for ( let j = 0; j < e; j++ ){
result = result * current;
result = result % n;
}
//console.log("*",result)
return result;
}
const decrypt= (i,d,n)=>{
let current, result;
current = i;
result = 1;
for ( let j = 0; j < d; j++ ){
result = result * current;
result = result % n;
}
return result + 97;
}
const main = () =>{
let p, q, n, t, e, d;
let encryptedText = [100];
//memset(encryptedText, 0, sizeof(encryptedText));
let decryptedText = [100];
var flag;
var msg;
document.write("<center><b>Welcome to RCA program</b></center><br /><br />");
do{
p = prompt ("Enter a Prime number p ",911);
document.writeln("<br /> P: ",p);
flag = isPrime( p );
if ( flag == false ){
document.write("<br /> WRONG INPUT (This number is not Prime. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself) <br />");
}
} while ( flag == false );
do{
q = prompt("Enter a Prime number q :",971);
document.writeln("<br /> Q: ",q)
flag = isPrime( q );
if ( flag == false ){
document.write("<br /> WRONG INPUT (This number is not Prime. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself) <br />");
}
} while ( flag == false);
n = p * q;
document.write("<br /> Result of computing n = p*q = " + n +"<br />");
t = ( p - 1 ) * ( q - 1 );
document.write("Result of Q(n): = " + t +"<br />");
e = calculateE( t );
d = calculateD( e, t );
document.write("<br /> RSA public key is (n = "+ n + ", e = " + e + ")"+"<br />");
document.write("RSA private key is (n = " + n + ", d = " + d + ")"+ "<br />");
msg = prompt("Enter Message to be encryped:","GroupeNumberTwo");
// there is a newline character left in the input stream, so we use ignore()
document.write("<br /> The message is: " + msg + "<br />");
// encryption
for (let i= 0; i < msg.length; i++){
encryptedText[i] = encrypt( msg[i].charCodeAt(), e, n);
}
document.write("<br /> THE ENCRYPTED MESSAGE IS: ");
for ( let i = 0; i < msg.length; i++ ){
document.write("<b>",String.fromCharCode(encryptedText[i]),"</b>") ;
}
//decryption
for (let i = 0; i < msg.length; i++){
decryptedText[i] = decrypt(encryptedText[i], d, n);
}
document.write("<br /> <br /> THE DECRYPTED MESSAGE IS: ");
for (let i = 0; i < msg.length; i++){
document.write("<b>",String.fromCharCode(decryptedText[i]),"</b>" );
}
}