summaryrefslogtreecommitdiff
path: root/set1/task7.c
diff options
context:
space:
mode:
Diffstat (limited to 'set1/task7.c')
-rw-r--r--set1/task7.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/set1/task7.c b/set1/task7.c
new file mode 100644
index 0000000..d71da7a
--- /dev/null
+++ b/set1/task7.c
@@ -0,0 +1,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;
+}
+