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'};
|
||||
size_t nread = fread(input, 1, BUF_SIZE, fp);
|
||||
if (nread == 0) {
|
||||
printf("Cannot read any character from file %s\n", filename);
|
||||
return EXIT_FAILURE;
|
||||
int c;
|
||||
size_t nread = 0;
|
||||
while ((c = fgetc(fp)) != EOF) {
|
||||
if (c == '\n' || c == '\r') { // ignore newlines
|
||||
continue;
|
||||
}
|
||||
input[nread] = c;
|
||||
nread++;
|
||||
}
|
||||
|
||||
unsigned char buf[BUF_SIZE] = {0};
|
||||
|
@ -46,12 +50,20 @@ int main(int argc, char *argv[]) {
|
|||
}
|
||||
|
||||
unsigned char output[BUF_SIZE] = {0};
|
||||
err = mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_DECRYPT, buf, output);
|
||||
if (err) {
|
||||
printf("Could not decode buffer\n");
|
||||
return EXIT_FAILURE;
|
||||
// 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) {
|
||||
printf("Could not decode buffer\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%s", output);
|
||||
|
||||
if (fclose(fp)) {
|
||||
printf("Error closing file %s\n", filename);
|
||||
return EXIT_FAILURE;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue