I use this code to create a Padding RSA function. Unfortunately when replacing in
mpz_get_str(str, base, N.get_mpz_t());
cout<<"\n\nLength of k = Modulus in bytes: "<<strlen(str);
str with str1 I receive segmentation dump. Why does this happen?
int main(const int argc, const char *const argv[])
{
// Check number of arguments
if (argc!=4){
printf("usage: %s [Message] [Exponent] [Modulus] \n", argv[0]);
return 1;
}
char *str;
char *str1="";
int base=10,l;
mpz_t op;
// Receive arguments
const mpz_class m(argv[1]), d(argv[2]),N(argv[3]),message(argv[1]);
mpz_get_str(str1, base, N.get_mpz_t());
cout<<"\n\nLength of k = Modulus in bytes: "<<strlen(str1);
// Calculate RSA
cout<<endl<<RSA(m,d,N);
//TestArea
cout<<"\n\n"<<m;
mpz_get_str(str, base, m.get_mpz_t());
cout<<"\n\nLength of string message in bytes: "<<strlen(str);
cout<<"\n\n"<<str;
return 0;
}
There are two errors (at least). One you've passed a string literal to mpz_get_str.
char *str1="";
mpz_get_str(str1, base, N.get_mpz_t());
String literals are not modifiable.
Secondly even if the string literal was modifiable you haven't allocated enough memory to hold your number.
Third error is conceptual. It seems you want to find out the number of bytes occupied by N
, this code, even when working, won't tell you that.
Here's the code to determine the number of bytes occupied by N
size_t num_bytes = mpz_size(N.get_mpz_t())*sizeof(mp_limb_t);
num_bytes
will be number of bytes used internally by GMP to store the magnitude of the number N
.