Fix base64 decoding and decipher single-char xor
This commit is contained in:
parent
2f206e91db
commit
87a1547478
2 changed files with 16 additions and 9 deletions
|
@ -40,7 +40,15 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
char input[BUF_SIZE] = {'\0'};
|
char input[BUF_SIZE] = {'\0'};
|
||||||
size_t nread = fread(input, 1, BUF_SIZE, fp);
|
int c;
|
||||||
|
size_t nread = 0;
|
||||||
|
while ((c = fgetc(fp)) != EOF) {
|
||||||
|
if (c == '\n' || c == '\r') { // ignore newlines
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
input[nread] = c;
|
||||||
|
nread++;
|
||||||
|
}
|
||||||
if (nread == 0) {
|
if (nread == 0) {
|
||||||
printf("Cannot read any character from file %s\n", filename);
|
printf("Cannot read any character from file %s\n", filename);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
@ -82,11 +90,14 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
char key[keysize];
|
char key[keysize];
|
||||||
for (size_t j = 0; j < keysize; ++j) {
|
for (size_t j = 0; j < keysize; ++j) {
|
||||||
printf("Guessing key character %2zu: ", j);
|
|
||||||
key[j] = best_single_char_xor_key(blocks_count, blocks[j]);
|
key[j] = best_single_char_xor_key(blocks_count, blocks[j]);
|
||||||
printf("%c\n", key[j]);
|
|
||||||
}
|
}
|
||||||
//printf("key: %s\n", key);
|
printf("Key: %s\n\n", key);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < len; ++i) {
|
||||||
|
printf("%c", buf[i] ^ key[i % keysize]);
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
unsigned char hex_to_byte(const char c) {
|
unsigned char hex_to_byte(const char c) {
|
||||||
|
@ -82,6 +81,7 @@ double frequency_score(size_t len, const char buf[static len]) {
|
||||||
0.0673, 0.0894, 0.0268, 0.0106, 0.0183, 0.0019, 0.0172, 0.0011,
|
0.0673, 0.0894, 0.0268, 0.0106, 0.0183, 0.0019, 0.0172, 0.0011,
|
||||||
// space
|
// space
|
||||||
0.19182};
|
0.19182};
|
||||||
|
|
||||||
// static const double english_freqs[27] = {
|
// static const double english_freqs[27] = {
|
||||||
// // A-Z
|
// // A-Z
|
||||||
// 0.08167, 0.01492, 0.02782, 0.04253, 0.12702, 0.02228, 0.02015, 0.06094,
|
// 0.08167, 0.01492, 0.02782, 0.04253, 0.12702, 0.02228, 0.02015, 0.06094,
|
||||||
|
@ -137,14 +137,10 @@ char best_single_char_xor_key(size_t len, unsigned char buf[static len]) {
|
||||||
cur[i] = buf[i] ^ c;
|
cur[i] = buf[i] ^ c;
|
||||||
}
|
}
|
||||||
double score = frequency_score(len, cur);
|
double score = frequency_score(len, cur);
|
||||||
//printf("current character: %c, current score: %f\n", c, score);
|
|
||||||
if (score < min_score) {
|
if (score < min_score) {
|
||||||
min_score = score;
|
min_score = score;
|
||||||
key = c;
|
key = c;
|
||||||
}
|
}
|
||||||
if (isfinite(min_score)) {
|
|
||||||
printf("current best character: %c, current best score: %f\n", key, min_score);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return key;
|
return key;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue