1/* NOCW */ 2/* 3 Please read the README file for condition of use, before 4 using this software. 5 6 Maurice Gittens <mgittens@gits.nl> January 1997 7 8*/ 9 10#include <stdio.h> 11#include <unistd.h> 12#include <fcntl.h> 13#include <sys/stat.h> 14#include <openssl/evp.h> 15 16#define STDIN 0 17#define STDOUT 1 18#define BUFLEN 512 19 20static const char *usage = "Usage: example4 [-d]\n"; 21 22void do_encode(void); 23void do_decode(void); 24 25int main(int argc, char *argv[]) 26{ 27 if ((argc == 1)) 28 { 29 do_encode(); 30 } 31 else if ((argc == 2) && !strcmp(argv[1],"-d")) 32 { 33 do_decode(); 34 } 35 else 36 { 37 fprintf(stderr,"%s", usage); 38 exit(1); 39 } 40 41 return 0; 42} 43 44void do_encode() 45{ 46 char buf[BUFLEN]; 47 char ebuf[BUFLEN+24]; 48 unsigned int ebuflen; 49 EVP_ENCODE_CTX ectx; 50 51 EVP_EncodeInit(&ectx); 52 53 while(1) 54 { 55 int readlen = read(STDIN, buf, sizeof(buf)); 56 57 if (readlen <= 0) 58 { 59 if (!readlen) 60 break; 61 else 62 { 63 perror("read"); 64 exit(1); 65 } 66 } 67 68 EVP_EncodeUpdate(&ectx, ebuf, &ebuflen, buf, readlen); 69 70 write(STDOUT, ebuf, ebuflen); 71 } 72 73 EVP_EncodeFinal(&ectx, ebuf, &ebuflen); 74 75 write(STDOUT, ebuf, ebuflen); 76} 77 78void do_decode() 79{ 80 char buf[BUFLEN]; 81 char ebuf[BUFLEN+24]; 82 unsigned int ebuflen; 83 EVP_ENCODE_CTX ectx; 84 85 EVP_DecodeInit(&ectx); 86 87 while(1) 88 { 89 int readlen = read(STDIN, buf, sizeof(buf)); 90 int rc; 91 92 if (readlen <= 0) 93 { 94 if (!readlen) 95 break; 96 else 97 { 98 perror("read"); 99 exit(1); 100 } 101 } 102 103 rc = EVP_DecodeUpdate(&ectx, ebuf, &ebuflen, buf, readlen); 104 if (rc <= 0) 105 { 106 if (!rc) 107 { 108 write(STDOUT, ebuf, ebuflen); 109 break; 110 } 111 112 fprintf(stderr, "Error: decoding message\n"); 113 return; 114 } 115 116 write(STDOUT, ebuf, ebuflen); 117 } 118 119 EVP_DecodeFinal(&ectx, ebuf, &ebuflen); 120 121 write(STDOUT, ebuf, ebuflen); 122} 123 124