jg943
jg943

Reputation: 309

unsigned long ints multiplication

I am creating a public key generator and I do c= p*q; Where p and q are large prime numbers, but I keep getting this for C 11875820813;

long unsigned int c= p*q;
printf("C is: %d\n", c);

I know I has something to do with my numbers, but I don't know how to fix this.

I am trying to multiply:

872017*533297

Upvotes: 0

Views: 2228

Answers (3)

Henrick Hellström
Henrick Hellström

Reputation: 2666

You can't do public key cryptography using only the built in integer types of C, regardless of platform. Contemporary cryptography deals with numbers that are at least 1024 bits wide, which, if implemented in software, requires libraries that support such large values.

Upvotes: 1

NSProgrammer
NSProgrammer

Reputation: 2396

32-bit unsigned long max value is 4294967295. Your multiplication is 465044050049 which overflows the unsigned long.

4294967295 (ULONG_MAX)
  <
465044050049 (your result)
  <
18446744073709551615 (ULONG_LONG_MAX)

You'll need to use an unsigned long long (64-bit unsigned integer type) (supposing your system supports them, which all modern systems do)

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409432

You might be overflowing the value. Remember that on a 32-bit platform an unsigned long can be at most a little over 4 billion.

Upvotes: 1

Related Questions