diff --git a/rsa.c b/rsa.c index 42ca2fa..a1cb0b5 100644 --- a/rsa.c +++ b/rsa.c @@ -46,6 +46,7 @@ long long ExtEuclid(long long a, long long b) long long rsa_modExp(long long b, long long e, long long m) { if (b < 0 || e < 0 || m <= 0){ + printf("%s: Invalid parameter(s)!\n", __FUNCTION__); exit(1); } b = b % m; @@ -57,12 +58,13 @@ long long rsa_modExp(long long b, long long e, long long m) if( e % 2 == 1){ return ( b * rsa_modExp(b, (e-1), m) % m ); } - + printf("%s: Impossible!\n", __FUNCTION__); + exit(1); } // Calling this function will generate a public and private key and store them in the pointers // it is given. -void rsa_gen_keys(struct public_key_class *pub, struct private_key_class *priv, char *PRIME_SOURCE_FILE) +void rsa_gen_keys(struct public_key_class *pub, struct private_key_class *priv, const char *PRIME_SOURCE_FILE) { FILE *primes_list; if(!(primes_list = fopen(PRIME_SOURCE_FILE, "r"))){ @@ -126,6 +128,9 @@ void rsa_gen_keys(struct public_key_class *pub, struct private_key_class *priv, phi_max = (p-1)*(q-1); } while(!(p && q) || (p == q) || (gcd(phi_max, e) != 1)); + + fclose(primes_list); + primes_list = NULL; // Next, we need to choose a,b, so that a*max+b*e = gcd(max,e). We actually only need b // here, and in keeping with the usual notation of RSA we'll call it d. We'd also like diff --git a/rsa.h b/rsa.h index f0208f3..77a8a7f 100644 --- a/rsa.h +++ b/rsa.h @@ -28,12 +28,12 @@ void rsa_gen_keys(struct public_key_class *pub, struct private_key_class *priv, // This function will encrypt the data pointed to by message. It returns a pointer to a heap // array containing the encrypted data, or NULL upon failure. This pointer should be freed when // you are finished. The encrypted data will be 8 times as large as the original data. -long long *rsa_encrypt(const char *message, const unsigned long message_size, const struct public_key_class *pub); +long long *rsa_encrypt(const char *message, unsigned long message_size, const struct public_key_class *pub); // This function will decrypt the data pointed to by message. It returns a pointer to a heap // array containing the decrypted data, or NULL upon failure. This pointer should be freed when // you are finished. The variable message_size is the size in bytes of the encrypted message. // The decrypted data will be 1/8th the size of the encrypted data. -char *rsa_decrypt(const long long *message, const unsigned long message_size, const struct private_key_class *pub); +char *rsa_decrypt(const long long *message, unsigned long message_size, const struct private_key_class *pub); #endif diff --git a/test.c b/test.c index 95c6c9f..5b0a56b 100644 --- a/test.c +++ b/test.c @@ -40,7 +40,7 @@ int main(int argc, char **argv) printf("%lld\n", (long long)decrypted[i]); } - printf("\n"); + printf("\nDone\n"); free(encrypted); free(decrypted); return 0;