AMNA Arooj Rsa

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

DATA SECURITY AND APPLICATIONS

LAB TASK

Submitted By

Amna Arooj (F-301055)

Class: BSSE-3 (7th Semester)

Instructor Name
Engr. Fahim Muhammad Khan

Submission Date: 03rd-Jan-2024


LAB TASK
IMPLEMENTATION OF RSA CIPHER
Encrypting And Decrypting Plain Text Messages Containing
Alphabets and Numbers Using Their ASCII Value:
OUTPUT

CODE
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#define MAX_PRIME 250

static int prime[MAX_PRIME];


static int primeCount = 0;
static int* publicKey = NULL;
static int* privateKey = NULL;
static int* nValue = NULL;
static int* fiValue = NULL;

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;
}
}

for (int i = 0, j = 0; i < MAX_PRIME; i++) {


if (sieve[i]) {
prime[j++] = i;
primeCount++;
}
}
}

int PickRandomPrime() {
int k = rand() % primeCount;
int ret = prime[k];

for (int i = k; i < primeCount - 1; i++) {


prime[i] = prime[i + 1];
}

primeCount--;
return ret;
}

void SetKeys() {
int prime1 = PickRandomPrime();
int prime2 = PickRandomPrime();

*nValue = prime1 * prime2;


int fi = (prime1 - 1) * (prime2 - 1);
*fiValue = fi;

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 Encrypt(int message) {


int e = *publicKey;
int encryptedText = 1;
while (e > 0) {
encryptedText *= message;
encryptedText %= *nValue;
e -= 1;
}
return encryptedText;
}

int Decrypt(int encryptedText) {


int d = *privateKey;
int decrypted = 1;
while (d > 0) {
decrypted *= encryptedText;
decrypted %= *nValue;
d -= 1;
}
return decrypted;
}

int GCD(int a, int b) {


if (b == 0) {
return a;
}
return GCD(b, a % b);
}

void Encoder(const char* message, int* encoded) {


for (size_t i = 0; i < strlen(message); i++) {
encoded[i] = Encrypt((int)message[i]);
}
}

void Decoder(const int* encoded, char* decoded) {


for (size_t i = 0; i < strlen(decoded); i++) {
decoded[i] = (char)Decrypt(encoded[i]);
}
}

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();

const char* message = "Test Message";

int* coded = (int*)malloc(strlen(message) * sizeof(int));

Encoder(message, coded);

printf("Initial message:\n");
printf("%s\n\n", message);

printf("The encoded message (encrypted by public key):\n");


for (size_t i = 0; i < strlen(message); i++) {
printf("%d", coded[i]);
}
printf("\n\n");

printf("The decoded message (decrypted by public key):\n");


char* decoded = (char*)malloc(strlen(message) * sizeof(char));
Decoder(coded, decoded);
printf("%s\n", decoded);

free(publicKey);
free(privateKey);
free(nValue);
free(fiValue);
free(coded);
free(decoded);

return 0;
}
OUTPUT

CODE:

#include <stdio.h>
#include <math.h>

double gcd(double a, double h) {


double temp;
while (1) {
temp = fmod(a, h);
if (temp == 0)
return h;
a = h;
h = temp;
}
}

int main() {
double p = 3;
double q = 7;
double n = p * q;
double e = 2;
double phi = (p - 1) * (q - 1);

while (e < phi) {


if (gcd(e, phi) == 1)
break;
else
e++;
}

int k = 2;
double d = (1 + (k * phi)) / e;
double msg = 12;

printf("Message data = %.6f\n", msg);

// 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;
}

You might also like