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 encrypt and want the values of x and y (the prime numbers) to be the same. Eg 3 and 7. Basically making the RSA code simpler. Please don't change or add to the used libraries.
#include
#include
#include
#include
int x, y, n, t, i, flag;
long int e[50], d[50], temp[50], j, m[50], en[50];
char msg[100];
int prime(long int);
void encryption_key();
long int cd(long int);
void encrypt();
void decrypt();
int main()
{
printf("\nENTER FIRST PRIME NUMBER\n");
scanf("%d", &x);
flag = prime(x);
if (flag == 0)
{
printf("\nINVALID INPUT\n");
exit(0);
}
printf("\nENTER SECOND PRIME NUMBER\n");
scanf("%d", &y);
flag = prime(y);
if (flag == 0 || x == y)
{
printf("\nINVALID INPUT\n");
exit(0);
}
printf("\nENTER MESSAGE OR STRING TO ENCRYPT\n");
scanf("%s", msg);
for (i = 0; msg[i] != '\0'; i++)
m[i] = msg[i];
n = x * y;
t = (x - 1) * (y - 1);
encryption_key();
printf("\nPOSSIBLE VALUES OF e AND d ARE\n");
for (i = 0; i < j - 1; i++)
printf("\n%ld\t%ld", e[i], d[i]);
encrypt();
decrypt();
return 0;
}
int prime(long int pr)
{
int i;
j = sqrt(pr);
for (i = 2; i <= j; i++)
{
if (pr % i == 0)
return 0;
}
return 1;
}
//function to generate encryption key
void encryption_key()
{
int k;
k = 0;
for (i = 2; i < t; i++)
{
if (t % i == 0)
continue;
flag = prime(i);
if (flag == 1 && i != x && i != y)
{
e[k] = i;
flag = cd(e[k]);
if (flag > 0)
{
d[k] = flag;
k++;
}
if (k == 99)
break;
}
}
}
long int cd(long int a)
{
long int k = 1;
while (1)
{
k = k + t;
if (k % a == 0)
return (k / a);
}
}
//function to encrypt the message
void encrypt()
{
long int pt, ct, key = e[0], k, len;
i = 0;
len = strlen(msg);
while (i != len)
{
pt = m[i];
pt = pt - 96;
k = 1;
for (j = 0; j < key; j++)
{
k = k * pt;
k = k % n;
}
temp[i] = k;
ct = k + 96;
en[i] = ct;
i++;
}
en[i] = -1;
printf("\n\nTHE ENCRYPTED MESSAGE IS\n");
for (i = 0; en[i] != -1; i++)
printf("%c", en[i]);
}
//function to decrypt the message
void decrypt()
{
long int pt, ct, key = d[0], k;
i = 0;
while (en[i] != -1)
{
ct = temp[i];
k = 1;
for (j = 0; j < key; j++)
{
k = k * ct;
k = k % n;
}
pt = k + 96;
m[i] = pt;
i++;
}
m[i] = -1;
printf("\n\nTHE DECRYPTED MESSAGE IS\n");
for (i = 0; m[i] != -1; i++)
printf("%c", m[i]);
printf("\n");
}
assuming you only want me to make this code such that you only
have to input string and rest program will do itself
code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int x, y, n, t, i, flag;
long int e[50], d[50], temp[50], j, m[50], en[50];
char msg[100];
int prime(long int);
void encryption_key();
long int cd(long int);
void encrypt();
void decrypt();
int main()
{
x = 3;
flag = prime(x);
y = 7;
flag = prime(y);
printf("\nENTER MESSAGE OR STRING TO ENCRYPT\n");
scanf("%s", msg);
for (i = 0; msg[i] != '\0'; i++)
m[i] = msg[i];
n = x * y;
t = (x - 1) * (y - 1);
encryption_key();
encrypt();
decrypt();
return 0;
}
int prime(long int pr)
{
int i;
j = sqrt(pr);
for (i = 2; i <= j; i++)
{
if (pr % i == 0)
return 0;
}
return 1;
}
//function to generate encryption key
void encryption_key()
{
int k;
k = 0;
for (i = 2; i < t; i++)
{
if (t % i == 0)
continue;
flag = prime(i);
if (flag == 1 && i != x && i != y)
{
e[k] = i;
flag = cd(e[k]);
if (flag > 0)
{
d[k] = flag;
k++;
}
if (k == 99)
break;
}
}
}
long int cd(long int a)
{
long int k = 1;
while (1)
{
k = k + t;
if (k % a == 0)
return (k / a);
}
}
//function to encrypt the message
void encrypt()
{
long int pt, ct, key = e[0], k, len;
i = 0;
len = strlen(msg);
while (i != len)
{
pt = m[i];
pt = pt - 96;
k = 1;
for (j = 0; j < key; j++)
{
k = k * pt;
k = k % n;
}
temp[i] = k;
ct = k + 96;
en[i] = ct;
i++;
}
en[i] = -1;
printf("\nTHE ENCRYPTED MESSAGE IS\n");
for (i = 0; en[i] != -1; i++)
printf("%c", en[i]);
}
//function to decrypt the message
void decrypt()
{
long int pt, ct, key = d[0], k;
i = 0;
while (en[i] != -1)
{
ct = temp[i];
k = 1;
for (j = 0; j < key; j++)
{
k = k * ct;
k = k % n;
}
pt = k + 96;
m[i] = pt;
i++;
}
m[i] = -1;
printf("\n\nTHE DECRYPTED MESSAGE IS\n");
for (i = 0; m[i] != -1; i++)
printf("%c", m[i]);
printf("\n");
}
COMMENT DOWN FOR ANY QUERY
PLEASE GIVE A THUMBS UP
C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want...
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 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();...
Write helpful comments for the following code: use Vigenere cipher tech to encrypt and decrypt message The code: #include<stdio.h> #include <stdlib.h> char arr[26][26]; char message[22], key[22], emessage[22], retMessage[22]; int findRow(char); int findColumn(char); int findDecRow(char, int); int main() { int i = 0, j, k, r, c; k = 96; for (i = 0; i<26; i++) { k++; for (j = 0; j<26; j++) { arr[i][j] = k++; if (k == 123) k = 97; } } printf("\nEnter message\n"); fgets(message, 22,...
4. Suppose you wish to encrypt the message, M 42 using RSA encryption. Given a public key where p- 23 and q-11 and the relative prime e- 7. Find n, and show all necessary steps to encrypt your message (42). (Hint: check p.411 of the text for information on public key RSA) (5 points)
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...
I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text and key My Current Code is: void SubDecrypt(char *message, char *encryptKey) { int iteration; int iteration_Num_Two = 0; int letter; printf("Enter Encryption Key: \n"); //Display the message to enter encryption key scanf("%s", encryptKey); //Input the Encryption key for (iteration = 0; message[iteration] != '0'; iteration++) //loop will continue till message reaches to end { letter = message[iteration]; ...
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...
Question: Please Provide Comments on each Line of code explaining what the C Function is doing throughout the code. // Function used for substitution encryption void SubEncrypt(char *message, char *encryptKey) { int iteration = 0; printf("Enter Aphabet Encryption Key: \n"); scanf("%s", encryptKey); for (iteration = 0; iteration < strlen(message); iteration++) { char letter = message[iteration]; if (letter >= 'A' && letter <= 'Z') { letter = encryptKey[letter - 'A']; } message[iteration] = letter; } printf("CipherText message: %s\n", message); } //_________________________________________________________________________________________________________________________________________________...
How to encrypt a string in C. I am using RSA encryption. I have a public and private key already. Public Key is (3,319) and private is (187,319) I have a string that says "I want to buy a gift". The person im sending this message to already has my public key. In C, how do encrpyt the string with my private key before I send it
Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util.*; import java.lang.*; /** * * @author STEP */ public class ShiftCipher { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); String plainText; System.out.print("Please enter your string: "); // get message plainText = input.nextLine(); System.out.print("Please enter your shift cipher key: "); // get s int s = input.nextInt(); int...