122 lines
3.1 KiB
C
122 lines
3.1 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include <openssl/evp.h>
|
|
#include <openssl/err.h>
|
|
|
|
|
|
#define ENCRYPT 1
|
|
#define DECRYPT 0
|
|
#define MAX_BUFFER 1024
|
|
|
|
void handle_errors(){
|
|
ERR_print_errors_fp(stderr);
|
|
abort();
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
|
|
// int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, const unsigned char *key, const unsigned char *iv, int enc);
|
|
// int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, const unsigned char *in, int inl);
|
|
// int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
|
|
|
|
|
|
|
|
|
|
if(argc != 3){
|
|
fprintf(stderr,"Invalid parameters. Usage: %s file_in ALGO-TO-USE\n",argv[0]);
|
|
exit(1);
|
|
}
|
|
|
|
|
|
FILE *f_in;
|
|
if((f_in = fopen(argv[1],"r")) == NULL) {
|
|
fprintf(stderr,"Couldn't open the input file, try again\n");
|
|
abort();
|
|
}
|
|
/*
|
|
if(strlen(argv[2])!=32){
|
|
fprintf(stderr,"Wrong key length\n");
|
|
abort();
|
|
}
|
|
if(strlen(argv[3])!=32){
|
|
fprintf(stderr,"Wrong IV length\n");
|
|
abort();
|
|
}
|
|
*/
|
|
/*FILE *f_out;
|
|
if((f_out = fopen(argv[2],"wb")) == NULL) {
|
|
fprintf(stderr,"Couldn't open the output file, try again\n");
|
|
abort();
|
|
}*/
|
|
unsigned char key[] = "0123456789ABCDEF";
|
|
unsigned char iv[] = "0123456789ABCDEF";
|
|
/*unsigned char key[strlen(argv[2])/2];
|
|
for(int i = 0; i < strlen(argv[2])/2;i++){
|
|
sscanf(&argv[2][2*i],"%2hhx", &key[i]);
|
|
}
|
|
|
|
unsigned char iv[strlen(argv[3])/2];
|
|
for(int i = 0; i < strlen(argv[3])/2;i++){
|
|
sscanf(&argv[3][2*i],"%2hhx", &iv[i]);
|
|
}
|
|
*/
|
|
/* Load the human readable error strings for libcrypto */
|
|
ERR_load_crypto_strings();
|
|
/* Load all digest and cipher algorithms */
|
|
OpenSSL_add_all_algorithms(); // deprecated since version 1.1.1
|
|
|
|
|
|
|
|
// pedantic mode: check NULL
|
|
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
|
|
|
|
if(!EVP_CipherInit(ctx,EVP_get_cipherbyname(argv[2]), key, iv, DECRYPT))
|
|
handle_errors();
|
|
EVP_CIPHER_CTX_set_padding(ctx,0);
|
|
int length;
|
|
unsigned char ciphertext[MAX_BUFFER+16];
|
|
|
|
int n_read;
|
|
unsigned char buffer[MAX_BUFFER];
|
|
|
|
while((n_read = fread(buffer,1,MAX_BUFFER,f_in)) > 0){
|
|
//printf("n_Read=%d-",n_read);
|
|
if(!EVP_CipherUpdate(ctx,ciphertext,&length,buffer,n_read))
|
|
handle_errors();
|
|
//printf("length=%d\n",length);
|
|
|
|
/*if(fwrite(ciphertext, 1, length,f_out) < length){
|
|
fprintf(stderr,"Error writing the output file\n");
|
|
abort();
|
|
}*/
|
|
}
|
|
|
|
if(!EVP_CipherFinal_ex(ctx,ciphertext,&length))
|
|
handle_errors();
|
|
|
|
//printf("lenght=%d\n",length);
|
|
printf("ALGORITHM:%s, Output: %s\n", argv[2], ciphertext);
|
|
/*if(fwrite(ciphertext,1, length, f_out) < length){
|
|
fprintf(stderr,"Error writing in the output file\n");
|
|
abort();
|
|
}
|
|
*/
|
|
EVP_CIPHER_CTX_free(ctx);
|
|
|
|
fclose(f_in);
|
|
// fclose(f_out);
|
|
|
|
//printf("File decrypted!\n");
|
|
|
|
|
|
// completely free all the cipher data
|
|
CRYPTO_cleanup_all_ex_data();
|
|
/* Remove error strings */
|
|
ERR_free_strings();
|
|
|
|
return 0;
|
|
}
|
|
|