/* Encrypts stdin to stdout using BLOWFISH */ #include #include #include #include #include /* #include */ typedef enum { ENCRYPT, DECRYPT } operation; int main(int argc, char *argv[]) { MCRYPT td; int i; char *key; char password[20]; char block_buffer; char *IV; int keysize=16; /* 128 bits */ key=calloc(1, keysize); strcpy(password, "isa"); operation op = ENCRYPT ; if ( argc == 2 && strcmp(argv[1], "-d") == 0 ) { op = DECRYPT; } /* Generate the key using the password */ /* mhash_keygen( KEYGEN_MCRYPT, MHASH_MD5, key, keysize, NULL, 0, password, strlen(password)); */ memmove( key, password, strlen(password)); td = mcrypt_module_open("blowfish", NULL, "cfb", NULL); if (td==MCRYPT_FAILED) { return 1; } IV = malloc(mcrypt_enc_get_iv_size(td)); // Put random data in IV. Does not have to be secrete, can even be public. for (i=0; i< mcrypt_enc_get_iv_size(td); i++) { IV[i] = rand(); } i=mcrypt_generic_init( td, key, keysize, IV); if (i<0) { mcrypt_perror(i); return 1; } /* Encryption in CFB is performed in bytes */ while ( fread (&block_buffer, 1, 1, stdin) == 1 ) { if ( op == ENCRYPT ) { mcrypt_generic (td, &block_buffer, 1); } else { mdecrypt_generic (td, &block_buffer, 1); } fwrite ( &block_buffer, 1, 1, stdout); } /* Deinit the encryption thread, and unload the module */ mcrypt_generic_end(td); return 0; }