home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cactus.zip / Source / FCryptM.cpp next >
Text File  |  1996-04-14  |  3KB  |  110 lines

  1. #include  <string.h>
  2. #include  <stdlib.h>
  3. #include  <stdio.h>
  4. #include <iomanip.h>
  5. #include <fstream.h>
  6. #include "cactus.hpp"
  7. #include <string.hpp>
  8. int main(int argc, char * argv[]){
  9. char * key, * msk;
  10. char *path;
  11. char ch;
  12. unsigned long length,clr_len;
  13. String clr_str,cry_str;
  14. unsigned long i,j,k;
  15. char clr[256],cry[256];
  16. char i1,i2,i3,o1,o2,o3,o4;
  17. char p[] ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  18. MiCactus crypt;
  19. if (!registered) cout << "\a >>> Please register FCryptM.EXE! \a\n";
  20. if (argc < 4) {
  21.    cout << "File encryption using the CACTUS-Algorithm and MIME coding." << '\n'
  22.         << "Author: Michael Mieves, Version 1.0, April 1996." << '\n'
  23.         << "FCryptM produces a MIME coded encrypted/decrypted file." << '\n'
  24.         << "Usage: FCryptM -e|d <clearfile> <cryptfile> " << '\n'
  25.         << "-e encrypts an clear file, -d decrypts a crypted file." << '\n';
  26.         return 0;
  27.    }
  28. cout << "Please wait . . ." << '\n';
  29. char mode[3];
  30. int crypt_flag;
  31. strcpy(mode,argv[1]);
  32. strcpy(clr,argv[2]);
  33. strcpy(cry,argv[3]);
  34. key = new char[crypt.lenall];
  35. msk = new char[256];
  36. for (i = 0;i<crypt.lenall;i++){
  37.             key[i] = 0;
  38.             }
  39. for (i = 0;i<256;i++){
  40.             msk[i] = 0;
  41.             }
  42. if(path == getenv("CACTUSKEY")){
  43.       ifstream inkey(path,ios::binary);
  44.       for (i = 0;i<crypt.lenall;i++){
  45.            inkey >> key[i];
  46.            }
  47.       for (i = 0;i<256;i++){
  48.            inkey >> msk[i];
  49.            }
  50.       inkey.close();
  51.       }
  52.  
  53. crypt.initiate(key,msk);
  54.  
  55. if (mode[1] == 'e'){
  56.     crypt.reset(key,msk,0);
  57.     ifstream infile(clr,ios::binary|ios::ate);
  58.     length = infile.tellg();
  59.     infile.seekg(0);
  60.     ofstream outfile(cry,ios::binary);
  61.     outfile.flags(ios::left);
  62.     outfile << setw(10) << length;
  63.     for (k=0;k<length;k++){
  64.         clr_str.put_at(k,infile.get());
  65.         }
  66.     for (i=0;i<clr_str.length();i=i+3){
  67.        i1=i2=i3=0;
  68.        i1=clr_str[i] ^ crypt.gen_byte();
  69.        i2=clr_str[i+1] ^ crypt.gen_byte();
  70.        i3=clr_str[i+2] ^ crypt.gen_byte();
  71.        outfile.put((char)p[((i1&0xfc)>>2)]);
  72.        outfile.put((char)p[((i1&3)<<4) + ((i2&0xf0)>>4)]);
  73.        outfile.put((char)p[((i2&0x0f)<<2) + ((i3&0xc0)>>6)]);
  74.        outfile.put((char)p[(i3&0x3f)]);
  75.        }
  76.     infile.close();
  77.     outfile.close();
  78.     }
  79.  else{
  80.     crypt.reset(key,msk,0);
  81.     ifstream infile(cry,ios::binary|ios::ate);
  82.     length = infile.tellg();
  83.     infile.seekg(0);
  84.     infile >> dec >> clr_len;
  85.     infile.seekg(10);
  86.     ofstream outfile(clr,ios::binary);
  87.     for (k=0;k<length;k++){
  88.         cry_str.put_at(k, infile.get());
  89.         }
  90.     j=0;
  91.     for (i=0;i<cry_str.length();i=i+4){
  92.        o1=o2=o3=o4=0;
  93.        o1 = cry_str[i];
  94.        o2 = cry_str[i+1];
  95.        o3 = cry_str[i+2];
  96.        o4 = cry_str[i+3];
  97.        clr_str.put_at(j, (((strchr(p,o1)-p)<<2) + (((strchr(p,o2)-p)&0x30)>>4)) ^ crypt.gen_byte());
  98.        clr_str.put_at(j+1,((((strchr(p,o2)-p)&0xf)<<4) + (((strchr(p,o3)-p)&0x3c)>>2)) ^ crypt.gen_byte());
  99.        clr_str.put_at(j+2,((((strchr(p,o3)-p)&3) <<6 )+ (strchr(p,o4)-p)) ^ crypt.gen_byte());
  100.        j = j+3;
  101.        }
  102.      clr_str = clr_str(0,clr_len);
  103.      outfile << clr_str;
  104.      infile.close();
  105.      outfile.close();
  106.   }
  107.  
  108. return 1;
  109. }
  110.