Last active
December 20, 2017 21:49
-
-
Save debuti/8240f427271b3c192aa85947c7301bbb to your computer and use it in GitHub Desktop.
A little tool to perform RSA key calculations, all the data is requested in base10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <gmp.h> | |
#include <stdio.h> | |
#include <assert.h> | |
/// gcc -o gmprsa gmprsa.c -g -lgmp && ./gmprsa | |
int main(){ | |
char inputStr[1024]; | |
mpz_t p,q,e,n,r,p_,q_,d; | |
int flag; | |
printf ("Enter p: "); | |
scanf("%1023s" , inputStr); | |
mpz_init(p); | |
mpz_set_ui(p,0); | |
flag = mpz_set_str(p, inputStr, 10); | |
assert (flag == 0); | |
printf ("p = "); | |
mpz_out_str(stdout, 10, p); | |
printf ("\n"); | |
printf ("Enter q: "); | |
scanf("%1023s" , inputStr); | |
mpz_init(q); | |
mpz_set_ui(q,0); | |
flag = mpz_set_str(q, inputStr, 10); | |
assert (flag == 0); | |
printf ("q = "); | |
mpz_out_str(stdout, 10, q); | |
printf ("\n"); | |
printf ("Enter e: "); | |
scanf("%1023s" , inputStr); | |
mpz_init(e); | |
mpz_set_ui(e,0); | |
flag = mpz_set_str(e, inputStr, 10); | |
assert (flag == 0); | |
printf ("e = "); | |
mpz_out_str(stdout, 10, e); | |
printf ("\n"); | |
mpz_init(n); | |
mpz_mul(n,p,q); | |
printf ("n = "); | |
mpz_out_str(stdout, 10, n); | |
printf ("\n"); | |
printf ("n (hex) = "); | |
mpz_out_str(stdout, 16, n); | |
printf ("\n"); | |
mpz_init(p_); | |
mpz_sub_ui(p_, p, 1); | |
mpz_init(q_); | |
mpz_sub_ui(q_, q, 1); | |
mpz_init(r); | |
mpz_mul(r,p_,q_); | |
printf ("r = "); | |
mpz_out_str(stdout, 10, r); | |
printf ("\n"); | |
mpz_init(d); | |
mpz_invert (d, e, r); | |
printf ("d = "); | |
mpz_out_str(stdout, 10, d); | |
printf ("\n"); | |
printf ("d (hex) = "); | |
mpz_out_str(stdout, 16, d); | |
printf ("\n"); | |
mpz_clear(p); | |
mpz_clear(q); | |
mpz_clear(e); | |
mpz_clear(n); | |
mpz_clear(r); | |
mpz_clear(p_); | |
mpz_clear(q_); | |
mpz_clear(d); | |
// Now you can echo your result hex string and pipe it to xxd -r -p | sha256sum | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment