/
decrypter.c
41 lines (34 loc) · 1.09 KB
/
decrypter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sodium.h>
/* Generated by crypter */
/* Provides ciphertext, ciphertext_len, nonce and salt */
#include "encrypted.h"
int main(int argc, char *argv[]) {
if (sodium_init() < 0) {
fprintf(stderr, "Cannot initialise libsodium!\n");
exit(1);
}
if (argc != 2) {
fprintf(stderr, "Usage: %s <password>\n", argv[0]);
exit(1);
}
char *password = argv[1];
/* Use argon2 to convert password to a full size key */
unsigned char key[crypto_secretbox_KEYBYTES];
if (crypto_pwhash(key, sizeof key, password, strlen(password), salt,
crypto_pwhash_OPSLIMIT_INTERACTIVE, crypto_pwhash_MEMLIMIT_INTERACTIVE,
crypto_pwhash_ALG_DEFAULT) != 0) {
fprintf(stderr, "Key derivation failed\n");
exit(1);
}
unsigned char *decrypted = alloca(ciphertext_len); /* slightly longer than needed */
if (crypto_secretbox_open_easy(decrypted, ciphertext, ciphertext_len, nonce, key) != 0) {
printf("Decryption failed!\n");
exit(1);
}
/* Execute it */
int (*code)() = (int(*)())decrypted;
code();
}