home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_200 / 247_01 / encode.c < prev    next >
Text File  |  1989-04-19  |  3KB  |  121 lines

  1. /*
  2.  *   Program to encode text using RSA public key.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include "miracl.h"
  7.  
  8. void strip(name)
  9. char name[];
  10. { /* strip off filename extension */
  11.     int i;
  12.     for (i=0;name[i]!='\0';i++)
  13.     {
  14.         if (name[i]!='.') continue;
  15.         name[i]='\0';
  16.         break;
  17.     }
  18. }
  19.  
  20. main()
  21. {  /*  encode using public key  */
  22.     big e,m,y,ke,mn,mx;
  23.     FILE *ifile;
  24.     FILE *ofile;
  25.     static char line[500];
  26.     char ifname[13],ofname[13];
  27.     bool fli,last;
  28.     int i,ipt,klen;
  29.     mirsys(100,MAXBASE);
  30.     e=mirvar(0);
  31.     m=mirvar(0);
  32.     y=mirvar(0);
  33.     ke=mirvar(0);
  34.     mn=mirvar(0);
  35.     mx=mirvar(0);
  36.     ifile=fopen("public.key","r");
  37.     IOBASE=60;
  38.     cinnum(ke,ifile);
  39.     fclose(ifile);
  40.     root(ke,3,mn);
  41.     multiply(mn,mn,m);
  42.     multiply(mn,m,mx);
  43.     subtract(mx,m,mx);
  44.     klen=0;
  45.     copy(mx,m);
  46.     while (size(m)>0)
  47.     { /* find key length in characters */
  48.         klen++;
  49.         subdiv(m,128,m);
  50.     }
  51.     klen--;
  52.     printf("file to be encoded = ");
  53.     gets(ifname);
  54.     fli=FALSE;
  55.     if (strlen(ifname)>0) fli=TRUE;
  56.     if (fli)
  57.     { /* set up input file */
  58.         strcpy(ofname,ifname);
  59.         strip(ofname);
  60.         strcat(ofname,".rsa");
  61.         ifile=fopen(ifname,"r");
  62.         printf("encoding message\n");
  63.     }
  64.     else
  65.     { /* accept input from keyboard */
  66.         ifile=stdin;
  67.         do
  68.         {
  69.             printf("output filename = ");
  70.             gets(ofname); 
  71.         } while (strlen(ofname)==0);
  72.         strip(ofname);    
  73.         strcat(ofname,".rsa");
  74.         printf("input message - finish with cntrl z\n");
  75.     }
  76.     ofile=fopen(ofname,"w");
  77.     WRAP=OFF;
  78.     ipt=0;
  79.     last=FALSE;
  80.     while (!last)
  81.     { /* encode line by line */
  82.         if (fgets(&line[ipt],132,ifile)==NULL) last=TRUE;
  83.         if (line[ipt]==EOF) last=TRUE;
  84.         ipt=strlen(line);
  85.         if (ipt<klen && !last) continue;
  86.         while (ipt>=klen)
  87.         { /* chop up into klen-sized chunks and encode */
  88.             for (i=0;i<klen;i++)
  89.                 IBUFF[i]=line[i];
  90.             IBUFF[klen]='\0';
  91.             for (i=klen;i<=ipt;i++)
  92.                 line[i-klen]=line[i];
  93.             ipt-=klen;
  94.             IOBASE=128;
  95.             cinnum(m,ifile);
  96.             power(m,3,ke,e);
  97.             IOBASE=60;
  98.             cotnum(e,ofile);
  99.         }
  100.         if (last && ipt>0)
  101.         { /* now deal with left overs */
  102.             strcpy(IBUFF,line);
  103.             IOBASE=128;
  104.             cinnum(m,ifile);
  105.             if (compare(m,mn)<0)
  106.             { /* pad out with random number if necessary */
  107.                 bigrand(mn,y);
  108.                 multiply(mn,mn,e);
  109.                 subtract(e,y,e);
  110.                 multiply(mn,e,y);
  111.                 add(m,y,m);
  112.             }
  113.             power(m,3,ke,e);
  114.             IOBASE=60;
  115.             cotnum(e,ofile);
  116.         }
  117.     }
  118.     fclose(ofile);
  119.     if (fli) fclose(ifile);
  120. }   
  121.