2010-04-25 22 views
0

我做了一個隱寫術程序來加密/解密圖像音頻和視頻下的一些文本。隱寫:編碼的音頻和視頻文件沒有被播放,被損壞。問題是什麼

我用圖像作爲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; 
} 
+0

'cprintf'是非標準的。請將其轉換爲'cout <<',以便我們可以運行您的代碼。我在那裏看不到'main()'。另外,您是如何執行原始文件和解密文件之間的比較? – Potatoswatter 2010-04-25 21:09:36

+0

我很難理解你的代碼以及它應該做什麼。你至少應該避免使用'new []'''delete []'東西,並用自動向量代替它。 – sellibitze 2010-04-26 01:41:41

+0

@sellibitze&@Patatoswatter:我在Turbo C++編譯器上開玩笑。並且我沒有在這裏包含整個代碼,我提供了一個在傳遞文件名後被調用的函數。我從來沒有使用矢量,所以我不知道它。請幫助我 – 2010-04-26 14:36:19

回答

1

這不是隱祕,這是扔掉身體的圖像文件,並用文字代替它?

0

我想你忘了實際編碼祕密數據,而只是將祕密數據以明文形式放入文件中。你的代碼還有其他許多問題,例如「我」在「解密」while循環中做了什麼。

相關問題