Last active
April 4, 2022 09:20
-
-
Save tirzasrwn/5b468de5d9ee227247d8efb57377ed4b to your computer and use it in GitHub Desktop.
Encrypt decrypt file using openssl library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <openssl/evp.h> | |
#include <openssl/aes.h> | |
const unsigned char g_cKey[] = "thiskeyisverybad"; | |
const unsigned char g_iVec[] = "dontusethisinput"; | |
void encrypt(FILE *ifp, FILE *ofp) | |
{ | |
// Get file size. | |
fseek(ifp, 0L, SEEK_END); | |
int fsize = ftell(ifp); | |
// Set back to normal. | |
fseek(ifp, 0L, SEEK_SET); | |
int outLen1 = 0; | |
int outLen2 = 0; | |
unsigned char *indata = malloc(fsize); | |
unsigned char *outdata = malloc(fsize * 2); | |
// Read file. | |
fread(indata, sizeof(char), fsize, ifp); // Read entire file. | |
// Set up encryption. | |
EVP_CIPHER_CTX *ctx; | |
ctx = EVP_CIPHER_CTX_new(); | |
EVP_EncryptInit(ctx, EVP_aes_128_cbc(), g_cKey, g_iVec); | |
EVP_EncryptUpdate(ctx, outdata, &outLen1, indata, fsize); | |
EVP_EncryptFinal(ctx, outdata + outLen1, &outLen2); | |
fwrite(outdata, sizeof(char), outLen1 + outLen2, ofp); | |
EVP_CIPHER_CTX_free(ctx); | |
} | |
void decrypt(FILE *ifp, FILE *ofp) | |
{ | |
// Get file size. | |
fseek(ifp, 0L, SEEK_END); | |
int fsize = ftell(ifp); | |
// Set back to normal. | |
fseek(ifp, 0L, SEEK_SET); | |
int outLen1 = 0; | |
int outLen2 = 0; | |
unsigned char *indata = malloc(fsize); | |
unsigned char *outdata = malloc(fsize); | |
// Read file. | |
fread(indata, sizeof(char), fsize, ifp); // Read entire file. | |
// Setup decryption. | |
EVP_CIPHER_CTX *ctx; | |
ctx = EVP_CIPHER_CTX_new(); | |
EVP_DecryptInit(ctx, EVP_aes_128_cbc(), g_cKey, g_iVec); | |
EVP_DecryptUpdate(ctx, outdata, &outLen1, indata, fsize); | |
EVP_DecryptFinal(ctx, outdata + outLen1, &outLen2); | |
fwrite(outdata, sizeof(char), outLen1 + outLen2, ofp); | |
EVP_CIPHER_CTX_free(ctx); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
FILE *fIN, *fOUT; | |
// Encrypt file now. | |
fIN = fopen("plain", "rb"); // File to be encrypted. Plain text. | |
fOUT = fopen("cyphertext", "wb"); // File to be written. Cipher text. | |
encrypt(fIN, fOUT); | |
fclose(fIN); | |
fclose(fOUT); | |
// Decrypt file now. | |
fIN = fopen("cyphertext", "rb"); // File to be open. Cipher text. | |
fOUT = fopen("decrypted", "wb"); // File to be written. Comeback to plain text. | |
decrypt(fIN, fOUT); | |
fclose(fIN); | |
fclose(fOUT); | |
return 0; | |
} | |
// Compile: | |
// gcc main.c -o main -lcrypto | |
// Need libssl-dev library. | |
// Check with hexdump: | |
// hexdump -n 100 -C plain | |
// hexdump -n 100 -C cyphertext | |
// hexdump -n 100 -C decrypted |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment