diff options
| -rw-r--r-- | set6/task41.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/set6/task41.c b/set6/task41.c new file mode 100644 index 0000000..6793ecc --- /dev/null +++ b/set6/task41.c @@ -0,0 +1,55 @@ +#include "../lib/lib.h" +#include "../lib/lib2.h" +#include "../lib/lib3.h" +#include "../lib/lib4.h" +#include "../lib/lib5.h" +#include "../lib/lib6.h" + +int main() +{ + struct rsa_key_bignum public, private; + unsigned char *mess = "Love of my live you hurt me. You stolen my heart"; + BIGNUM *bn_mess = BN_bin2bn(mess, strlen(mess), NULL); + BIGNUM *encrypted = BN_new(); + BIGNUM *decrypted = BN_new(); + out = BIO_new(BIO_s_file()); + BIO_set_fp(out, stdout, BIO_NOCLOSE); + ctx = BN_CTX_new(); + + printf("plaintext is: %s\n", mess); + BN_print(out, bn_mess); + + rsa_generate_key_bignum(&public, &private); + + rsa_encrypt_bignum(bn_mess, encrypted, &public); + + printf("\nencrpyted message is:\n"); + BN_print(out, encrypted); + BIGNUM *S = BN_new(); + // genrate random value mod N + BN_pseudo_rand(S, 256, -1, -1); + BN_nnmod(S, S, public.modulo, ctx); + + BIGNUM *C_ = BN_new(); + BIGNUM *tmp = BN_new(); + + BN_mod_exp(tmp, S, public.exponent, public.modulo, ctx); + BN_mod_mul(C_, tmp, encrypted, public.modulo, ctx); + + printf("\nmodified ciphertext for decryption is:\n"); + BN_print(out, C_); + + // ok submit this totally differen ciphertext for decryption + rsa_decrypt_bignum(C_, decrypted, &private); + // multiply by modinv of s + modular_multiplicative_inverse_bignum_my(tmp, S, public.modulo); + BN_mod_mul(tmp, decrypted, tmp, public.modulo, ctx); + printf("\nrecovered plaintext is:\n"); + BN_print(out, tmp); + printf("\n"); + unsigned char *__dec = malloc(strlen(mess)); + memset(__dec, 0, strlen(mess)); + BN_bn2bin(tmp, __dec); + printf("recovered plaintext: %s\n", __dec); + +} |
