Problem in fwrite and fread function [modified]
-
Hi, In my project simply encrypt data using XOR operator ,Then write encrypted data in file using fwrite function in MFC. Problem is occure only possible to decrypt pice of data from encrypted file. For example , File1:Input File size(6kb) FIle2:Encrypt File size(5kb) File3:Decrypt File size(1kb) coding: Encrypt: while(!feof(file1)) { int iout; fread(&iout,sizeof(iout),1,file1); iout^=1300; fwrite(&iout,sizeof(iout),1,file2); } Decrypt: while(!feof(file2)) { int iout; fread(&iout,sizeof(iout),1,file2); iout^=1300; fwrite(&iout,sizeof(iout),1,file3); } Note : NoProblem occur when if XOR with value of 10 while(!feof(file1)) { int iout; fread(&iout,sizeof(iout),1,file1); iout^=10; fwrite(&iout,sizeof(iout),1,file2); } Please replay me urgently -- modified at 8:40 Thursday 26th April, 2007
-
Hi, In my project simply encrypt data using XOR operator ,Then write encrypted data in file using fwrite function in MFC. Problem is occure only possible to decrypt pice of data from encrypted file. For example , File1:Input File size(6kb) FIle2:Encrypt File size(5kb) File3:Decrypt File size(1kb) coding: Encrypt: while(!feof(file1)) { int iout; fread(&iout,sizeof(iout),1,file1); iout^=1300; fwrite(&iout,sizeof(iout),1,file2); } Decrypt: while(!feof(file2)) { int iout; fread(&iout,sizeof(iout),1,file2); iout^=1300; fwrite(&iout,sizeof(iout),1,file3); } Note : NoProblem occur when if XOR with value of 10 while(!feof(file1)) { int iout; fread(&iout,sizeof(iout),1,file1); iout^=10; fwrite(&iout,sizeof(iout),1,file2); } Please replay me urgently -- modified at 8:40 Thursday 26th April, 2007
mohindar_kks wrote:
Problem is occure only possible to decrypt pice of data from encrypted file.
This is a bit unclear. What exactly is the problem? Are you concerned that
file1
(the original) andfile3
(the decrypted) are not the same? You'll likely see the same problem even if you omitted the XOR statement. Try:while (! feof(file1))
{
char iout;
if (fread(&iout, sizeof(iout), 1, file1) == 1)
{
iout ^= 1300;
fwrite(&iout, sizeof(iout), 1, file2);
}
}
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
mohindar_kks wrote:
Problem is occure only possible to decrypt pice of data from encrypted file.
This is a bit unclear. What exactly is the problem? Are you concerned that
file1
(the original) andfile3
(the decrypted) are not the same? You'll likely see the same problem even if you omitted the XOR statement. Try:while (! feof(file1))
{
char iout;
if (fread(&iout, sizeof(iout), 1, file1) == 1)
{
iout ^= 1300;
fwrite(&iout, sizeof(iout), 1, file2);
}
}
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Hi, In my project simply encrypt data using XOR operator ,Then write encrypted data in file using fwrite function in MFC. No problem in Decryption if successfully retrive data from encrypted file until EOF. But some time encrypted file automatically return EOF after postion of data retrive this data also decrypted successfully. For example , File1:Input File size(6kb) FIle2:Encrypt File size(5kb) File3:Decrypt File size(1kb) coding: Encrypt: while(!feof(Inputfile)) { int iout; fread(&iout,sizeof(iout),1,Inputfile); iout^=1300; fwrite(&iout,sizeof(iout),1,encryptfile); } Decrypt: while(!feof(encryptfile)) { int iout; fread(&iout,sizeof(iout),1,encryptfile); iout^=1300; fwrite(&iout,sizeof(iout),1,decryptfile); } Note : NoProblem occur when if XOR with value of 10 while(!feof(Inputfile)) { int iout; fread(&iout,sizeof(iout),1,Inputfile); iout^=10; fwrite(&iout,sizeof(iout),1,encryptfile); } Please replay me urgently
-
mohindar_kks wrote:
Problem is occure only possible to decrypt pice of data from encrypted file.
This is a bit unclear. What exactly is the problem? Are you concerned that
file1
(the original) andfile3
(the decrypted) are not the same? You'll likely see the same problem even if you omitted the XOR statement. Try:while (! feof(file1))
{
char iout;
if (fread(&iout, sizeof(iout), 1, file1) == 1)
{
iout ^= 1300;
fwrite(&iout, sizeof(iout), 1, file2);
}
}
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
char iout; ... iout ^= 1300; :~ :)
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
char iout; ... iout ^= 1300; :~ :)
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
Bad, yes. Part of the problem, no. His two major problems were not checking the return value from
fread()
, and trying to readbyte
s from the disk asint
s.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne