Home > Enterprise >  how can i solve the Floating point error core dump in this code?
how can i solve the Floating point error core dump in this code?

Time:10-27

Hi i'm getting trouble to run this code, it's a project for school, i need to make an RSA key,but the time i compiled it,it fail. I have this type of error:

[1] 14790 floating point exception (core dumped) ./a.out

i don't use C since 2019 so i forgot some stuff

sorry for the confusion of the code i wrote, i'm just digging in a mental ocean full of shit :)

#include <math.h>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;

/* -- COSTANTS -- */
const int DIM_ARRAY_KEY = 2;
/* -- -------- --*/

bool isPrime(int number) {  // funzione che controlla se un numero è primo
  for(int i = 2; i <= number / 2; i  ) {  // arrivo fino alla meta' del numero
    if(number % i == 0) {
      return false;
      break;
    }
    return true;
  }
  //return 1;
}
int getPrimeNumber() {  // funzione che ritorna un numero primo
  int tmp_number;
  do {
    tmp_number = rand() % 999   1;
    if (isPrime(tmp_number)) {
      return tmp_number;
    }

  } while(!isPrime(tmp_number));
  //return 1;
}

int getE(int n_){
  int e;
  do{
    e = rand() % 1000;
  }while(e > 1 && e < n_ && e/n_ == 0);{
    return e;
  }
}

void getPQ(int (&array_p_q)[DIM_ARRAY_KEY]){ // funzione che ritorna un array dove P è in prima posizione e Q è in seconda posizione
  int p, q;
  do {
    p = rand() % 1000;
    q = rand() % 1000;
  } while(p == q || !isPrime(p) || !isPrime(q));

  array_p_q[0] = p;
  array_p_q[1] = q;
}
int getV(int p, int q) {
  return (p-1) * (q-1);
}
int getN(int p, int q) {
  return p * q;
}
void getVscomposta(int v) { // funzione che ritorna i numeri che compongono V in un array
  vector<int> numbers;
  int i = 1;
  // numbers.push_back(val) - numbers.insert(val) per inserire infondo
  while (v > 1) {
    if (isPrime(i) && v%i == 0) {
      v /= i;
      numbers.push_back(i);
    }
    i  ;
  }
}
int getNpri(int (&v_scomposta)[3], int v, int p, int q) {  // diverso da P, Q e i numeri che compongono V
  getVscomposta(v); // array di numeri che compongono V
  for(int i = 0; i < 3; i  ){
    if(v == v_scomposta[i]){
      //cambia
      break;
    }
  }
  if(v == p && v == q){//cambia
  }

  return 0;
}

int getPrivateKey(int n_, int e_){
  int k = rand() % 20;
  int d = (k*(n_) 1)/e_;
  cout<<"private key: "<<d;
  return d;
}

int main(){
  srand(time(NULL));
  int Kpri[DIM_ARRAY_KEY], Kpub[DIM_ARRAY_KEY];

  // trovo P e Q
  int array_p_q[DIM_ARRAY_KEY];
  getPQ(array_p_q);

  int p = array_p_q[0];
  int q = array_p_q[1];
  int v = getV(p, q);

  cout<<"p: "<<p<<"\n"<<"q: "<< q<<"\n"<<"V: "<< v;

  int n = getN(p, q);
  int e = getE(n);
  getPrivateKey(n, e);
  return 0;
}

CodePudding user response:

This method:

int getE(int n_){
  int e;
  do{
    e = rand() % 1000;
  }while(e > 1 && e < n_ && e/n_ == 0);{
    return e;
  }
}

Can return zero, and the return value is used in a divide statement.

Also, I don't know what's with the extra brace indentation near the end.

int getE(int n_){
  int e;
  do{
    e = rand() % 1000;
  }while(e > 1 && e < n_ && e/n_ == 0);

  return e;
}

I think the difference between yours and mine suggests you intended some other code or something. An if-statement? Dunno.

  •  Tags:  
  • c
  • Related