Hi, I am newbie to the cryptography. I am using crypto library of openssl 0.9.6b version on ReHat Linux 7.2. I wanted to encrypt strings.
the code is attached. If I give strings like "aaa" for encryption it is giving some junk characters. What could be the problem? regards, Sambaiah K
#include <stdio.h> #include <time.h> #include <string.h> #include <stdlib.h> #include "blowfish.h" #define SIZE 8 static unsigned char cbc_iv[8] = "akaa2345"; static unsigned char veckey[8] = "This is "; void blowfish_enc(unsigned char *iinput, unsigned char *ooutput, long length, BF_KEY *bfkey, int *num); void blowfish_dec(unsigned char *iinput, unsigned char *ooutput, long length, BF_KEY *bfkey,int *num); int main(int argc, char *argv[]) { FILE *fp; unsigned char *con_in, *con_out, *cbc_out; int n, t; long i = 64; BF_KEY key; BF_set_key(&key, sizeof(veckey), veckey); //printf("%d\n", sizeof(veckey)); con_in = (unsigned char *)malloc(sizeof(char) * i); memset(con_in, 0, i); //strcpy(con_in, "this is to test more than two words. ok !!"); con_out = (unsigned char *)malloc(sizeof(char)*i); memset(con_out, 0, i); cbc_out = (unsigned char *)malloc(sizeof(char)*i); memset(cbc_out, 0, i); if ( (fp = (FILE *)fopen("blowfish.dat", "rb")) != NULL ) { while ( !feof(fp) ) { memset(con_out, 0, i); memset(cbc_out, 0, i); if ( fgets(con_out, i, fp) != NULL ) { //if ( strstr(con_out, "\n") == NULL ) //{ n = 0; t = (int)strlen(con_out)+1; blowfish_dec(con_out, cbc_out, t, &key, &n); //BF_cfb64_encrypt(con_out, cbc_out, i, &key, iv, &n, BF_DECRYPT); printf("FEc(%03d,%03d): %s\n", (int)strlen(con_out), n, con_out); printf("FDc(%03d): %s\n", (int)strlen(cbc_out), cbc_out); //} } if ( feof(fp) ) break; } fclose(fp); } else { printf("Error Opening file blowfish.dat\n"); } printf("Enter String : "); gets(con_in); printf("\nCurrent String :\n"); n = 0; t = (int)strlen(con_in)+1; blowfish_enc(con_in, con_out, t, &key, &n); //BF_cfb64_encrypt(con_in, con_out, i, &key, iv, &n, BF_ENCRYPT); printf("Ori(%03d): %s\n", (int)strlen(con_in), con_in); printf("Enc(%03d,%03d): %s\n", (int)strlen(con_out), n, con_out); n=0; blowfish_dec(con_out, cbc_out, t, &key, &n); //BF_cfb64_encrypt(con_out, cbc_out, i, &key, iv, &n, BF_DECRYPT); printf("Dec(%03d,%03d): %s\n", (int)strlen(cbc_out), n, cbc_out); if ( (fp = (FILE *)fopen("blowfish.dat", "ab")) != NULL ) { fprintf(fp,"%s\n", con_out); fclose(fp); } else { printf("Error Opening file blowfish.dat\n"); } } void blowfish_enc(unsigned char *iinput, unsigned char *ooutput, long length, BF_KEY *bfkey, int *num) { unsigned char *ivec; ivec = (unsigned char *)malloc(sizeof(char)*SIZE); memcpy(ivec, cbc_iv, SIZE); BF_cfb64_encrypt(iinput, ooutput, length, bfkey, ivec, num, BF_ENCRYPT); free(ivec); } void blowfish_dec(unsigned char *iinput, unsigned char *ooutput, long length, BF_KEY *bfkey, int *num) { unsigned char *ivec; ivec = (unsigned char *)malloc(sizeof(char)*SIZE); memcpy(ivec, cbc_iv, SIZE); BF_cfb64_encrypt(iinput, ooutput, length, bfkey, ivec, num, BF_DECRYPT); free(ivec); }