请教,VC读取后缀名为.bin的二进制文件的问题
1个回答
推荐于2016-11-09
展开全部
首先需要完善EncTable里的字节内容。(我估计这个EncTable里的字节应该是256个不重复的字节,要不密文就还原不了了。)
const unsigned char EncTable[256] =
{
0x76,0x3F,0xD9,0xE4,0xBB,0x92,0xDF,0xF4,0xF2,0xAF,0x97,0x34,0xE7,0xA8,0x20,0xF3,
0xC3,0xBE,0xA1,0xB8,0x41,0x38,0x8B,0x59,0x26,0x94,0x74,0x96,0xA5,0xA6,0xC7,0xEA,
0x16,0x3C,0x4E,0x88,0xD3,0x19,0x75,0x9F,0x03,0x15,0x54,0x42,0x8A,0xD7,0xCC,0x5A,
0xD8,0xBC,0x43,0x00,0x5D,0xA2,0xF5,0xFA,0x40,0xC8,0x65,0xC2,0xCF,0x72,0xA4,0xFC,
0xFB,0x77,0x33,0xF8,0x1B,0xF9,0xB3,0x80,0xF7,0x3D,0x45,0x9C,0xAB,0xE8,0xE1,0x58,
0x3A,0xCE,0x2C
};
const int N=1024; //我每次从文件中读1024个字节进行转换,你可以根据需要适当调节。
// strHexPathName是你要转换的Hex文件路径字符串(包括路径,文件名和扩展名)
// strBinPathName是转换后的Bin文件路径字符串(包括路径,文件名和扩展名)
void Convert(LPCTSTR strHexPathName,LPCTSTR strBinPathName)
{
CFile fileHex;
CFile fileBin;
if(!fileHex.Open(strHexPathName,CFile::modeRead|CFile::typeBinary)){
return;
}
if(!fileBin.Open(strBinPathName,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary)){
fileHex.Close();
return;
}
unsigned char pBufRead[N]={0};
unsigned char pBufWrite[N]={0};
int nReadLen=0;
do{
nReadLen=fileHex.Read(pBufRead,N);
if(nReadLen>0){
Encrypt(pBufRead,pBufWrite,nReadLen);
}
fileBin.Write(pBufWrite,nReadLen);
}while(nReadLen>0);
fileBin.Close();
fileHex.Close();
}
void Encrypt(unsigned char* pInBuf,unsigned char* pOutBuf,int nLength)
{
for(int i=0;i<nLength;i++){
pOutBuf[i]=EncTable[pInBuf[i]];
}
}
const unsigned char EncTable[256] =
{
0x76,0x3F,0xD9,0xE4,0xBB,0x92,0xDF,0xF4,0xF2,0xAF,0x97,0x34,0xE7,0xA8,0x20,0xF3,
0xC3,0xBE,0xA1,0xB8,0x41,0x38,0x8B,0x59,0x26,0x94,0x74,0x96,0xA5,0xA6,0xC7,0xEA,
0x16,0x3C,0x4E,0x88,0xD3,0x19,0x75,0x9F,0x03,0x15,0x54,0x42,0x8A,0xD7,0xCC,0x5A,
0xD8,0xBC,0x43,0x00,0x5D,0xA2,0xF5,0xFA,0x40,0xC8,0x65,0xC2,0xCF,0x72,0xA4,0xFC,
0xFB,0x77,0x33,0xF8,0x1B,0xF9,0xB3,0x80,0xF7,0x3D,0x45,0x9C,0xAB,0xE8,0xE1,0x58,
0x3A,0xCE,0x2C
};
const int N=1024; //我每次从文件中读1024个字节进行转换,你可以根据需要适当调节。
// strHexPathName是你要转换的Hex文件路径字符串(包括路径,文件名和扩展名)
// strBinPathName是转换后的Bin文件路径字符串(包括路径,文件名和扩展名)
void Convert(LPCTSTR strHexPathName,LPCTSTR strBinPathName)
{
CFile fileHex;
CFile fileBin;
if(!fileHex.Open(strHexPathName,CFile::modeRead|CFile::typeBinary)){
return;
}
if(!fileBin.Open(strBinPathName,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary)){
fileHex.Close();
return;
}
unsigned char pBufRead[N]={0};
unsigned char pBufWrite[N]={0};
int nReadLen=0;
do{
nReadLen=fileHex.Read(pBufRead,N);
if(nReadLen>0){
Encrypt(pBufRead,pBufWrite,nReadLen);
}
fileBin.Write(pBufWrite,nReadLen);
}while(nReadLen>0);
fileBin.Close();
fileHex.Close();
}
void Encrypt(unsigned char* pInBuf,unsigned char* pOutBuf,int nLength)
{
for(int i=0;i<nLength;i++){
pOutBuf[i]=EncTable[pInBuf[i]];
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询