working encryption with sample data

This commit is contained in:
2026-03-29 17:07:54 +02:00
parent 66e261165c
commit f72b2156cb

32
main.c
View File

@@ -48,7 +48,7 @@ uint64_t quick_pow(uint64_t *d_binary, uint64_t a, uint64_t n, uint64_t length)
} }
bool prime_test(uint64_t n, int a) { bool prime_test(uint64_t n, int a) {
printf("\n\nprime test: %ju\n", n); // printf("\n\nprime test: %ju\n", n);
// Miller Rabin prime test // Miller Rabin prime test
// choose a base: a, which should be a prime so that (n, a) = 1 // choose a base: a, which should be a prime so that (n, a) = 1
// then do 2 rounds of tests provided the first one did not fail // then do 2 rounds of tests provided the first one did not fail
@@ -84,10 +84,10 @@ bool prime_test(uint64_t n, int a) {
for (int i = 0; i <= r; i++) { for (int i = 0; i <= r; i++) {
if (first_qp_res == n - 1) { if (first_qp_res == n - 1) {
free(d_binary); free(d_binary);
printf("true\n"); // printf("true\n");
return true; return true;
} else if (first_qp_res < n - 2) { } else if (first_qp_res < n - 2) {
printf("first_qp_res became smaller then n!!\n"); // printf("first_qp_res became smaller then n!!\n");
break; break;
} else { } else {
first_qp_res = (uint64_t)(((unsigned __int128)first_qp_res * first_qp_res) % n); first_qp_res = (uint64_t)(((unsigned __int128)first_qp_res * first_qp_res) % n);
@@ -108,9 +108,9 @@ void *prime_thread_worker(void *arg) {
do { do {
result_ptr->prime = rand64(); result_ptr->prime = rand64();
printf("\nGenerating a new prime number (%p). Candidate: ", result_ptr); // printf("\nGenerating a new prime number (%p). Candidate: ", result_ptr);
printf("%ju", result_ptr->prime); // printf("%ju", result_ptr->prime);
printf("\n"); // printf("\n");
} while (!prime_test(result_ptr->prime, result_ptr->base)); } while (!prime_test(result_ptr->prime, result_ptr->base));
return NULL; return NULL;
@@ -157,6 +157,10 @@ euklidian_result_t euklidian_algorigthm_extended(unsigned __int128 a, unsigned _
} }
int main() { int main() {
uint64_t m = 0;
printf("give input for m: \n");
scanf("%ju", &m);
srand(time(NULL)); srand(time(NULL));
uint64_t base = 2; uint64_t base = 2;
@@ -172,6 +176,9 @@ int main() {
printf("\n"); printf("\n");
p.prime = 11;
q.prime = 29;
unsigned __int128 n = p.prime * q.prime; unsigned __int128 n = p.prime * q.prime;
print_uint128(n); print_uint128(n);
printf("\n"); printf("\n");
@@ -186,8 +193,17 @@ int main() {
e = rand64(); e = rand64();
} while (e <= 1 && e >= fi_n && prime_test(e, base)); } while (e <= 1 && e >= fi_n && prime_test(e, base));
euklidian_result_t test = euklidian_algorigthm_extended(192, 11); e = 17;
printf("test lnko: %ju\n", test.lnko);
euklidian_result_t calc_d = euklidian_algorigthm_extended(fi_n, e);
__int128 d = calc_d.y;
uint64_t length = 0;
uint64_t *nyenye = dec_to_bin(e, &length);
unsigned __int128 c = quick_pow(nyenye, m, n, length);
free(nyenye);
printf("\nc: ");
print_uint128(c);
return 0; return 0;
} }