io_decrypt.c 1.43 KB
Newer Older
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

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>

#include "io_crypt.h"
#include "crypt_lfsr64.h"
#include "hash_password.h"


#define BLOCKSIZE (8 * 1024)


int main(int argc, char *argv[])
{
    if(argc != 4)
    {
        printf("Usage: '%s <infile> <outfile> <password>\n", argv[0]);
        return -1;
    }
    
    char *in_filename = argv[1];
    char *out_filename = argv[2];
    char *password = argv[3];

    /* hash the password */
    uint64_t key = 0;
    hash_password(password, &key);
    
    /* setup cipher with that hash */
    crypt_cipher_t *crypt_ctx;
    crypt_lfsr64_init(&crypt_ctx, key);
    
    /* open files */
    FILE *in_file = fopen(in_filename, "rb");
    if(!in_file)
    {
        printf("Could not open '%s'\n", in_filename);
40
        return -1;
41
42
43
44
45
46
    } 
    
    FILE *out_file = fopen(out_filename, "w");
    if(!in_file)
    {
        printf("Could not open '%s'\n", out_filename);
47
        return -1;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    }
    
    char *buffer = malloc(BLOCKSIZE);
    uint32_t file_offset = 0;
    
    while(!feof(in_file))
    {
        int ret = fread(buffer, 1, BLOCKSIZE, in_file);
        
        if(ret > 0)
        {
            crypt_ctx->decrypt(crypt_ctx, buffer, buffer, ret, file_offset);
            fwrite(buffer, 1, ret, out_file);
            file_offset += ret;
        }
    }
    
    fclose(in_file);
    fclose(out_file);
}