summaryrefslogtreecommitdiff
path: root/set5/task40.c
blob: 42ce66e284cb12f6dfd74c5314a0afc4e12efe5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "../lib/lib.h"
#include "../lib/lib2.h"
#include "../lib/lib3.h"
#include "../lib/lib4.h"
#include "../lib/lib5.h"

int main()
{
	int i;
	struct rsa_key_bignum private[3];
	struct rsa_key_bignum public[3];
	BIGNUM *res = BN_new();
	BIGNUM *encrypted[3];
	BIGNUM *decrypted = BN_new();
	BIGNUM *ab[3];
	BIGNUM *nb[3];
	BIGNUM *solution_no_mod = BN_new();

	out = BIO_new(BIO_s_file());
	BIO_set_fp(out, stdout, BIO_NOCLOSE);

	ctx = BN_CTX_new();

	printf("encrypt the following message with three different keys:\n");
	unsigned char *mess = "All you need is love! Love is all you need";
	printf("%s\n", mess);

	for(i=0;i<2;i++) {
		rsa_generate_key_bignum(&public[i], &private[i]);
		do {
			rsa_generate_key_bignum(&public[i+1], &private[i+1]);
		} while (!BN_cmp(private[i].exponent, private[i+1].exponent));
	}

	printf("private keys:\n");
	for(i=0;i<3;i++) {
		BN_print(out, private[i].exponent);
		printf("\n");
	}

	BIGNUM *message = BN_bin2bn(mess, strlen(mess), NULL);
	printf("message as BN:\n");
	BN_print(out, message);
	printf("\nencrypted rsa messages:\n");
	for(i=0;i<3;i++) {
		encrypted[i] = BN_new();
		if(!rsa_encrypt_bignum(message, encrypted[i], &public[i]))
			die("could not rsa encrypt message");

		BN_print(out, encrypted[i]);
		printf("\n");
	}

	for(i=0;i<3;i++) {
		ab[i] = BN_new();
		nb[i] = BN_new();
		BN_copy(ab[i], encrypted[i]);
		BN_copy(nb[i], public[i].modulo);
	}

	BIGNUM *n_3 = BN_new();
	BN_set_word(n_3, 3);
	
	rsa_broadcast_cube(res, ab, nb);
	printf("\nrsa broadcast cube is\n");
	BN_print(out, res);
	printf("\noriginal message is:\n");
	BN_print(out, message);
	unsigned char *__dec = malloc(1000);
	BN_bn2bin(res, __dec);	
	printf("\ndecrypted text with rsa broadcast attack: %s\n", __dec);
}