home *** CD-ROM | disk | FTP | other *** search
/ Hacks & Cracks / Hacks_and_Cracks.iso / hackersguides-&-software / hdsk41.zip / SOURCE.ZIP / HIDESEEK.C < prev    next >
C/C++ Source or Header  |  1994-04-20  |  9KB  |  383 lines

  1. #include "hideseek.h"
  2. #include "gif.hpp"
  3. #include "idea.h"
  4. #include <time.h>
  5.  
  6. //--------------global data---------------------
  7. char giffile[200];   // lots of room for a filename is a Good Thing
  8. unsigned int key[8];
  9. int dispersion;
  10. unsigned seed;
  11. int version,subversion;
  12.  
  13. //--------------functions----------------------
  14. void usage_exit(char *module)
  15. {
  16.    if (stricmp(module,"hide")==0)
  17.    {
  18.       printf("\nHIDE data hiding program v %d.%d\n",VERSION,SUBVERSION);
  19.       printf("      hides data inside GIF files.\n");
  20.       printf("\n    usage: hide <infile.ext> <giffile[.gif]> [key]\n");
  21.       printf("             where [key] is an optional locking code, up to 8 chars.\n");
  22.       printf("\n    note: will always produce a new GIF called outfile.gif\n");
  23.    }
  24.    else if (stricmp(module,"seek")==0)
  25.    {
  26.       printf("\nSEEK data extractor v %d.%d\n",VERSION,SUBVERSION);
  27.       printf("       extracts data hidden in GIF files by HIDE\n");
  28.       printf("\n     usage: seek <infile[.gif]> <outfile.ext> [key]\n");
  29.       printf("\n     note: if <outfile.ext> exists, it will be overwritten, so BE CAREFULL!!\n");
  30.    }
  31.    exit(0);
  32. };
  33.  
  34. void process_args(int argc, char *argv[])
  35. {
  36.    struct ffblk ffbk;
  37.    int x=0;
  38.    char *drive=(char *)malloc(200*sizeof(char)); //yes, 200 is a lot mof memory for this
  39.    char *dir=(char *)malloc(200*sizeof(char));   // version 3.5 only allocated 25, and would
  40.    char *fname=(char *)malloc(200*sizeof(char)); // crash if run from a deep sub-directory.
  41.    char *ext=(char *)malloc(200*sizeof(char));  // this will put up with much deeper subdirectories
  42.                         // although it still could crash....
  43.    fnsplit(argv[0],drive,dir,fname,ext);
  44.  
  45.    if (argc!=3 && argc!=4) usage_exit(fname);
  46.  
  47.    if (argc==3)                      // default key---fill, it with 0's
  48.    {
  49.        for (int x=0;x<8;x++) key[x]=0;
  50.    }
  51.    else if (strlen(argv[3])>8)
  52.    {
  53.       printf("\nKEY can be no longer than 8 characters!!!\n");
  54.       exit(0);
  55.    }
  56.    else
  57.    {
  58.       for (int x=0;x<8;x++)
  59.      if (*argv[3]!='\0') key[x]=*argv[3]++;  // put the key into "key"
  60.      else key[x]=0;                          // and pad with 0's (important!)
  61.    }
  62.  
  63.    if (stricmp(fname,"hide")==0)          // set up for hide
  64.    {
  65.       if (findfirst(argv[1],&ffbk,0)!=0)
  66.       {
  67.      printf("\nInfile %s not found.\n",argv[1]);
  68.      exit(0);
  69.       }
  70.       else
  71.       {
  72.      x=0;
  73.      while (argv[2][x]!='.' && argv[2][x]!='\0') x++;
  74.      if (argv[2][x]!='.')
  75.      {
  76.         strcpy(giffile,argv[2]);
  77.         strcat(giffile,".gif");
  78.      }
  79.      else strcpy(giffile,argv[2]);
  80.      if (findfirst(giffile,&ffbk,0)!=0)
  81.      {
  82.         printf("\nGIF file %s not found.\n",giffile);
  83.         exit (0);
  84.      }
  85.       }
  86.       if (findfirst("outfile.gif",&ffbk,0)==0)
  87.       {
  88.      printf("\nOUTFILE.GIF already exists!!!! overwrite?(Y/n)");
  89.      x=getche();
  90.      printf("\n");
  91.      if (toupper(x)=='N')
  92.      {
  93.         printf("\nMake the necessary changes and re-run HIDE\n");
  94.         exit(0);
  95.      }
  96.       }
  97.    }
  98.    if (stricmp(fname,"seek")==0)      // set up for seek
  99.    {
  100.       x=0;
  101.       while (argv[1][x]!='.' && argv[1][x]!='\0') x++;
  102.       if (argv[1][x]!='.')
  103.       {
  104.      strcpy(giffile,argv[1]);
  105.      strcat(giffile,".gif");
  106.       }
  107.       else strcpy(giffile,argv[1]);
  108.       if (findfirst(giffile,&ffbk,0)!=0)
  109.       {
  110.      printf("\nGIF file %s not found.\n",giffile);
  111.      exit (0);
  112.       }
  113.    }
  114.  
  115.  
  116.  
  117.    free (dir); free(drive); free(fname); free(ext);
  118. }
  119.  
  120. void test_gif(void)
  121. {
  122.    FILE *fgif = fopen(giffile, "rb" );
  123.    if( fgif == 0 )
  124.    {
  125.       printf( "\nGIF File '%s' not found\n", giffile );
  126.       exit (0);
  127.    }
  128.  
  129.    // read header and screen descriptor
  130.    GIFHEADER hdr;
  131.    if( hdr.get( fgif ) )
  132.    {
  133.       printf( "\nError reading GIF header\n" );
  134.       exit( 0 );
  135.    }
  136.    if( ! hdr.isvalid() )
  137.    {
  138.       printf( "\nFile %s is not a valid GIF file\n",giffile);
  139.       exit( 0 );
  140.    }
  141.    GIFSCDESC scd;
  142.    if( scd.get( fgif ) )
  143.    {
  144.       printf( "\nError reading GIF screen descriptor\n" );
  145.       exit( 0 );
  146.    }
  147.  
  148.    if (scd.ncolors()!=256)
  149.    {
  150.       printf("\nGIF file %s does not have 256 colors.\n",giffile);
  151.       exit(0);       //HIDESEEK only uses 256 color (or shades-of-grey) GIFs
  152.    }
  153.  
  154.    fclose(fgif);
  155. }
  156.  
  157. int prepare_screen(void)
  158. {
  159.    int old_mode;
  160.    int x;
  161.  
  162.    clrscr();
  163.    printf("\nPreparing to process files.......");
  164.    printf("Press any key when ready.\n");
  165.    getch();
  166.  
  167.    old_mode=fg_getmode();
  168.    fg_setmode(23);
  169.    x=fg_showgif(giffile,0);
  170.    if (x!=0)
  171.    {
  172.       fg_setmode(old_mode);
  173.       printf("\ncryptic error message 101%d-ß\n",x); // means that there's a mystery problem
  174.       exit(0);                                       // shouldn't come up.
  175.    }                                                 // x=1:file not found
  176.    return old_mode;                                  // x=2:file not a GIF
  177. }                                                    // and we've already found it
  178.                              // and checked to see if its a GIF
  179. int process_infile(FILE *infile)
  180. {
  181.    int c=0;
  182.    long length=0;
  183.    long total=19000;
  184.    int used=0;
  185.  
  186.    //output header info---length and dispersion
  187.    length=filelength(fileno(infile));      // file length
  188.    dispersion=(int)(total/length);         // data dispersion
  189.    if (dispersion<1) return 0;
  190.    outheader(length);
  191.  
  192.    while ((c=getc(infile))!=EOF)
  193.    {
  194.       dispersion=(int)(total/length);
  195.       if ((used=outbyte(c))==0)
  196.      return 0;
  197.       length--;            // dynamic dispersion computation at its finest 
  198.       total*=8;
  199.       total-=used;
  200.       total/=8;
  201.    }
  202.  
  203.    return 1;
  204. }
  205.  
  206. void outheader(long length)
  207. {
  208.    word16 in[4];
  209.    word16 out[4];
  210.    IDEAkey Z;
  211.    time_t t;
  212.  
  213.    // set up random number generator and generate a seed.
  214.    srand((unsigned) time(&t));
  215.  
  216.    // encrypt length and dispersion
  217.    en_key_idea(key,Z);
  218.    in[0]=(word16)(low16(length));
  219.    in[1]=seed=(unsigned)random(32000);
  220.    in[2]=((VERSION<<8)|(SUBVERSION)); // in[3] doesn't matter--it isn't used by HIDESEEK (currently)
  221.    cipher_idea(in,out,Z);             // IDEA needs 4 subblocks though, so there it is.
  222.  
  223.    // write header info
  224.  
  225.    dispersion=1;
  226.    for (int x=0;x<4;x++)
  227.    {
  228.       outbyte((char)(out[x]&255l));
  229.       outbyte((char)(out[x]>>8));
  230.    }
  231.  
  232.    // set dispersion for rest of file and seed randomizer
  233.    dispersion=(int)(19000/length);
  234.    srand(seed);  
  235. }
  236.  
  237. int outbyte(char c)
  238. {
  239.    int v=0,clr=0,i=0;
  240.    static int x=0,y=0;         // note the static part there, kids....
  241.    int used=0,disp=0,extra=0;
  242.  
  243.    for(i=0;i<8;i++)
  244.    {
  245.      v=(c&1);
  246.      clr=fg_getpixel(x,y);
  247.      if (v==1)
  248.     clr=(clr|1);
  249.      else
  250.     clr=((clr>>1)<<1);    // sets that low bit to 0
  251.      fg_setcolor(clr);
  252.      fg_point(x,y);
  253.      disp=(random(dispersion+extra)+1);
  254.      used+=disp;
  255.      extra=((dispersion*(i+1))-used);   // even more dynamic dispersion computation
  256.      x+=disp;
  257.      if (x>=320)
  258.      {
  259.     while (x>=320)
  260.     {
  261.        x=x-319;
  262.        y++;
  263.        if (y==480 && i<7)    // oops! went past the end!
  264.           return 0;          // now how did that happen?
  265.     }
  266.      }
  267.      c>>=1;
  268.    }
  269.    return used;
  270. }
  271.  
  272. int process_outfile(char *filename)
  273. {
  274.    FILE *f;
  275.    int c=0;
  276.    unsigned count=0;
  277.    int disp=0;
  278.    word16 in[4],out[4];
  279.    IDEAkey Z,DK;
  280.  
  281.    //.....get the header info
  282.  
  283.    dispersion=1;
  284.  
  285.    // read the header
  286.  
  287.    for (int x=0;x<4;x++)
  288.    {
  289.       in[x]=0;
  290.       in[x]|=((word16)(inbyte(0)));
  291.       in[x]|=((word16)(inbyte(0)<<8));
  292.    }
  293.  
  294.    en_key_idea(key,Z);
  295.    de_key_idea(Z,DK);
  296.  
  297.    // decrypt the header
  298.  
  299.    cipher_idea(in,out,DK);
  300.  
  301.    //set the variables
  302.  
  303.    count=out[0];
  304.    seed=out[1];
  305.    version=out[2]>>8;
  306.    subversion=out[2]&255;
  307.    switch (version)  // is it a valid version number?
  308.    {
  309.       case 3: if (subversion!=5) return -1;break;   //still compatible with 3.5
  310.       case 4: if ((subversion!=0) && (subversion!=1)) return -1; break;  // and 4.0
  311.       default: return -1;
  312.    };
  313.    disp=(int)(19000/count);
  314.  
  315.    // set dispersion and seed randomizer
  316.    dispersion=disp;
  317.    srand(seed);
  318.  
  319.    // process files
  320.  
  321.    f=fopen(filename,"wb");
  322.    if (f==NULL)
  323.       return 0;
  324.    else
  325.    {
  326.       while ((count-->0) && ((c=inbyte((int)(count+1)))!=-1))
  327.      if (fwrite(&c,sizeof(char),1,f)!=1) return 0;
  328.  
  329.    }
  330.    fclose(f);
  331.    return 1;
  332. }
  333.  
  334. int inbyte(int length)
  335. {
  336.    static int x=0,y=0;
  337.    static long total=19000;
  338.    int chr=0,cnt=0,v=0;
  339.    int used=0,disp=0,extra=0;
  340.  
  341.    for (cnt=0;cnt<8;cnt++)
  342.    {
  343.       v=fg_getpixel(x,y);
  344.       v&=1;
  345.       chr=chr|(v<<cnt);
  346.       disp=(random(dispersion+extra)+1);    // look familiar? 3 words--
  347.       used+=disp;                           // Dynamic Dispersion Calculation
  348.       if (version==4 && subversion==1)      // (maybe)
  349.          extra=((dispersion*(cnt+1))-used);  //else variety remains 0
  350.       x+=disp;
  351.       if (x>=320)
  352.       {
  353.      while (x>=320)
  354.      {
  355.         x=x-319;
  356.         y++;
  357.         if (y>=480 && cnt<7)
  358.            chr=-1;
  359.      }
  360.       }
  361.    }
  362.    if (version==4 && ((subversion==0) || (subversion==1)))
  363.    {
  364.       if (length>1)
  365.       {
  366.      length--;                  //here's that dynamic dispersion
  367.      total*=8;                 // calulation again.
  368.      total-=used;
  369.      total/=8;
  370.      dispersion=(int)(total/length);
  371.       }
  372.    }
  373.  
  374.    return chr;
  375. }
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.