0

我正在尝试编写一个应用程序,其中使用 openssl blowfish 实现(blowfish.h)通过简单的服务器/客户端对传输文件。

然而,虽然一些文件被正确加密、传输、接收和解密,但在最终解密阶段之后,一些文件最终被破坏。这使我认为加密例程没有被正确调用(因为我也尝试过使用等效的 DES 库调用,并具有相同的“间歇性损坏”结果)。

相关代码贴在下面。

基本上,它以函数 send_file(由连接的客户端调用)开始。这会将文件拆分为块。每个 1024 字节的块单独加密然后发送。然后服务器在 receive_file 函数中接收每个块,解密并保存到磁盘。

知道问题可能是什么吗?(请注意,如有必要,我将添加整个应用程序的代码)。

干杯,本。

void encryptHelper(const char*,int);
void decryptHelper(const char*,int);

inline void blowfish(unsigned char *data, int data_len, char* key, int enc)
{
    //  hash the key first! 
    unsigned char obuf[20];
    bzero(obuf,20);
    SHA1((const unsigned char*)key, strlen(key), obuf);

    BF_KEY bfkey;
    int keySize = strlen(key);
    BF_set_key(&bfkey, 16, (const unsigned char*)obuf);

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

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

    data=out;

    //memcpy(data, out, data_len);
    //free(out);
}
void MyFrame::encryptHelper(char* orig, int inlength)
{
char *pb=(char*)(std::string((passInput->GetValue()).mb_str()).c_str());
blowfish((unsigned char*)orig, inlength, pb, DES_ENCRYPT);
}

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


int MyFrame::send_file(int fd)
{

char rec[10];
struct stat stat_buf;
fstat (fd, &stat_buf);  
int size=stat_buf.st_size;

int remSize=size;

int value=0;

while(size > 0)
{
    char buffer[1030];
    bzero(buffer,1030);
    bzero(rec,10);
    int n;
    if(size>=1024)
    {
        value+=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
    {
        value+=size;
        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 );
    }

    MyFooEvent event( 0, 992 );
    double firstBit = (double)value/remSize;
    firstBit=firstBit*100.0;
    event.adouble=firstBit;     
    wxPostEvent (this, event);  


    size -= 1024;

}

// Send a completion string
int n = send(sockFile_, "COMP",strlen("COMP"), 0 );
char buf[10];
bzero(buf,10);
// Receive an acknowledgemnt
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;
//read(This->sockpw,buffer,bufferSize);
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
std::cout<<arraySize(saveName)<<std::endl;
std::cout<<strlen(saveName)<<std::endl;
if(encButtonOn->GetValue()) decryptHelper(saveName,strlen(saveName));
ofstream outFile(saveName,ios::out|ios::binary|ios::app);

// vars for status gauge
int remSize=size;
int value=0;

while(size > 0)
{       
    // buffer for storing incoming data
    char buf[1030];
    bzero(buf,1030);
    if(size>=1024)
    {

        value+=1024; // for status gauge

        // 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
    {
        value+=size;
        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 );
    }

    // Update status gauge
    MyFooEvent event( 0, 992 );
    double firstBit = (double)value/remSize;
    firstBit=firstBit*100.0;
    event.adouble=firstBit;
    wxPostEvent (this, event);  

    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;

// Display image event
MyFooEvent eventF( 0, 995 );
eventF.SetText(wxString(saveName, wxConvUTF8));
wxPostEvent (this, eventF);     

return(0);
 }
4

4 回答 4

2

我假设:

char *pb=(char*)(std::string((passInput->GetValue()).mb_str()).c_str());
blowfish((unsigned char*)orig, inlength, pb, DES_DECRYPT);

解密成pb,实际上是一个临时字符串的缓冲区。您根本不能像这样使用 std::string 。您必须使用这么多 casr 来执行此操作的事实应该是一个警告 - 好的 C 和 C++ 代码通常根本不需要强制转换。基本上,你需要重新考虑你在做什么。

于 2010-01-03T18:41:31.953 回答
0

不确定,可能是某处的缓冲区溢出或内存损坏...

您可以使用valgrind来检测问题,或者尝试简化转换/...

于 2010-01-03T18:42:09.300 回答
0

通过询问其他一些问题修复了一些错误后,我已经使文件加密过程正常工作,但前提是客户端和服务器都在同一台 localhost 机器上。当它们驻留在不同的机器上时,文件仍然会损坏。我认为这是由于从线程调用 send_file 和接收文件的事实如下:

void 
*MyFrame::send_fileT(void* tid)
{
accessHelper* ah = static_cast<accessHelper*>(tid);
MyFrame* This = ah->This;
This->send_file(fileSendID);
pthread_exit(NULL);
} 

void 
*MyFrame::receive_fileT(void* tid)
{
accessHelper* ah = static_cast<accessHelper*>(tid);
MyFrame* This = ah->This;
This->receive_file();
pthread_exit(NULL);
}

....然后receive_file 或send_file 函数正在调用blowfish 函数来执行加密。现在,如果在 pthread 中调用一个函数(即 send_file 和 receive_file),那么如果该函数调用另一个函数(即 encryptHelper --blowfish),调用函数是否可能不会“正确”等待被调用函数完成正确吗?

于 2010-01-07T17:55:59.623 回答
0

固定的:

 n=read(fd, buffer, 2048);
 if(enc)encryptHelper(buffer,n);
 n=send(sockFile_, buffer, n, 0 );
 [called in a loop]

问题是,不能确保加密缓冲区的所有 n 个字节都被传输。因此,只有一些加密字节被发送,导致接收端解密不一致。

于 2010-01-12T09:51:22.143 回答