Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update rsa.c #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"))){
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions rsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down