cryptopals_c

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

commit 68c30d63749eb3d54e8895ab069bfd5bdac3d320
parent 71ac60e9be73526d8564c0948c09cdf594b1f4d4
Author: anna <anna@brokenlove.online>
Date:   Tue, 27 Aug 2024 23:12:42 +0200

some style changes

Diffstat:
Ms1/c2.c | 7+++++--
Ms1/c4.c | 26+++++++++++++++++---------
2 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/s1/c2.c b/s1/c2.c @@ -1,15 +1,18 @@ #include <ba.h> /* see my tools repo for this */ int main(int argc, char* argv[]) { - if (argc != 3) - return -1; ba* a; ba* b; + + if (argc != 3) + return -1; + a = ba_from_hex(argv[1]); b = ba_from_hex(argv[2]); if (a == NULL || b == NULL) { return -1; } + ba_xor(a, b); ba_fprint(a, stdout, 0); printf("\n"); diff --git a/s1/c4.c b/s1/c4.c @@ -7,9 +7,11 @@ void preprocess_string(char (*l)[MAXLEN]) { + int i; + while (isspace(**l)) *l++; - for (int i = 0; i < strlen(*l); i++) { + for (i = 0; i < strlen(*l); i++) { if ((*l)[i] == '\n') { (*l)[i] = '\0'; break; @@ -19,23 +21,29 @@ void preprocess_string(char (*l)[MAXLEN]) int main(int argc, char* argv[]) { - if (argc != 2) - return -1; - FILE *p = fopen(argv[1], "r"); + unsigned long maxscore; char l[MAXLEN]; - //char best[MAXLEN]; - unsigned long maxscore = 0; + ba *best; + FILE *p; ba *ct; - ba *best = (ba *) malloc(sizeof(ba)); + + if (argc != 2) + return -1; + + p = fopen(argv[1], "r"); + maxscore = 0; + best = (ba *) malloc(sizeof(ba)); + while (fgets(l, sizeof(l), p)) { + unsigned long score; + preprocess_string(&l); ct = ba_from_hex(l); ct = decrypt_scxor(ct); - unsigned long score = score_english(ct); + score = score_english(ct); if (score > maxscore) { maxscore = score; ba_copy(best, ct); - //strcpy(best, l); } } ba_fprint_ascii(best, stdout, 0);