C Programming - RSA encryption
Hi there, I'm struggling with writing a C program to encrypt and decrypt a string using the RSA algorithm (C90 language only).
It can contain ONLY the following libraries: stdio, stdlib, math and string.
I want to use the following function prototypes to try and implement things:
/*Check whether a given number is prime or not*/
int checkPrime(int n)
/*to find gcd*/
int gcd(int a, int h)
void Encrypt();
void Decrypt();
int getPublicKeys();
int getPrivateKeys();
An attempt so far, but it doesn't work and can't encrypt words:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
/*Check whether a given number is prime or not*/
int checkPrime(int n)
{
int i, j;
j = sqrt(n);
for (i=2; i<=j; i++)
{
if(n%i==0)
return 0;
}
/*Finding the greatest common denominator*/
int gcd(int a, int h)
{
int temp;
while (1)
{
temp = a % h;
if (r == 0)
return h;
a = h;
h = temp;
}
}
void Encrypt(int n, int e)
{
double msg;
printf("\nMessage to be encrypted: ");
scanf("%lf", &msg);
double c = pow(msg, e);
c = fmod(c, n);
printf("\nData that's encrypted: %lf", c);
}
void Decrypt(int n, int d)
{
double msg;
printf("\nMessage to be decrypted: ");
scanf("%lf", &msg);
double c = pow(msg, d);
c = fmod(c, n);
printf("\nData decrypted (Original message): %lf,
c);
}
void getPublicKeys(int p, int q, int *n, int *e)
{
*n = p * q;
*e = 2;
int phi = (p - 1) * (q - 1);
while ((*e) < phi)
{
if (gcd((*e), phi) != 1)
(*e)++;
else
break;
}
}
int getPrivateKeys(int p, int q, int e)
{
int k = 2;
int phi = (p - 1) * (q - 1);
int d = (1 + (k * phi)) / e;
return d;
}
/*Driver Function*/
int main()
{
int p;
int q;
printf("\nChoose two random prime numers p&q:
");
scanf("%d %d",&p,&q);
if (checkPrime(p) || checkPrime(q))
{
printf("One or more of the numbers
are not prime\nExiting...\n");
return 0;
}
int n, e;
getPublicKeys(p, q, &n, &e);
int d = getPrivateKeys(p, q, e);
printf("%d %d %d\n", n, e, d);
printf("Keys Generated...\n\n");
printf("-----------MENU-----------\n");
printf("1. Encrypt a message\n");
printf("2. Decrypt a message\n");
printf("3. Exit the program\n");
while (1)
{
int ch;
printf("Enter your choice:
");
scanf("%d", &ch);
if (ch == 1)
Encrypt(n,
e);
else if (ch == 2)
Decrypt(n,
d);
else if (ch == 3)
break;
else
printf("Wrong
choice\n");
}
return 0;
}
Please don't do something like this: http://www.trytoprogram.com/c-examples/c-program-to-encrypt-and-decrypt-string/. I'm trying to incorporate this into a header file and the use of global variables and a fix sized encryption and decryption for a char[50] long is making it impossible. Something a little easier would be better. Appreciate any help
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
/*Check whether a given number is prime or not*/
int checkPrime(int n)
{
int i, j;
j = sqrt(n);
for (i=2; i<=j; i++)
{
if(n%i==0)
{
return 0;
}
}
return 1;
}
/*Finding the greatest common denominator*/
int gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
void Encrypt(double n, double e)
{
double msg;
printf("Message to be encrypted: ");
scanf("%lf", &msg);
double c = pow(msg, e);
c = fmod(c, n);
printf("Data that's encrypted: %lf", c);
}
void Decrypt(double n, double d)
{
double msg;
printf("Message to be decrypted: ");
scanf("%lf", &msg);
double c = pow(msg, d);
c = fmod(c, n);
printf("Data decrypted (Original message): %lf", c);
}
void getPublicKeys(double p, double q, double *n, double
*e,double *phi)
{
*n = p * q;
*e = 2;
*phi = (p - 1) * (q - 1);
while (*e < *phi)
{
if (gcd(*e, *phi) != 1)
(*e)++;
else
break;
}
}
int getPrivateKeys(double phi,double e)
{
int k = 2;
double d = (1 + (k * phi)) / e;
return d;
}
/*Driver Function*/
int main()
{
double p,q,n,e,phi,d;
printf("Choose two random prime numers p&q: ");
scanf("%lf %lf",&p,&q);
if (!checkPrime(p)&&!checkPrime(q))
{
printf("One or more of the numbers are not prime
Exiting...");
return 0;
}
getPublicKeys(p, q, &n, &e, &phi);
d = getPrivateKeys(phi, e);
printf("%lf %lf %lf\n", n, e, d);
printf("Keys Generated...\n\n");
printf("-----------MENU-----------\n");
printf("1. Encrypt a message\n");
printf("2. Decrypt a message\n");
printf("3. Exit the program\n");
while (1)
{
int ch;
printf("Enter your choice: ");
scanf("%d", &ch);
if (ch == 1)
Encrypt(n, e);
else if (ch == 2)
Decrypt(n, d);
else if (ch == 3)
break;
else
printf("Wrong choice\n");
}
return 0;
}

C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string usi...
C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want to be able to integrate it into a header file which contains codes for compression, linked list etc.. However, the use of global variables and fixed size of encryption is making it hard to do so Can someone please help me modify the following the code? I want to be able to just pass it in a string to...
C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want to be able to integrate it into a header file which contains codes for compression, linked list etc.. However, the use of global variables and fixed size of encryption is making it hard to do so Can someone please help me modify the following the code? I want to be able to just pass it in a string to...
Write code for RSA encryption package rsa; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class RSA { private BigInteger phi; private BigInteger e; private BigInteger d; private BigInteger num; public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Enter the message you would like to encode, using any ASCII characters: "); String input = keyboard.nextLine(); int[] ASCIIvalues = new int[input.length()]; for (int i = 0; i < input.length(); i++) { ASCIIvalues[i] = input.charAt(i); } String ASCIInumbers...
Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...
Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...
Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin, cout, cin.getline) . Re-make the func function by the following prototype: void func( double, double &); #include int user_interface(); double func(double); void print_table(); int main (int argc, char *argv[]) { print_table(user_interface()); return 0 ; } int user_interface(int val){ int input = 0; printf("This function takes in x and returns an output\n"); printf("Enter Maximum Number of X:"); scanf("%d", &input);...
I am told to find the median of random inputs using if statements in C #include <stdio.h> #include <stdlib.h> int main() { double a, b, c, d; scanf("%lf", &a); scanf("%lf", &b); scanf("%lf", &c); if (a<b && b<c) d = b; printf("%.0f\n", d); else if (b<a && a<c) d = a; printf("%.0f\n", d); else d = c; printf("%.0f\n", d); return 0; } That is my code, I keep getting an error for the else if and else saying there is no...
Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the OpenMP parallel program much longer? Serial Program #include <stdio.h> #include <string.h> #include <time.h> int main(){ char str[1000], ch; int i, frequency = 0; clock_t t; struct timespec ts, ts2; printf("Enter a string: "); gets(str); int len = strlen(str); printf("Enter a character...
C programming Rewrite the following code replacing the else-if construct with a switch statement. Make sure you test your code. Supplied code #include <stdio.h> int main(void) { char ch; int countA = 0; int countE = 0; int countI = 0; printf("Enter in a letter A, E, or I.\n"); scanf(" %c", &ch); //Replace the following block with your switch if(ch == 'E' || ch == 'e') countE++; else if(ch == 'A' || ch == 'a') countA++; else if(ch == 'I'...
i'm having trouble making an else statement that will allow the program to add and output the average the valid numbers inputed into the program. Here's my output: Enter Score 1:36 Enter Score 2:-1 Invalid Input Enter Score 2:89.5 Enter Score 3:42 Enter Score 4:66.3 Enter Score 5:93 You entered: 36.000000, 89.500000, 42.000000, 66.300000, 93.000000 Total Score: 362.800000 Average Score: 72.560000 Max Score: 93.000000 Min Score: 36.000000 Here's my code: #include <stdio.h> int main(void) { double score[5]; double min; double...