home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Security / Security.zip / bfish163.zip / SAMPLE2.C < prev    next >
C/C++ Source or Header  |  1998-01-08  |  2KB  |  68 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // SAMPLE2.C - Example of Blowfish API call, using System linkage. 
  4. //             Also demonstrates use of a random chaining seed, and the 
  5. //             compression interface.
  6. //
  7. // Compile this, and link with ENCRYPT.LIB to use encryption functions in DLL
  8. // (or link with ENCRYPT.OBJ to build stand-alone .EXE).
  9. //
  10. // Matthew Spencer, 23/5/96
  11. // Updated 06/01/98, for version 1.62
  12. //
  13. ///////////////////////////////////////////////////////////////////////////////
  14. #include <stdio.h>    // for printf()
  15. #include <string.h>   // for strlen()
  16. #include <stdlib.h>   // for free()
  17.  
  18. #define BF_SYS_LINKAGE // to force "_System" linkage - NO EXTRA CHANGES REQUIRED
  19. #include "encrypt.h"
  20.  
  21. int main(int argc, char * argv[])
  22. {
  23.    unsigned char text[] = "Example_Example_Example_Example_Example";
  24.    unsigned char key[]  = "A sample key.";
  25.    unsigned char seed[8];  
  26.    unsigned char * comp_text, * decomp_text;
  27.    unsigned long comp_len, decomp_len;
  28.  
  29.    // version 1.62: setup Chaining mode, and use a random IV (see blowfish.doc)
  30.    SetChainingMode(ReInitialise);           
  31.    SetChainingSeed(GetRandomBlock(&seed));  
  32.  
  33.    // intialise
  34.    Initialise(key, strlen(key));   // call the _System linkage function
  35.  
  36.    // display original text
  37.    printf("\nThe original text (%d bytes) is:  >>%s<<\n\n", strlen(text), text);
  38.  
  39.    // compress it.
  40.    comp_len = Compress(text, strlen(text), &comp_text);
  41.    comp_text[comp_len] = '\0';  // to help printf
  42.    printf("After compression, the text is:   >>%s<<  (%ld bytes)\n\n", comp_text, comp_len);
  43.  
  44.    // call encryption
  45.    EncryptBlock(comp_text, comp_len);          
  46.    printf("After encryption, the text is:    >>%s<<\n\n", comp_text);
  47.  
  48.    // call decryption - should be the same as previous
  49.    DecryptBlock(comp_text, comp_len);
  50.    printf("After decryption, the text is:    >>%s<<\n\n", comp_text);
  51.  
  52.    // decompress it.
  53.    decomp_len = Decompress(comp_text, comp_len, &decomp_text);
  54.    decomp_text[decomp_len] = '\0';  // to help printf
  55.    printf("After decompression, the text is: >>%s<<\n\n", decomp_text);
  56.  
  57.    // free the allocated memory
  58.    #ifndef __DEBUG_ALLOC__
  59.       free(comp_text);  
  60.       free(decomp_text);
  61.    #else
  62.       _debug_free(comp_text, "sample2.c", 1);  
  63.       _debug_free(decomp_text, "sample2.c", 2);
  64.    #endif
  65.  
  66.    return 0;
  67. }
  68.