#code's not executing, even though everything seems fine

6 messages · Page 1 of 1 (latest)

vague wedge
#

im trying to implement the RSA encryption algorithm however for some reason the code's not running; am i missing something?
the code :
#define fastio ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define pii pair<int,int>
#define ll long long
#include <bits/stdc++.h>
using namespace std;

//public key cryptography

ll modexp(ll base, ll exp, ll mod){
ll ans=1;
base%=mod;
while(exp>0){
if(exp&1) ans=(ansbase)%mod;
base=(base
base)%mod;
exp>>=1;
}
return ans;
}

bool isPrime(ll num){
if(num<2) return false;
for(int i=2;i*i<=num;i++){
if(num%i==0) return false;
}
return true;
}

void rsa(ll num,ll p,ll q){
ll n=pq;
ll eulerTotient=(p-1)
(q-1);
ll E,D;
//public key E such that 1<E<eulerTotient and gcd(E,eulerTotient)=1
for(ll i=2;i<eulerTotient;i++){
if(__gcd(i,eulerTotient)==1){
E=i;
break;
}
}
cout<<"Public key : "<<E<<endl;
//getting private key
for(ll i=2;i<eulerTotient;i++){
if(((i*E)%eulerTotient)==1){
D=i;
break;
}
}
cout<<"Private key : "<<D<<endl;
ll cyphr, dcrypt;
cyphr=modexp(num,E,n);
cout<<"Encrypted number : "<<cyphr<<endl;
dcrypt=modexp(cyphr,D,n);
cout<<"Decrypted number : "<<dcrypt<<endl;
}

int main(){
fastio;
ll p,q;
cout<<"Enter p : ";
cin>>p;
if(!isPrime(p)){
cout<<"p is not prime.";
return -1;
}
cout<<"Enter q : ";
cin>>q;
if(!isPrime(q)){
cout<<"q is not prime.";
return -1;
}
ll pt;
cout<<"Enter value to encrypt : ";
cin>>pt;
rsa(pt,p,q);
return 0;
}

opaque tapirBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

vague wedge
#

!solved

opaque tapirBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

edgy tartan
#

why is this written like a competitive programming solution 😭

#

if you're not under extreme time pressure, you should take the time to write your code in a "standard" way