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

  1. /*
  2.  *   Program to encipher text using OKAMOTO 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. {  /*  encipher using public key  */
  22.     big e,m,m1,m2,n,u,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[4],kl,turn;
  29.     mirsys(100,MAXBASE);
  30.     e=mirvar(0);
  31.     m=mirvar(0);
  32.     m1=mirvar(0);
  33.     m2=mirvar(0);
  34.     n=mirvar(0);
  35.     u=mirvar(0);
  36.     mx=mirvar(0);
  37.     IOBASE=60;
  38.     ifile=fopen("public.key","r");
  39.     cinnum(n,ifile);
  40.     cinnum(u,ifile);
  41.     fclose(ifile);
  42.     root(n,9,mx);
  43.     kl=0;
  44.     while (size(mx)>0)
  45.     { /* find max message length < n^(1/9) */
  46.         kl++;
  47.         subdiv(mx,128,mx);
  48.     }
  49.     klen[0]=klen[1]=kl-1;
  50.     copy(n,mx);
  51.     kl=0;
  52.     while (size(mx)>0)
  53.     { /* find max message length  < n */
  54.         kl++;
  55.         subdiv(mx,128,mx);
  56.     }
  57.     klen[2]=klen[3]=kl-1;
  58.     printf("file to be enciphered = ");
  59.     gets(ifname);
  60.     fli=FALSE;
  61.     if (strlen(ifname)>0) fli=TRUE;
  62.     if (fli)
  63.     { /* set up input file */
  64.         strcpy(ofname,ifname);
  65.         strip(ofname);
  66.         strcat(ofname,".oka");
  67.         ifile=fopen(ifname,"r");
  68.         printf("enciphering message\n");
  69.     }
  70.     else
  71.     { /* accept input from keyboard */
  72.         ifile=stdin;
  73.         do
  74.         {
  75.             printf("output filename = ");
  76.             gets(ofname); 
  77.         } while (strlen(ofname)==0);
  78.         strip(ofname);    
  79.         strcat(ofname,".oka");
  80.         printf("input message - finish with cntrl z\n");
  81.     }
  82.     ofile=fopen(ofname,"w");
  83.     WRAP=OFF;
  84.     ipt=0;
  85.     last=FALSE;
  86.     turn=0;
  87.     while (!last)
  88.     { /* encipher line by line */
  89.         if (fgets(&line[ipt],132,ifile)==NULL) last=TRUE;
  90.         if (line[ipt]==EOF) last=TRUE;
  91.         ipt=strlen(line);
  92.         if (ipt<klen[turn] && !last) continue;
  93.         while (ipt>=klen[turn])
  94.         { /* chop up into klen-sized chunks and encipher */
  95.             kl=klen[turn];
  96.             for (i=0;i<kl;i++)
  97.                 IBUFF[i]=line[i];
  98.             IBUFF[kl]='\0';
  99.             for (i=kl;i<=ipt;i++)
  100.                 line[i-kl]=line[i];
  101.             ipt-=kl;
  102.             IOBASE=128;
  103.             if (turn==0)
  104.             {
  105.                 turn=1;
  106.                 cinnum(m1,ifile);
  107.                 continue;
  108.             }
  109.             cinnum(m,ifile);
  110.             if (turn==1)
  111.             {
  112.                 copy(m,m2);
  113.                 multiply(m1,u,e);
  114.                 add(e,m2,e);
  115.                 mad(e,e,e,n,n,e);   /* e = (m2+m1.u)^2 mod n */
  116.             }
  117.             if (turn==2) mad(m2,m,m,n,n,e);
  118.             if (turn==3) mad(m1,m,m,n,n,e);
  119.             IOBASE=60;
  120.             cotnum(e,ofile);
  121.             turn++;
  122.             if (turn>3) turn=0;
  123.         }
  124.         if (last && (ipt>0  || turn==1))
  125.         { /* now deal with left overs */
  126.             strcpy(IBUFF,line);
  127.             IOBASE=128;
  128.             if (turn==0)
  129.             {
  130.                 zero(m2);
  131.                 cinnum(m1,ifile);
  132.             }
  133.             else if (ipt>0) cinnum(m,ifile);
  134.             if (turn==1)
  135.             {
  136.                 copy(m,m2);
  137.                 multiply(m1,u,e);
  138.                 add(e,m2,e);
  139.                 mad(e,e,e,n,n,e);
  140.             }
  141.             if (turn==2) mad(m2,m,m,n,n,e);
  142.             if (turn==3) mad(m1,m,m,n,n,e);
  143.             IOBASE=60;
  144.             cotnum(e,ofile);
  145.         }
  146.     }
  147.     fclose(ofile);
  148.     if (fli) fclose(ifile);
  149. }   
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.