summaryrefslogtreecommitdiff
path: root/set6/task41.c
diff options
context:
space:
mode:
authorBenedict <benedict@0xb8000.de>2016-12-03 23:20:28 +0100
committerBenedict <benedict@0xb8000.de>2017-02-21 13:00:26 +0100
commitc735b384e726558608b5442bd42f0df7a7e2daf6 (patch)
treeabcc1f70757521704c39fcefeaf05758d76336d4 /set6/task41.c
parente5f83c17cb1d4edddad05aa3bf2ae84b8da739f6 (diff)
set6: task41: SOLVED!
Diffstat (limited to 'set6/task41.c')
-rw-r--r--set6/task41.c55
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);
+
+}