summaryrefslogtreecommitdiff
path: root/set1/task7.c
blob: d71da7a9099e0ce626f58575146dc4fa239d68bb (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/err.h>

int handleErrors() 
{
	ERR_print_errors_fp(stderr);
	abort();
}

int do_crypt(FILE *in, FILE *out, int do_encrypt)
{
	unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
	int inlen, outlen;
	EVP_CIPHER_CTX *ctx;

	unsigned char key[] = "YELLOW SUBMARINE";

	ctx = EVP_CIPHER_CTX_new();

	EVP_CipherInit_ex(ctx, EVP_aes_128_ecb(), NULL, NULL, NULL, do_encrypt);

	EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, do_encrypt);

	for(;;) {
		inlen = fread(inbuf, 1, 1024, in);
		if (inlen <= 0)
			break;

		if(!EVP_CipherUpdate(ctx, outbuf, &outlen, inbuf, inlen)) {
			EVP_CIPHER_CTX_free(ctx);
			return 0;
		}

		fwrite(outbuf, 1, outlen, out);
	}

	if(!EVP_CipherFinal_ex(ctx, outbuf, &outlen)) {
		EVP_CIPHER_CTX_free(ctx);
		return 0;
	}

	fwrite(outbuf, 1, outlen, out);

	EVP_CIPHER_CTX_free(ctx);
	return 1;
}

int main(int argc, char **argv)
{
	ERR_load_crypto_strings();

	OpenSSL_add_all_algorithms();

	OPENSSL_config(NULL);


	FILE *in;
	FILE *out;

	in = fopen("7unbased.txt", "r");
	out = fopen ("7cleartext.txt", "w");

	if ( in == NULL) {
		printf("error in\n");
		return 1;
	}

	if (out == NULL) {
		printf("error out\n");
		return 1;
	}

	do_crypt(in, out, 0);


	EVP_cleanup();

	CRYPTO_cleanup_all_ex_data();

	ERR_free_strings();

	return 0;
}