-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsa.cpp
More file actions
69 lines (56 loc) · 1.16 KB
/
rsa.cpp
File metadata and controls
69 lines (56 loc) · 1.16 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "rsa.h"
int prime_p = 61;
int prime_q = 53;
// Function to calculate the greatest common divisor
int gcd(int a, int b)
{
if (b == 0)
{
return a;
}
else
{
return gcd(b, a % b);
}
}
// Function to generate the keys
void generateKeys(int *e, int *d, int *n)
{
int p, q;
// Choose two large prime numbers
p = prime_p; // Example
q = prime_q; // Example
*n = p * q; // Compute n
int phi = (p - 1) * (q - 1); // compute φ(n)
// Choose a public exponent(s)
*e = 17; // Example, must be less than φ(n) and relatively prime to φ(n)
// Compute private exponent (d)
*d = 1;
while (((*d) * (*e)) % phi != 1)
{
(*d)++;
}
}
// Encrypt function
int encrypt(int message, int e, int n)
{
int cipher = 1;
for (int i = 0; i < e; i++)
{
cipher = (cipher * message) % n;
}
return cipher;
}
// Decrypt function
int decrypt(int cipher, int d, int n)
{
int message = 1;
for (int i = 0; i < d; i++)
{
message = (message * cipher) % n;
}
return message;
}