作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我做了一个隐写程序来加密/解密图像音频和视频下的一些文本。
我使用图像作为 bmp(54 字节标题)文件,音频作为 wav(44 字节标题)文件和视频作为 avi(56 字节标题)文件格式。
当我尝试加密所有这些文件下的文本时,它被成功加密,并且也被正确解密。
但这会导致音频和视频出现问题,即这些文件在加密结果后无法播放。
可能是什么问题。我正在研究 Turbo C++ 编译器。我知道它是 super 过时的编译器,但我只能这样做。
这是我的要加密的代码。
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;
}
最佳答案
这不是隐写术,这是丢弃图像文件的主体并用文本替换它?
关于c++ - 隐写术 : Encoded audio and video file not being played, 已损坏。什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2708389/
本文分享自华为云社区《华为大咖说 | 企业应用AI大模型的“道、法、术” ——道:认知篇》,作者:华为云PaaS服务小智。 本期核心观点 上车:AGI是未来5~10年内,每个人都无法回避的技
有人可以解释一下“Forever Frame”技术是如何工作的吗?代码示例会很棒!我浏览了在 Google 中可以找到的所有 Material ,但我仍然不太了解它是如何工作的。 最佳答案 迪伦·希曼
我是一名优秀的程序员,十分优秀!