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
73
74
75
76
77
|
#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 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;
}
#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");
unsigned char *mess = "Hello, i wanna get encrypted";
printf("%s\n", mess);
BIGNUM *message = BN_bin2bn(mess, strlen(mess), NULL);
//BIGNUM *message = BN_new();
//BN_set_word(message, 4234667);
BIGNUM *encrypted = BN_new();
BIGNUM *decrypted = BN_new();
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);
unsigned char *dec_mess = malloc(BN_num_bytes(decrypted));
BN_bn2bin(decrypted, dec_mess);
printf("\ndecrepyted message:\n%s\n", dec_mess);
BN_CTX_free(ctx);
free_rsa_key_bignum(&private);
free(public.exponent);
}
|