summaryrefslogtreecommitdiff
path: root/set5/task39.c
diff options
context:
space:
mode:
Diffstat (limited to 'set5/task39.c')
-rw-r--r--set5/task39.c127
1 files changed, 127 insertions, 0 deletions
diff --git a/set5/task39.c b/set5/task39.c
new file mode 100644
index 0000000..40c00ef
--- /dev/null
+++ b/set5/task39.c
@@ -0,0 +1,127 @@
+#include "../lib/lib.h"
+#include "../lib/lib2.h"
+#include "../lib/lib3.h"
+#include "../lib/lib4.h"
+#include "../lib/lib5.h"
+#include <time.h>
+
+#include<openssl/bn.h>
+#include<openssl/bio.h>
+
+int modular_multiplicative_inverse(int number, int _modulo)
+{
+ struct extended_euclid tmp;
+ extended_euclid_algo(number, _modulo, &tmp);
+ // only has a inverse iff gcd = 1
+ if ( tmp.d != 1)
+ return INT_MIN;
+
+ // mod works not fine for negytive numbers in c
+ return modulo(tmp.s, _modulo);
+}
+/*
+ * TODO do it iterative, maybe stack it not big enough
+void extended_euclid_algo_bignum(BIGNUM *a, BIGNUM *b, struct extended_euclid_bignum *e)
+{
+ struct extended_euclid_bignum tmp;
+ tmp.d = BN_new();
+ tmp.s = BN_new();
+ tmp.t = BN_new();
+
+ if (BN_is_zero(b)) {
+ e->d=a;
+ BN_one(e->s);
+ BN_zero(e->t);
+ }
+ BIGNUM *mod = BN_new();
+ BN_mod(mod, a, b, ctx);
+
+ extended_euclid_algo_bignum(b, mod, &tmp);
+ BN_copy(e->d, tmp.d);
+ BN_copy(e->s, tmp.t);
+ BN_div(mod, NULL, a, b, ctx);
+ BN_mul(mod, mod, tmp.s, ctx);
+ BN_sub(e->t, tmp.s, mod);
+ //BN_copy(e->t, );
+
+ BN_free(mod);
+ BN_free(tmp.d);
+ BN_free(tmp.s);
+ BN_free(tmp.t);
+ printf("durchlauf von extended_euclid durch\n");
+ return;
+}
+int modular_multiplicative_inverse_bignum(BIGNUM *res, BIGNUM *number, BIGNUM *modulo)
+{
+//
+ struct extended_euclid_bignum tmp;
+ tmp.d = BN_new();
+ tmp.s = BN_new();
+ tmp.t = BN_new();
+ extended_euclid_algo_bignum(number, modulo, &tmp);
+ // only has a invese iff gcd = 1
+ if (BN_is_one(tmp.d))
+ return -1;
+
+ return BN_mod(res, tmp.s, modulo, ctx);
+}
+*/
+
+#define BN_DEBUG
+
+int main()
+{
+ struct rsa_key_bignum private, public;
+ // debugging: printing BN's
+ BIO *out = BIO_new(BIO_s_file());
+ BIO_set_fp(out, stdout, BIO_NOCLOSE);
+
+ ctx = BN_CTX_new();
+
+ rsa_generate_key_bignum(&private, &public);
+
+ printf("message:\n");
+ BIGNUM *message = BN_new();
+ BIGNUM *encrypted = BN_new();
+ BIGNUM *decrypted = BN_new();
+ BN_set_word(message, 4234667);
+ BN_print(out, message);
+
+ if(!rsa_encrypt_bignum(message, encrypted, &public))
+ die("could not rsa encrypt message");
+
+ printf("\nencrypted rsa message\n");
+ BN_print(out, encrypted);
+
+ if(!rsa_decrypt_bignum(encrypted, decrypted, &private))
+ die("could not rsa decrypt");
+
+ printf("\ndecrypted message:\n");
+ BN_print(out, decrypted);
+
+ BN_CTX_free(ctx);
+ free_rsa_key_bignum(&private);
+ free(public.exponent);
+}
+
+int main_littlenum()
+{
+ int message = 65;
+ int p = 5, q = 11;
+ int n = p * q;
+ int et = (p-1) * (q-1);
+ int e = 3;
+
+ // does not work, nums are above INT_MAX
+ int d = modular_multiplicative_inverse(e, et);
+ // public key is [e, n], private key is [d, n]
+ struct rsa_key public = { .exponent = e, .modulo = n };
+ struct rsa_key private = { .exponent = d, .modulo = n };
+ printf("public key is: %i, %i\n", public.exponent, public.modulo);
+ printf("private key is: %i, %i\n", private.exponent, private.modulo);
+ int ciphertext = rsa_encrypt(message, &public);
+ printf("encrpyt %i: %i\n", message, ciphertext);
+ int dec_message = rsa_decrpyt(ciphertext, &private);
+ printf("decrypt %i: %i\n", ciphertext, dec_message);
+ return 0;
+}