home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / data / cipher / cipher.cpp next >
Text File  |  1996-10-29  |  3KB  |  72 lines

  1. //************************************************************
  2. // Data Types - Using an IString as a Data Buffer
  3. //
  4. // Copyright (C) 1994, Law, Leong, Love, Olson, Tsuji.
  5. // Copyright (c) 1997 John Wiley & Sons, Inc. 
  6. // All Rights Reserved.
  7. //
  8. // DESCRIPTION:                                                              
  9. //   This program implements a simplistic file encryption/decryption scheme. 
  10. //   It simply XORs the contents of cin using the argument "key" string and  
  11. //   writes the result to cout.                                              
  12. //                                                                           
  13. //   Usage to cipher:  cipher "secret key" < inputfile > outputfile                        
  14. //       to uncipher:  cipher "secret key" < outputfile
  15. //                                                                           
  16. //************************************************************
  17. #include <istring.hpp>
  18. #include <iostream.h>
  19. #include <stdio.h>
  20.  
  21. int main ( int argc, char *argv[] )
  22.   {
  23.   int
  24.     result = 0;
  25.  
  26.   /*----------------------------------------------------------------------------
  27.   | Get key.                                                                   |
  28.   ----------------------------------------------------------------------------*/
  29.   IString
  30.     key( argv[1] );
  31.  
  32.   /*----------------------------------------------------------------------------
  33.   | Ensure key was specified.                                                  |
  34.   ----------------------------------------------------------------------------*/
  35.   if ( key.length() )
  36.     {
  37.     /*--------------------------------------------------------------------------
  38.     | Put input and output files in binary mode.                               |
  39.     --------------------------------------------------------------------------*/
  40.     if ( !freopen( "", "rb", stdin )
  41.          ||
  42.          !freopen( "", "wb", stdout ) )
  43.       {
  44.       cerr << "Error opening input/output files.\a" << endl;
  45.       result = 2;
  46.       }
  47.     else
  48.       {
  49.       /*------------------------------------------------------------------------
  50.       | Read from stdin, XOR the bytes with the key, and write to stdout.      |
  51.       ------------------------------------------------------------------------*/
  52.       IString
  53.         buffer( 0, 4096 ); // Allocate 4K buffer.
  54.       while ( true )
  55.         {
  56.         size_t
  57.           n = fread( (char*)buffer, 1, buffer.length(), stdin );
  58.         buffer ^= key;
  59.         fwrite( (char*)buffer, 1, n, stdout );
  60.         if ( n < buffer.length() )
  61.           break;
  62.         }
  63.       }
  64.     }
  65.   else
  66.     {
  67.     cerr << "You must specify a key!\a" << endl;
  68.     result = 1;
  69.     }
  70.   return result;
  71.   }
  72.