AMNA Arooj Rsa
AMNA Arooj Rsa
AMNA Arooj Rsa
LAB TASK
Submitted By
Instructor Name
Engr. Fahim Muhammad Khan
CODE
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
void PrimeFiller() {
bool sieve[MAX_PRIME];
memset(sieve, true, sizeof(sieve));
sieve[0] = false;
sieve[1] = false;
for (int i = 2; i < MAX_PRIME; i++) {
for (int j = i * 2; j < MAX_PRIME; j += i) {
sieve[j] = false;
}
}
int PickRandomPrime() {
int k = rand() % primeCount;
int ret = prime[k];
primeCount--;
return ret;
}
void SetKeys() {
int prime1 = PickRandomPrime();
int prime2 = PickRandomPrime();
int e = 2;
while (1) {
if (GCD(e, fi) == 1) {
break;
}
e += 1;
}
*publicKey = e;
int d = 2;
while (1) {
if ((d * e) % fi == 1) {
break;
}
d += 1;
}
*privateKey = d;
}
int main() {
srand((unsigned int)time(NULL));
PrimeFiller();
publicKey = (int*)malloc(sizeof(int));
privateKey = (int*)malloc(sizeof(int));
nValue = (int*)malloc(sizeof(int));
fiValue = (int*)malloc(sizeof(int));
SetKeys();
Encoder(message, coded);
printf("Initial message:\n");
printf("%s\n\n", message);
free(publicKey);
free(privateKey);
free(nValue);
free(fiValue);
free(coded);
free(decoded);
return 0;
}
OUTPUT
CODE:
#include <stdio.h>
#include <math.h>
int main() {
double p = 3;
double q = 7;
double n = p * q;
double e = 2;
double phi = (p - 1) * (q - 1);
int k = 2;
double d = (1 + (k * phi)) / e;
double msg = 12;
// Encryption c = (msg ^ e) % n
double c = fmod(pow(msg, e), n);
printf("Encrypted data = %.6f\n", c);
// Decryption m = (c ^ d) % n
double m = fmod(pow(c, d), n);
printf("Original Message Sent = %.6f\n", m);
return 0;
}