/* To normalize a given text file
ie, only a single blank to exist between 2 given words
calculate the hidden message of the given file
as a function of the input file
get another hidden message from the user
and create the output file with the combined hidden message (watermarking)
not yet:=>Tried and tested :: The required output file has been generated */
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<math.h>
#include<string.h>
void main()
{
FILE *fp,*ft;
char
ch,s[80],temp[20],message[10],base[10],aux[80],hidden_copy[20],hidden[20],hh;
// remember we can't use ASCII 0 as it is NULL
int
i,j,k=0,flag=0,c,count=0,check,mess[10],m,base_8[10],t,arr[20],ch1=32,ch2=9,tt1,tt2,l;
fp=fopen("c:\\plain.txt","r+");
ft=fopen("c:\\text.txt","w+");
clrscr();
if(fp==NULL)
{
printf("\n Unable to open File ");
getch();
exit(0);
}
while(!feof(fp))
{
ch=fgetc(fp);
if(ch!=' ' && (int)ch!=9)
{
fputc(ch,ft);
flag=0;
}
else
flag++;
if(flag==1)
putc(' ',ft);
}
fclose(fp);
rewind(ft);
printf("\n\n\n \t My normalized file is ...\n\n\n");
/*while(!feof(ft))
{
printf("%c",fgetc(ft));
} */
while(fgets(s,79,ft)!=NULL)
{
printf("%s ",s,count);
count++;
}
count--;
printf("\n\tNo. of lines =>%d",count);
fclose(ft);
printf("\nEnter the main hidden message in small letters (< than %d
characters)",count);
gets(hidden_copy);
l=strlen(hidden_copy);
// calculate the hidden message
fp=fopen("c:\\text.txt","r+");
if(fp==NULL)
{
printf("\n Unable to open File ");
getch();
exit(0);
}
while(fgets(s,79,fp)!=NULL)
{
i=0;j=0;check=0;count=0;
while(s[i]!=NULL)
{
if(s[i]!=' ' &&
check==0)
{
temp[j++]=s[i];
count+=int(s[i]);
//printf(" %d",count);
check=1;
}
if(s[i]==' ')
check=0;
i++;
}
temp[j]=NULL;
m=count%26;
printf("\n\t %s =%d
%d",temp,count,m);
message[k]='a'+m;
mess[k]=m;
k++;
}
message[k-1]=NULL;
printf("\n\tCalculated mesage=%s",message);
i=0;
while(hidden_copy[i]!=NULL)
i++;
printf("\n i =>%d k-1=>%d",i,k-1);
/* for(j=i;j<k-1;j++)
hidden_copy[j]=' ';
*/
while(i<(k-1))
{
hidden_copy[i]=32;
i++;
}
hidden_copy[k-1]=NULL;
//printf("\n testing : %s",hidden_copy);
printf("\n testing =>");
for(i=0;i<k-1;i++)
printf("%c",hidden_copy[i]);
getch();
for(i=0;i<k-1;i++)
{
if(hidden_copy[i]==' ')
hidden[i]='?'; // (63)
base 10 ie (77) base 8
else
hidden[i]=hidden_copy[i]+message[i]-2*'a';
}
hidden[k-1]=NULL;
printf("\n\tfinal message =%s",hidden);
// change the base of the message to 8
for(i=0;i<20;i++)
base_8[i]=0;
i=0;
while(i<l)
{
hh=hidden[i];
t=hh;
j=0;
while(t!=0)
{
base_8[i]+=((t%8)*(pow(10,j)));
t/=8;
j++;
}
i++;
}
// hidden message in base 8::
printf("\n\tChange base of the encrypted hidden message to
8:\n\t");
for(i=0;i<k-1;i++)
printf(" %d",base_8[i]);
getch();
/* printf("\n\t hidden message to be watermarked into the output
file: ");
for(i=0;i<k-1;i++)
base[i]=base_8[i]+'a';
base[i]=NULL;
printf("%s",base);
*/
// Watermark the hidden message into the final file
rewind(fp);// the original file should be rewinded
ft=fopen("c:\\final.txt","w+"); /* we will write
the final file with
the hidden message in c:\final.txt */
k=0;
while(fgets(s,79,fp)!=NULL)
{
i=0;j=0;count=0;flag=0;
tt2=base_8[k]%10;
tt1=(base_8[k]-tt2)/10;
printf("\n\t tt1=%d tt2=%d",tt1,tt2);
while(s[i]!=NULL)
{
if(tt1==count && flag==0)
aux[j++]=ch1;
if(tt2==count && flag==0)
aux[j++]=ch2;
if(s[i]!=' ')
{
aux[j]=s[i];
flag=1;
}
if(s[i]==' ')
{
flag=0;
count++;
aux[j]=s[i];
}
j++;
i++;
}
k++;
aux[j]=NULL;
fputs(aux,ft);
}
rewind(ft);
printf("\n\n\t The final file is :\n");
while(!feof(ft))
printf("%c",fgetc(ft));
fclose(fp);
fclose(ft);
getch();
}
/* Low-level file-copy intended for image copying */
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys\types.h>
#include<sys\stat.h>
#include<io.h>
main (int argc,char *argv[])
{
clrscr();
char buffer[512],buffer1[512],buffer2[8],*str;
int inhandle1,inhandle2,outhandle,bytes,bytes1,i,k=0,h;
/* Reading hidden message to be embedded */
printf("Enter text to be hidden :");
gets(str);
int l=strlen(str);
if(argc!=3)
{
puts("\n\t Insufficient arguments");
exit(1);
}
/* Reading the host image file */
inhandle1=open(argv[1],O_RDONLY|O_BINARY);
if(inhandle1==-1)
{
puts("\n \t Cannot open file");
exit(1);
}
/* Writing pixel information of host image into the image file to be transmitted */
outhandle=open(argv[2],O_CREAT|O_BINARY|O_WRONLY,S_IWRITE);
if(outhandle==-1)
{
puts("\n \t Cannot open file");
exit(1);
}
bytes=read(inhandle1,buffer1,54);
write(outhandle,buffer1,bytes);
/* Inserting hidden message into the final image file */
while(1)
{
bytes=read(inhandle1,buffer1,512);
for(i=0;i<bytes;i++)
{
for(h=0;h<l;h++)
{
if(i%512==0 )
{
buffer[i]=str[h];
}
else
buffer[i]=buffer1[i];
}
}
/* Writing into the final image file from output buffer */
if(bytes>0)
write(outhandle,buffer,bytes);
else
break;
}
close(inhandle1);
close(outhandle);
return(0);
getch();
}
/* Low-level file-copy intended for text extraction*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys\types.h>
#include<sys\stat.h>
#include<io.h>
main (int argc,char *argv[])
{
clrscr();
char buffer[512],buffer1[512],buffer2[8],*str;
int inhandle1,inhandle2,outhandle,bytes,bytes1,i,h;
if(argc!=3)
{
puts("\n\t Insufficient arguments");
exit(1);
}
/* Opening transmitted file in Read Only mode */
inhandle1=open(argv[1],O_RDONLY|O_BINARY);
/* Opening another file to store extracted text */
outhandle=open(argv[2],O_CREAT|O_BINARY|O_WRONLY|S_IWRITE);
if(inhandle1==-1)
{
puts("\n \t Cannot open file");
exit(1);
}
if(outhandle==-1)
{
puts("\n \t Cannot open file");
exit(1); }
54
/* Reading extracted hidden message into output buffer */
while(1)
{
bytes=read(inhandle1,buffer1,512);
for(i=0;i<bytes;i++)
{
if(i%512==0 )
{
buffer[i]=buffer1[i];
}
}
/* Copying hidden text into Output file */
if(bytes>0)
write(outhandle,buffer,bytes);
else
break;
}
close(inhandle1);
close(outhandle);
/* Reading file containing the hidden message */
inhandle2=open(argv[2],O_RDONLY|O_TEXT);
if(inhandle2==-1)
{
puts("\n \t Cannot open file");
exit(1);
}
while(1)
{
bytes=read(inhandle2,buffer,512);
for(i=0;i<bytes;i++)
{
*str=buffer[i];
str++;
}
}
puts(str);
close(inhandle2);
return(0);
getch();
}