Solve challenge 7
This commit is contained in:
parent
02ca35a4ff
commit
8d9878964a
1 changed files with 20 additions and 8 deletions
|
@ -23,10 +23,14 @@ 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;
|
||||||
if (nread == 0) {
|
size_t nread = 0;
|
||||||
printf("Cannot read any character from file %s\n", filename);
|
while ((c = fgetc(fp)) != EOF) {
|
||||||
return EXIT_FAILURE;
|
if (c == '\n' || c == '\r') { // ignore newlines
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
input[nread] = c;
|
||||||
|
nread++;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char buf[BUF_SIZE] = {0};
|
unsigned char buf[BUF_SIZE] = {0};
|
||||||
|
@ -46,11 +50,19 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char output[BUF_SIZE] = {0};
|
unsigned char output[BUF_SIZE] = {0};
|
||||||
err = mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_DECRYPT, buf, output);
|
// Decrypt block by block
|
||||||
|
for (size_t i = 0; i <= BUF_SIZE - 16; i += 16) {
|
||||||
|
if (buf[i] == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
err = mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_DECRYPT, buf + i, output + i);
|
||||||
if (err) {
|
if (err) {
|
||||||
printf("Could not decode buffer\n");
|
printf("Could not decode buffer\n");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s", output);
|
||||||
|
|
||||||
if (fclose(fp)) {
|
if (fclose(fp)) {
|
||||||
printf("Error closing file %s\n", filename);
|
printf("Error closing file %s\n", filename);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue