#include "global.h"
#include "rsaref.h"
#include "des_ecb.h"
#include "r_encode.h"
#include <stdio.h>
#include <string.h>
 
DES_ECB_CTX context ;
unsigned char key[8];

#define ASCIIPAD 0xFF
#define ENCRYPT 1
#define DECRYPT 0

void print_N ( FILE * F, unsigned char * s, int N ) {
   while (N--) {
      if ( *s!=ASCIIPAD ) fprintf(F,"%c",*s) ;
      s++ ;
   }
   fprintf(F,"\n") ;
} 

void print_N2 ( FILE * F, unsigned char * s, int N ) {
   while (N--) {
      if ( *s!=ASCIIPAD ) fprintf(F,"%c",*s) ;
      s++ ;
   }
} 

int get_clear ( FILE * F, unsigned char * buf ) {
   /* returns a buffer full of the next characters to
      process, and the number of characters returned.
      This should be 48 except for the last line of
      text. 0 means end of file */
   int nc = 0 ;
   int i ;
   while ( nc<48 && (i=getc(F))!=EOF ) {
      nc++ ;
      *buf++ = i ;
   }
   while ( nc%8 ) {
printf("get_clear: pad\n") ;
      *buf++ = ASCIIPAD ;
      nc++ ;
   }
   return(nc) ;
}

int get_cipher( FILE * F, unsigned char * buf ) {
   /* returns a buffer full of he next characters to
   process, and the number of characters returned.
   This should be 64 except for the list line of text.
   0 means end of file */
 
   while ( fgets( buf, 70, F ) ) 
      if ( buf[0]!='-' ) return( strlen(buf)-1 ) ;
   return(0) ;
}

int detect_filetype(F) {
   return( DECRYPT ) ;
}

int main(int argc, char *argv[]){

   unsigned char s[100], t[100] ;
   unsigned char encodedBlock[100] ;
   int encodedBlockLen ;
   int sl ;
   int i ;
   int mode = ENCRYPT ;
   FILE * F, * G ;

   for ( i=0;i<8;i++) s[i]= ASCIIPAD ;
   printf("Keyword: ") ;
   gets( s ) ;
   for ( i=0;i<8;i++) key[i]=s[i] ;

   printf("File: ") ;
   gets( s ) ;
   F = fopen( s, "r" ) ;
   if ( F==NULL ) {
      fprintf(stderr,"Can't open %s\n", s ) ;
      exit(1) ;
   }
   G = fopen( strcat( s, ".pem"), "w" ) ;
   if ( G==NULL ) {
      fprintf(stderr,"Can't open %s\n", s ) ;
      exit(1) ;
   }

   mode = detect_filetype(F) ;
   if ( argc>1 ) mode = DECRYPT ;
   else mode = ENCRYPT ;

   if ( mode==ENCRYPT ) {

    /*fprintf(G, "-----BEGIN PEM-----\n") ;
      fprintf(G, "DEK-Info: DES-ECB\n\n") ;
    */

      DES_ECBInit (&context, key, ENCRYPT) ;

      while ( sl = get_clear(F, s) ) {

         /*Encrypt */
         DES_ECBUpdate (&context, t, s, sl) ;
         /* ASCIIize the output */
         R_EncodePEMBlock( encodedBlock, &encodedBlockLen, t, sl) ;
         /* output */
         print_N(G,encodedBlock,encodedBlockLen) ;

      }
    /*
      fprintf(G, "-----END PEM-----\n") ;
    */
   } 

   if ( mode==DECRYPT ) {

      DES_ECBInit (&context, key, DECRYPT) ;

      while ( encodedBlockLen = get_cipher(F, encodedBlock) ) {
         /* repack ASCIIization */
         R_DecodePEMBlock (s, &sl, encodedBlock, encodedBlockLen) ;
         /* and decode */
         DES_ECBUpdate (&context, t, s, sl) ;
         /* and output */
         print_N2(G,t,sl) ;
      }

   }

   fclose(F) ;
   fclose(G) ;
}

