views:

235

answers:

2

I have made a steganography program to encrypt/Decrypt some text under image audio and video.

I used image as bmp(54 byte header) file, audio as wav(44 byte header) file and video as avi(56 byte header) file formats.

When I tries to encrypt text under all these file then it gets encrypted successfully and are also getting decrypted correctly.

But it is creating a problem with audio and video i.e these files are not being played after encrypted result.

What can be the problem. I am working on Turbo C++ compiler. I know it is super outdated compiler but I have to do it in this only.

Here is my code to encrypt.

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;
}

Code to decrypt

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;
}
+1  A: 

This isn't steganography, this is throwing away the body of an image file and replacing it with text?

pjc50
A: 

I think you forgot to actually encode the secret data, and instead simply put the secret data in plaintext into a file. There are a number of other issues with your code, such as what "i" does in the "decrypting" while loop.

bowenl2