Hi, (I hope this is the correct list to be asking this, apologies if not)

I am trying to write an application which amongst other things uses the
blowfish implementation (blowfish.h) to transport files over a simple
server/client pair.

However, whilst some files are encrypted, transported, received and
decrypted correctly, some end up being corrupted, after the final decryption
stage. This leads me to think that the encryption routines are not being
called correctly (since I have also tried with equivalent DES library calls,
with the same 'intermittent corruption' results).

The relevant code is pasted below and further mirrored at
http://pastebin.com/d4d166d12.

So it starts with the function send_file (called by a connected client).
This splits the file into chunks. Each 1024 byte chunk is encrypted
separately and then sent. Each chunk is then received by the server in the
receive_file function, decrypted and saved to disc.

Any idea what the problem could be?

Cheers,
Ben.

inline void blowfish(unsigned char *data, int data_len, char* key, int enc)
{

 BF_KEY bfkey;
 BF_set_key(&bfkey, strlen(key)+1, (const unsigned char*)key);

 unsigned char ivec[8];
 memset(ivec, 0, 8);

 unsigned char *out = (unsigned char*) malloc(1024);
 bzero(out,1024);
 int num = 0;
 BF_cfb64_encrypt(data, out, data_len, &bfkey, ivec, &num, enc);

memcpy(data, out, data_len);
free(out);
}

void
MyFrame::encryptHelper(const char* orig, int inlength)
{
char *pb=(char*)(std::string((passInput->GetValue()).mb_str()).c_str());
blowfish((unsigned char*)orig, inlength, pb, BF_ENCRYPT);
}
void
MyFrame::decryptHelper(const char* orig, int inlength)
{
char *pb=(char*)(std::string((passInput->GetValue()).mb_str()).c_str());
blowfish((unsigned char*)orig, inlength, pb, BF_DECRYPT);
}

int
MyFrame::send_file(int fd)
{

// Note file size and name of file which the receiver is expecting have been
sent
// in a function prior to the call of this one
 char rec[10];
struct stat stat_buf;
fstat (fd, &stat_buf);
int size=stat_buf.st_size;
 while(size > 0)
{
char buffer[1030];
bzero(buffer,1030);
bzero(rec,10);
int n;
if(size>=1024)
{
n=read(fd, buffer, 1024);
 // encrypt is necessary
if(encButtonOn->GetValue()) encryptHelper(buffer,1024);
 // Send a chunk of data
n=send(sockFile_, buffer, 1024, 0 );
 // Wait for an acknowledgement
n = recv(sockFile_, rec, 10, 0 );
}
else // reamining file bytes
{
n=read(fd, buffer, size);
if(encButtonOn->GetValue()) encryptHelper(buffer,size);
buffer[size]='\0';
n=send(sockFile_,buffer, size, 0 );
n=recv(sockFile_, rec, 10, 0 );
}
 }
 // Send a 'completion' string
int n = send(sockFile_, "COMP",strlen("COMP"), 0 );

// Receive an acknowledgemnt
char buf[10];
bzero(buf,10);
n = recv(sockFile_, buf, 10, 0 );

return(0);
}

int
MyFrame::receive_file()
{
 // receive file size and send ack
char sizeBuffer[50];
bzero(sizeBuffer,50);
int n;
n=read(sockFile_, sizeBuffer, 50);
n=send(sockFile_,"OK", strlen("OK"), 0 );

int size = atoi(sizeBuffer);
 //std::cout<<size<<std::endl;
 // receive file name and send ack
char saveName[256];
bzero(saveName,256);
n=read(sockFile_, saveName, 256);

n=send(sockFile_,"OK",strlen("OK"), 0 );
 //std::cout<<saveName_<<std::endl;
 // start file writing process to local disk
// decrypt first if necessary
if(encButtonOn->GetValue()) decryptHelper(saveName,strlen(saveName));
ofstream outFile(saveName,ios::out|ios::binary|ios::app);

while(size > 0)
{
// buffer for storing incoming data
char buf[1030];
bzero(buf,1030);
if(size>=1024)
{
 // receive chunk of data
n=recv(sockFile_, buf, 1024, 0 );
 // decrypt if necessary
if(encButtonOn->GetValue()) decryptHelper(buf,1024);
 // write chunk of data to disk
outFile.write(buf,1024);
 // send acknowledgement
n = send(sockFile_, "OK", strlen("OK"), 0 );
 }
else
{
n=recv(sockFile_, buf, size, 0 );
if(encButtonOn->GetValue()) decryptHelper(buf,size);
buf[size]='\0';
outFile.write(buf,size);
n = send(sockFile_, "OK", strlen("OK"), 0 );
}
 size -= 1024;
 }
 outFile.close();
 // Receive 'COMP' and send acknowledgement
// ---------------------------------------
char buf[10];
bzero(buf,10);
n = recv(sockFile_, buf, 10, 0 );
n = send(sockFile_,  "OK", strlen("OK"), 0 );
std::cout<<"File received..."<<std::endl;
 return(0);
}

Reply via email to