cryptopals/src/set1.c

114 lines
2.8 KiB
C
Raw Normal View History

2024-04-11 13:57:44 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include "lfcrypto.h"
#include "lfinput.h"
#include "lfparsing.h"
#include "helpers.h"
static void challenge1() {
const char *input = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d";
size_t sz = 0;
const unsigned char *hex = hex_decode(input, &sz);
unsigned char *s = b64_encode(hex, sz);
printf("01-01: %s\n", s);
free(hex);
free(s);
}
static void challenge2() {
const char *input = "1c0111001f010100061a024b53535009181c";
size_t sz = 0;
const unsigned char *hex = hex_decode(input, &sz);
const char *input2 = "686974207468652062756c6c277320657965";
size_t sz2 = 0;
const unsigned char *hex2 = hex_decode(input2, &sz2);
unsigned char *sol = malloc(sizeof(unsigned char) * sz);
for (size_t i = 0; i < sz; ++i) {
sol[i] = hex[i] ^ hex2[i];
}
printf("01-02: ");
for (size_t i = 0; i < sz; ++i) {
printf("%x", sol[i]);
}
printf("\n");
free(hex);
free(hex2);
free(sol);
}
static void challenge3() {
const char *input = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736";
size_t sz = 0;
unsigned char *hex = hex_decode(input, &sz);
char answer[256];
int score = 0;
for (char c = 0; c < CHAR_MAX; ++c) {
const char *encode = one_byte_xor_s(hex, sz, c);
int t = simple_english_scoring(encode);
if (t > score && strstr(encode, "\n") == NULL) {
score = t;
strncpy(answer, encode, 256);
}
free(encode);
}
printf("01-03: %s\n", answer);
free(hex);
}
static void challenge4() {
size_t lines_sz = 0;
char **lines = get_lines("input/1-4", &lines_sz);
char answer[256];
int score = 0;
for (size_t i = 0; i < lines_sz; ++i) {
size_t hex_sz = 0;
unsigned char *hex = hex_decode(lines[i], &hex_sz);
for (char c = 0; c < CHAR_MAX; ++c) {
const char *encode = one_byte_xor_s(hex, hex_sz, c);
int t = simple_english_scoring(encode);
if (t > score) {
score = t;
strncpy(answer, encode, 256);
}
free(encode);
}
free(hex);
}
printf("01-04: %s", answer);
del_lines(lines);
}
static void challenge5() {
char *input = "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal";
const unsigned char* x = repeating_key_xor_s(input, "ICE");
const char *answer = hex_encode(x, strlen(input));
printf("01-05: %s\n", answer);
free(x);
free(answer);
}
static void challenge6() {
}
void set1() {
challenge1();
challenge2();
challenge3();
challenge4();
challenge5();
challenge6();
}