gpt4 book ai didi

c++ - blowfish.h 在简单的客户端/服务器应用程序中的用法

转载 作者:行者123 更新时间:2023-11-28 03:58:45 25 4
gpt4 key购买 nike

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

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

相关代码贴在下面。

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

知道问题出在哪里吗? (注意,如果有必要,我将添加整个应用程序的代码)。

干杯,本。

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);
}

最佳答案

我假设:

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

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

关于c++ - blowfish.h 在简单的客户端/服务器应用程序中的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1995830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com