cryptopals_c

cryptopals crypto challenges solutions in pure c
git clone git://git.superpozycja.net/cryptopals_c
Log | Files | Refs | README

commit 412d14330fd9fbd3e649e9c5f370f47a5ae65630
parent d6af37147d971612c349a27bc26f7fb8ba44b34d
Author: superpozycja <anna@superpozycja.net>
Date:   Sun,  1 Sep 2024 19:43:42 +0200

rename the lib; in c6 move functions out

Diffstat:
Msrc/s1/c3.c | 2+-
Msrc/s1/c4.c | 2+-
Msrc/s1/c5.c | 2+-
Msrc/s1/c6.c | 68++++----------------------------------------------------------------
4 files changed, 7 insertions(+), 67 deletions(-)

diff --git a/src/s1/c3.c b/src/s1/c3.c @@ -1,5 +1,5 @@ #include <ba.h> -#include <cryptanalysis.h> +#include <analysis.h> int main(int argc, char* argv[]) { diff --git a/src/s1/c4.c b/src/s1/c4.c @@ -1,7 +1,7 @@ #include <stdio.h> #include <ctype.h> #include <ba.h> -#include <cryptanalysis.h> +#include <analysis.h> #define MAXLEN 1000 diff --git a/src/s1/c5.c b/src/s1/c5.c @@ -1,7 +1,7 @@ #include <stdio.h> #include <ctype.h> #include <ba.h> -#include <cryptanalysis.h> +#include <analysis.h> int main() { diff --git a/src/s1/c6.c b/src/s1/c6.c @@ -1,70 +1,10 @@ #include <stdio.h> #include <ctype.h> #include <ba.h> -#include <cryptanalysis.h> +#include <analysis.h> #include <util.h> -float i_c(ba *ct) -{ - unsigned int occur[256]; - unsigned int i; - unsigned int t; - float res; - - res = 0; - memset(occur, 0, sizeof(occur)); - - for (i = 0; i < ct->len; i++) - occur[ct->val[i]]++; - - t = 0; - for (i = 0; i < 256; i++) - t += occur[i] * (occur[i] - 1); - - res = t / (float) (ct->len * (ct->len - 1)); - return res; -} - -float get_i_c_m(ba *ct, unsigned int m) -{ - float res; - int i; - res = 0; - for (i = 0; i < m; i++) { - ba *chunk = (ba *) malloc(sizeof(ba)); - int j; - int k; - - chunk->len = ct->len / m; - chunk->val = (uint8_t *) malloc(sizeof(uint8_t) * chunk->len); - - for (j = 0, k = 0; j < chunk->len && k + i < ct->len; j++, k += m) - chunk->val[j] = ct->val[k + i]; - res += i_c(chunk); - } - return res / m; -} - -unsigned int guess_m(ba *ct) -{ - unsigned int best; - float max; - int m; - - max = 0; - for (m = 1; m < 40; m++) { - float avg; - - avg = get_i_c_m(ct, m); - if (avg > max) { - max = avg; - best = m; - } - } - return best; -} - -void break_m(ba *ct, unsigned int m) +void break_w_keylen(ba *ct, unsigned int m) { int i; @@ -113,9 +53,9 @@ int main(int argc, char *argv[]) base64_to_hex(&hex, b64); ct = ba_from_hex(hex); - int m = guess_m(ct); + int m = guess_vigenere_keylen(ct); - break_m(ct, m); + break_w_keylen(ct, m); ba_fprint_ascii(ct, stdout, 0); printf("\n");