/* this program is meant to encrypt cms files after first converting
   them to ascii, it is meant to work in conjuction with a similar set
   of programs on the unix side  - S. Thorne 7/30/91 */
/* fgets trims trailing blanks off the line, so we had to add code to
   pad the line with spaces again (for the db loaders), make sure the
   record length is the same as that which is passed in.   */
 
# include <bsdtypes.h>
# include <stdio.h>
# include <string.h>
# include <des.h>
#include <des_ext>
#include  <fcntl.h>
#include  <error.h>
#include  <bsdtocms.h>
 
#pragma  linkage(CMXLATE,OS)
extern char ebcdicto;
extern char asciitoe;
#define buflen 1024 /* don't change this without also changing it on
                       the unix side */
des_cblock           my_cblok;
 
Key_schedule      key_schd;
char unsigned     ivec[buflen];
 
FILE *o,*i,*m;
char  buf[buflen + 1000];
char  output[buflen + 1000];
 
 
main(ac,av)
int ac;
char *av[];
{
 
char ebc_str[30];
char in_file[30],*end,*ptr;
char out_file[30],*cp;
int  x,thelen,ln,recl;
size_t  num,len,rc;
if (ac < 6) {
printf("You must enter the input and output file name and type\n");
printf("as well as the length of the record\n");
exit(); }
sprintf(in_file,"%s.%s",av[1],av[2]);
sprintf(out_file,"%s.%s",av[3],av[4]);
recl = atol(av[5]);
printf("infile %s\n",in_file);
printf("outfile %s\n",out_file);
printf("record length = %d\n",recl);
m =fopen(in_file      ,"r ");
o =fopen(out_file    ,"wb");
if (!m|!o){
  printf("file wasn't opend\n");exit(); }
printf("Password:\n");  /* should be using des_read_password, but it
                      does not work under current release */
fflush(stdout);
gets(ebc_str);
des_string_to_key(ebc_str,my_cblok);
des_key_sched(my_cblok ,    key_schd );
thelen = 0;
cp = buf;
while (!feof(m))
{
      fgets(cp , 1000  , m);
ln = strlen(cp);
if (ln == 0)
  break;
ptr = cp + ln -1;                 /* ok */
strncpy(ptr,"                        ",recl - ln);
cp[recl - 1] = '\n';
cp[recl] ='\0';
thelen = thelen + recl;
cp = cp + recl;
 
   if (thelen > buflen  ) { /* filled the buffer */
       CMXLATE(buf,&ebcdicto,buflen );
   des_cbc_encrypt(buf, output, buflen , key_schd , ivec, DES_ENCRYPT);
       fwrite(output,1,buflen , o);
/* now move the remainder to beginning of bu and reset ptrs & lens */
       bzero(buf,buflen);
       bcopy(buf+buflen ,buf,thelen -buflen);
       cp = buf + (thelen - buflen );
       thelen = thelen - buflen ;
      }
 
 
   } /* end of while */
    len = thelen;    /* write out the remainder */
    CMXLATE(buf,&ebcdicto,len); /* convrt > ascii */
    des_cbc_encrypt (buf, output, len, key_schd   , ivec, DES_ENCRYPT);
x = len%8;
if (x!=0)
len = len + 8 -x; /*routines pad to 8 byte multiples */
   fwrite(output,1  , len,o);
fclose( o);
fclose( m );
 
}
 
 
 
 
