home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cactus.zip / Source / StrCrypt.cpp < prev   
Text File  |  1996-04-14  |  3KB  |  92 lines

  1. #include  <string.h>
  2. #include  <stdlib.h>
  3. #include  <stdio.h>
  4. #include <iomanip.h>
  5. #include "cactus.hpp"
  6. int main(int argc, char * argv[]){
  7. char * key, * msk;
  8. char *path;
  9. char ch;
  10. unsigned long i,j;
  11. char i1,i2,i3,o1,o2,o3,o4;
  12. char p[] ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  13. MiCactus crypt;
  14. if (!registered) cout << "\a >>> Please register StrCrypt.EXE! \a\n";
  15. if (argc < 3) {
  16.    cout << "String encryptor using the CACTUS-Algorithm and MIME coding." << '\n'
  17.         << "Author: Michael Mieves, Version 1.0, April 1996." << '\n'
  18.         << "StrCrypt returns an encrypted/decrypted string (max 255 bytes)." << '\n'
  19.         << "Usage: StrCrypt -e|d <string> " << '\n'
  20.         << "-e encrypts an clear string, -d decrypts a crypted string" << '\n';
  21.         return -1;
  22.    }
  23. cout << "Please wait . . ." << '\n';
  24.  
  25. char clear_string[256];
  26. char crypt_string[350];
  27. char mode[3];
  28. int crypt_flag;
  29. strcpy(mode,argv[1]);
  30.  
  31. key = new char[crypt.lenall];
  32. msk = new char[256];
  33. for (i = 0;i<crypt.lenall;i++){
  34.             key[i] = 0;
  35.             }
  36. for (i = 0;i<256;i++){
  37.             msk[i] = 0;
  38.             }
  39. if(path == getenv("CACTUSKEY")){
  40.       ifstream inkey(path,ios::binary);
  41.       for (i = 0;i<crypt.lenall;i++){
  42.            inkey >> key[i];
  43.            }
  44.       for (i = 0;i<256;i++){
  45.            inkey >> msk[i];
  46.            }
  47.       inkey.close();
  48.       }
  49.  
  50. crypt.initiate(key,msk);
  51. crypt.reset(key,msk,0);
  52. if (mode[1] == 'e'){
  53.     strcpy(clear_string,argv[2]);
  54.     j = 0;
  55.     crypt_string[0]='\0';
  56.     for (i=0;i<strlen(clear_string);i=i+3){
  57.        i1=i2=i3=0;
  58.        i1=clear_string[i] ^ crypt.gen_byte();
  59.        i2=clear_string[i+1] ^ crypt.gen_byte();
  60.        i3=clear_string[i+2] ^ crypt.gen_byte();
  61.        crypt_string[j] = p[((i1&0xfc)>>2)];
  62.        crypt_string[j+1] = p[((i1&3)<<4) + ((i2&0xf0)>>4)];
  63.        crypt_string[j+2] = p[((i2&0x0f)<<2) + ((i3&0xc0)>>6)];
  64.        crypt_string[j+3] = p[(i3&0x3f)];
  65.        j = j+4;
  66.        }
  67.     cout << "Plain: " << clear_string << '\n';
  68.     cout << "Crypt: " << crypt_string << '\n';
  69.     }
  70.  else{
  71.     crypt.reset(key,msk,0);
  72.     strcpy(crypt_string,argv[2]);
  73.     clear_string[0] = '\0';
  74.     j = 0;
  75.     for (i=0;i<strlen(crypt_string);i=i+4){
  76.        o1=o2=o3=o4=0;
  77.        o1 = crypt_string[i];
  78.        o2 = crypt_string[i+1];
  79.        o3 = crypt_string[i+2];
  80.        o4 = crypt_string[i+3];
  81.        clear_string[j] = (((strchr(p,o1)-p)<<2) + (((strchr(p,o2)-p)&0x30)>>4)) ^ crypt.gen_byte();
  82.        clear_string[j+1] =((((strchr(p,o2)-p)&0xf)<<4) + (((strchr(p,o3)-p)&0x3c)>>2)) ^ crypt.gen_byte();
  83.        clear_string[j+2] =((((strchr(p,o3)-p)&3) <<6 )+ (strchr(p,o4)-p)) ^ crypt.gen_byte();
  84.        j = j+3;
  85.        }
  86.  
  87.     cout << "Plain: " << clear_string << '\n';
  88.     cout << "Crypt: " << crypt_string << '\n';
  89.   }
  90. return 0;
  91. }
  92.