[英]Steganography : Encoded audio and video file not being played, getting corrupted. What is the issue
我制作了一个隐写术程序来加密/解密图像音频和视频下的某些文本。
我将图像用作bmp(54字节标题)文件,将音频用作wav(44字节标题)文件,将视频用作avi(56字节标题)文件格式。
当我尝试对所有这些文件下的文本进行加密时,它将成功加密,并且也将正确解密。
但这在音频和视频方面造成了问题,即在加密结果后无法播放这些文件。
可能是什么问题。 我正在使用Turbo C ++编译器。 我知道它是过时的编译器,但我只需要这样做。
这是我要加密的代码 。
int Binary_encode(char *txtSourceFileName, char *binarySourceFileName, char *binaryTargetFileName,const short headerSize)
{
long BinarySourceSize=0,TextSourceSize=0;
char *Buffer;
long BlockSize=10240, i=0;
ifstream ReadTxt, ReadBinary; //reads
ReadTxt.open(txtSourceFileName,ios::binary|ios::in);//file name, mode of open, here input mode i.e. read only
if(!ReadTxt)
{
cprintf("\nFile can not be opened.");
return 0;
}
ReadBinary.open(binarySourceFileName,ios::binary|ios::in);//file name, mode of open, here input mode i.e. read only
if(!ReadBinary)
{
ReadTxt.close();//closing opened file
cprintf("\nFile can not be opened.");
return 0;
}
ReadBinary.seekg(0,ios::end);//setting pointer to a file at the end of file.
ReadTxt.seekg(0,ios::end);
BinarySourceSize=(long )ReadBinary.tellg(); //returns the position of pointer
TextSourceSize=(long )ReadTxt.tellg(); //returns the position of pointer
ReadBinary.seekg(0,ios::beg); //sets the pointer to the begining of file
ReadTxt.seekg(0,ios::beg); //sets the pointer to the begining of file
if(BinarySourceSize<TextSourceSize*50) //Minimum size of an image should be 50 times the size of file to be encrypted
{
cout<<"\n\n";
cprintf("Binary File size should be bigger than text file size.");
ReadBinary.close();
ReadTxt.close();
return 0;
}
cout<<"\n";
cprintf("\n\nSize of Source Image/Audio File is : ");
cout<<(float)BinarySourceSize/1024;
cprintf("KB");
cout<<"\n";
cprintf("Size of Text File is ");
cout<<TextSourceSize;
cprintf(" Bytes");
cout<<"\n";
getch();
//write header to file without changing else file will not open
//bmp image's header size is 53 bytes
Buffer=new char[headerSize];
ofstream WriteBinary; // writes to file
WriteBinary.open(binaryTargetFileName,ios::binary|ios::out|ios::trunc);//file will be created or truncated if already exists
ReadBinary.read(Buffer,headerSize);//reads no of bytes and stores them into mem, size contains no of bytes in a file
WriteBinary.write(Buffer,headerSize);//writes header to 2nd image
delete[] Buffer;//deallocate memory
/*
Buffer = new char[sizeof(long)];
Buffer = (char *)(&TextSourceSize);
cout<<Buffer;
*/
WriteBinary.write((char *)(&TextSourceSize),sizeof(long));
//writes no of byte to be written in image immediate after header ends
//to decrypt file
if(!(Buffer=new char[TextSourceSize]))
{
cprintf("Enough Memory could not be assigned.");
return 0;
}
ReadTxt.read(Buffer,TextSourceSize);//read all data from text file
ReadTxt.close();//file no more needed
WriteBinary.write(Buffer,TextSourceSize);//writes all text file data into image
delete[] Buffer;//deallocate memory
//replace Tsize+1 below with Tsize and run the program to see the change
//this is due to the reason that 50-54 byte no are of colors which we will be changing
ReadBinary.seekg(TextSourceSize+1,ios::cur);//move pointer to the location-current loc i.e. 53+content of text file
//write remaining image content to image file
while(i<BinarySourceSize-headerSize-TextSourceSize+1)
{
i=i+BlockSize;
Buffer=new char[BlockSize];
ReadBinary.read(Buffer,BlockSize);//reads no of bytes and stores them into mem, size contains no of bytes in a file
WriteBinary.write(Buffer,BlockSize);
delete[] Buffer; //clear memory, else program can fail giving correct output
}
ReadBinary.close();
WriteBinary.close();
//Encoding Completed
return 0;
}
解密代码
int Binary_decode(char *binarySourceFileName, char *txtTargetFileName, const short headerSize)
{
long TextDestinationSize=0;
char *Buffer;
long BlockSize=10240;
ifstream ReadBinary;
ofstream WriteText;
ReadBinary.open(binarySourceFileName,ios::binary|ios::in);//file will be appended
if(!ReadBinary)
{
cprintf("File can not be opened");
return 0;
}
ReadBinary.seekg(headerSize,ios::beg);
Buffer=new char[4];
ReadBinary.read(Buffer,4);
TextDestinationSize=*((long *)Buffer);
delete[] Buffer;
cout<<"\n\n";
cprintf("Size of the File that will be created is : ");
cout<<TextDestinationSize;
cprintf(" Bytes");
cout<<"\n\n";
sleep(1);
WriteText.open(txtTargetFileName,ios::binary|ios::out|ios::trunc);//file will be created if not exists else truncate its data
while(TextDestinationSize>0)
{
if(TextDestinationSize<BlockSize)
BlockSize=TextDestinationSize;
Buffer= new char[BlockSize];
ReadBinary.read(Buffer,BlockSize);
WriteText.write(Buffer,BlockSize);
delete[] Buffer;
TextDestinationSize=TextDestinationSize-BlockSize;
}
ReadBinary.close();
WriteText.close();
return 0;
}
int text_encode(char *SourcefileName, char *DestinationfileName)
{
ifstream fr; //reads
ofstream fw; // writes to file
char c;
int random;
clrscr();
fr.open(SourcefileName,ios::binary);//file name, mode of open, here input mode i.e. read only
if(!fr)
{
cprintf("File can not be opened.");
getch();
return 0;
}
fw.open(DestinationfileName,ios::binary|ios::out|ios::trunc);//file will be created or truncated if already exists
while(fr)
{
int i;
while(fr!=0)
{
fr.get(c); //reads a character from file and increments its pointer
char ch;
ch=c;
ch=ch+1;
fw<<ch; //appends character in c to a file
}
}
fr.close();
fw.close();
return 0;
}
int text_decode(char *SourcefileName, char *DestinationName)
{
ifstream fr; //reads
ofstream fw; // wrrites to file
char c;
int random;
clrscr();
fr.open(SourcefileName,ios::binary);//file name, mode of open, here input mode i.e. read only
if(!fr)
{
cprintf("File can not be opened.");
return 0;
}
fw.open(DestinationName,ios::binary|ios::out|ios::trunc);//file will be created or truncated if already exists
while(fr)
{
int i;
while(fr!=0)
{
fr.get(c); //reads a character from file and increments its pointer
char ch;
ch=c;
ch=ch-1;
fw<<ch; //appends character in c to a file
}
}
fr.close();
fw.close();
return 0;
}
这不是隐写术,而是扔掉了图像文件的主体,而是将其替换为文本?
我认为您忘记了对机密数据进行实际编码,而只是将机密数据以纯文本格式放入文件中。 您的代码还有许多其他问题,例如“ i”在“解密” while循环中的作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.