heres my best shot at rsa encryption. public code yo
//Library
#include <iostream>
//#include <string.h>
//#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <windows.h>
//Prototypes, unneeded
//long long encrypt(long long msg, long long key_n, int key_e);
//long long decrypt(long long encrypted_msg, long long secret_n, long long secret_d);
bool isprime(long long num) //Function to check if number is prime
{
if(num <= 1)
return false;
if(num == 2)
return true;
if(num % 2 == 0)
return false;
for(int i = 3; i <= sqrt(num * 1.0); i += 2)
{
if(num % i == 0)
return false;
}
return true;
}
long long rand_prime(int lower, int upper) //Generate random prime number
{
long long spread = upper - lower + 1;
while(1)
{
long long p = 1 | (rand() % spread + lower);
if (isprime(p)) return p;
}
}
int gcd(long long a, long long b) //Greatest common divisor between two numbers
{
int temp;
while(b != 0)
{
temp = b;
b = a % b;
a = temp;
}
return a;
}
//Formula: C = P^e % n
long long encrypt(long long msg, long long key_n, int key_e) //Encryption with two keys
{
long long encrypted_msg = 1; //Set it to a value of 1 for now
for(int i = 0; i < key_e; i++) //Register for faster performance
{
encrypted_msg = encrypted_msg * msg % key_n;
}
encrypted_msg = encrypted_msg % key_n;
return encrypted_msg;
}
//Forumula: P = C^d % n
long long decrypt(long long encrypted_msg, long long secret_n, long long secret_d) //Decryption with two different keys
{
long long decrypted = 1;
for(int i = 0; i < secret_d; i++)
{
decrypted = decrypted * encrypted_msg % secret_n;
}
decrypted = decrypted % secret_n;
return decrypted;
}
//Second main
int actualmain()
{
unsigned long long p; //First prime
unsigned long long q; //Second prime
unsigned long long n; //Product of both primes *****THIS IS PART OF BOTH THE PRIVATE AND PUBLIC KEY
unsigned long long m; //Product of both primes following the (p-1)*(q-1) formula
unsigned int e; //Coefficient of m *****SECOND PART OF PUBLIC KEY
unsigned long long d; //d = (1 + nm)/e where n is any number, not the product of both primes *****SECOND PART OF PRIVATE KEY
p = rand_prime(1000,5000); //Choose two random primes within range
q = rand_prime(6000,10000);
//Math formulas below for RSA
n = p*q;
m = (p-1)*(q-1);
e = 0;
for(int i = 2;; i++)
{
if((gcd(i,m)) == 1)
{
e = i;
break;
}
}
for(int a = 0;; a++)
{
if((1+(a*m)) % e == 0)
{
d = (1+(a*m))/e;
break;
}
}
//Debugging
std::cout << "\np = " << p << std::endl;
std::cout << "q = " << q << std::endl;
std::cout << "n = " << n << std::endl;
std::cout << "m = " << m << std::endl;
std::cout << "e = " << e << std::endl;
std::cout << "d = " << d << std::endl;
//Get user message, limit to 256 characters
char message[256];
//char* message = new char[256];
ZeroMemory(message, 256);
std::cout << "Enter message: ";
std::cin.getline(message, 256);
//Encrypt character by character(OLD) Encrypt as whole array(NEW)
long long messageenc = encrypt((long long)message, n, e);
std::cout << "Encrypted: " << messageenc << std::endl;
//Decrypt character by character(OLD) Decrypt as a whole array(NEW)
std::cout << "Decrypted: " << (char*)decrypt(messageenc, n, d) << std::endl; //Debug test to see if it worked
std::cout << "\nEnter encryption code: ";
long long code;
long long newn;
long long newd;
std::cin >> code;
std::cout << "Enter Key(n): ";
std::cin >> newn;
std::cout << "Enter Key(d): ";
std::cin >> newd;
std::cout << "Decrypted: " << (char*)decrypt(code, newn, newd); //decrypt it and turn it into a char pointer
std::cin.ignore(); //Seriously this cin cleanup procedure, boi oh boi
std::cin.clear();
return 0;
}
int main()
{
srand(time(NULL)); //Generate random time seed (ONLY NEEDS TO BE EXECUTED ONCE)
std::cout << "*NOTE THAT NUMBERS MUST BE EXACTLY CORRECT OTHERWISE PROGRAM WILL CRASH*" << std::endl; //We don't need to continously spam this, so execute once
for(;;) //Forever, to avoid calling main(); continously
{
actualmain();
}
}
maybe a pro can look this over and help me with cleaning it up if it needs it
whoops removed
#include <string.h>
#include <stdlib.h>
was going to use them for something but dont recall what