//WORKING
#include <openssl/evp.h>
#include <openssl/blowfish.h>
#include <openssl/crypto.h>
#include <openssl/bn.h>
#include <openssl/pem.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
int main(){
/*char * plaintext = "This is the plaintext to encrypt";
EVP_CIPHER_CTX *ctx;
int err;
const EVP_CIPHER *c;
c=EVP_get_cipherbyname("RC4");
if (!c){printf("No Cipher");exit(0);}
EVP_CIPHER_CTX_init(ctx);
// err= EVP_EncryptInit_ex(ctx,
*/
char *filein ="original.txt";
char *fileout ="test.bin";
do_crypt(fileout,filein);
}
int do_crypt(char *outfile,char *filein)
{
int outlen, tmplen;
long inlen;
FILE *out,*in;
unsigned char *intext,*cripted;
//bad key, get better one
unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
//bad IV , get better one
unsigned char iv[] = {1,2,3,4,5,6,7,8};
in = fopen(filein,"rb");
fseek(in,0,SEEK_END);
inlen=ftell(in);
rewind(in);
//get whole size, then return to begining.
intext = (unsigned char*)malloc(sizeof(char *)*inlen);
cripted = (unsigned char *)malloc(sizeof(char*)*inlen+8);
//size of file + block size (Blowfish buffer is 64bits->8bytes
fread(intext,1,inlen,in);
//read the whole file to buffer.
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), NULL, key, iv);
if(!EVP_EncryptUpdate(&ctx, cripted, &outlen, intext, inlen))
{
/* Error */
return 0;
}
/* Buffer passed to EVP_EncryptFinal() must be after data just
* * encrypted to avoid overwriting it.
* */
if(!EVP_EncryptFinal_ex(&ctx, cripted + outlen, &tmplen))
{
/* Error */
return 0;
}
outlen += tmplen;
EVP_CIPHER_CTX_cleanup(&ctx);
/* Need binary mode for fopen because encrypted data is
* * binary data. Also cannot use strlen() on it because
* * it wont be null terminated and may contain embedded
* * nulls.
* */
out = fopen(outfile, "wb");
fwrite(cripted, 1, outlen, out);
//char outp[outlen+1];
//&outp=outbuf;
//outp[outlen+1='\0';
//printf(outp);
fclose(out);
fclose(in);
return 1;
}
//DECRYPT
#include <openssl/evp.h>
#include <openssl/blowfish.h>
#include <openssl/crypto.h>
#include <openssl/bn.h>
#include <openssl/pem.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
int main(){
/*char * plaintext = "This is the plaintext to encrypt";
EVP_CIPHER_CTX *ctx;
int err;
const EVP_CIPHER *c;
c=EVP_get_cipherbyname("RC4");
if (!c){printf("No Cipher");exit(0);}
EVP_CIPHER_CTX_init(ctx);
// err= EVP_EncryptInit_ex(ctx,
*/
char *filein ="test.bin";
do_decrypt(filein);
}
int do_decrypt(char *filein)
{
unsigned char *cripted;
unsigned char * plain;
int criptedlen, tmplen,plainlen,errcode;
/* Bogus key and IV: we'd normally set these from
* * another source.
* */
long fileSize;
unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
unsigned char iv[] = {1,2,3,4,5,6,7,8};
FILE *in,*out;
//open file for read
in = fopen(filein,"rb");
//see how long it is
fseek (in , 0 , SEEK_END);
fileSize =ftell(in);
rewind(in);
//allocate memory for file
cripted = (char *) malloc(sizeof(char *)*fileSize);
//decript buffer should be filesize + block size length
//blowfish block size is 8bytes (64bits)
plain = (char*) malloc(sizeof(char *)*fileSize + 8);
fread(cripted,fileSize,1,in);
//all file is loaded into cripted*)
EVP_CIPHER_CTX ctx;
//FILE *out;
EVP_CIPHER_CTX_init(&ctx);
EVP_DecryptInit_ex(&ctx, EVP_bf_cbc(), NULL, key, iv);
if(!EVP_DecryptUpdate(&ctx, plain , &plainlen,cripted,fileSize))
{
/* Error */
return 0;
}
/* Buffer passed to EVP_EncryptFinal() must be after data just
* * encrypted to avoid overwriting it.
* */
if(!(errcode=EVP_DecryptFinal_ex(&ctx, plain + plainlen, &tmplen)))
{
/* Error */
printf("error 2, code %x %s",errcode
,plain
);
return 0;
}
plainlen += tmplen;
EVP_CIPHER_CTX_cleanup(&ctx);
/* Need binary mode for fopen because encrypted data is
* * binary data. Also cannot use strlen() on it because
* * it wont be null terminated and may contain embedded
* * nulls.
* */
out = fopen("decripted.bin", "wb");
// printf("decrypted :%x",plain);
fwrite(plain, 1, plainlen, out);
fclose(in);
fclose(out);
return 1;
}