"Fossies" - the Fresh Open Source Software Archive

Member "libmcrypt-2.5.8/doc/example.c" (9 Mar 2002, 2721 Bytes) of package /linux/privat/old/libmcrypt-2.5.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "example.c" see the Fossies "Dox" file reference documentation.

    1 /* First example: Encrypts stdin to stdout using TWOFISH with 128 bit key and CFB */
    2 
    3 #include <mcrypt.h>
    4 #include <stdio.h>
    5 #include <stdlib.h>
    6 /* #include <mhash.h> */
    7 
    8 main() {
    9 
   10   MCRYPT td;
   11   int i;
   12   char *key;
   13   char password[20];
   14   char block_buffer;
   15   char *IV;
   16   int keysize=19; /* 128 bits */
   17 
   18   key=calloc(1, keysize);
   19   strcpy(password, "A_large_key");
   20 
   21 /* Generate the key using the password */
   22 /*  mhash_keygen( KEYGEN_MCRYPT, MHASH_MD5, key, keysize, NULL, 0, password, strlen(password));
   23  */
   24   memmove( key, password, strlen(password));
   25 
   26   td = mcrypt_module_open("twofish", NULL, "cfb", NULL);
   27   if (td==MCRYPT_FAILED) {
   28      return 1;
   29   }
   30   IV = malloc(mcrypt_enc_get_iv_size(td));
   31 
   32 /* Put random data in IV. Note these are not real random data, 
   33  * consider using /dev/random or /dev/urandom.
   34  */
   35 
   36   /*  srand(time(0)); */
   37   for (i=0; i< mcrypt_enc_get_iv_size( td); i++) {
   38     IV[i]=rand();
   39   }
   40 
   41   i=mcrypt_generic_init( td, key, keysize, IV);
   42   if (i<0) {
   43      mcrypt_perror(i);
   44      return 1;
   45   }
   46 
   47   /* Encryption in CFB is performed in bytes */
   48   while ( fread (&block_buffer, 1, 1, stdin) == 1 ) {
   49       mcrypt_generic (td, &block_buffer, 1);
   50 
   51 /* Comment above and uncomment this to decrypt */
   52 /*    mdecrypt_generic (td, &block_buffer, 1);  */
   53 
   54       fwrite ( &block_buffer, 1, 1, stdout);
   55   }
   56   mcrypt_generic_deinit(td);
   57 
   58   mcrypt_module_close(td);
   59 
   60   return 0;
   61 
   62 }
   63 
   64 #if 0
   65 /* Second Example: encrypts using CBC and SAFER+ with 192 bits key */
   66 
   67 #include <mcrypt.h>
   68 #include <stdio.h>
   69 #include <stdlib.h>
   70 
   71 main() {
   72 
   73   MCRYPT td;
   74   int i;
   75   char *key; /* created using mcrypt_gen_key */
   76   char *block_buffer;
   77   char *IV;
   78   int blocksize;
   79   int keysize = 24; /* 192 bits == 24 bytes */
   80 
   81 
   82   key = calloc(1, keysize);
   83   strcpy(key, "A_large_and_random_key"); 
   84 
   85   td = mcrypt_module_open("saferplus", NULL, "cbc", NULL);
   86 
   87   blocksize = mcrypt_enc_get_block_size(td);
   88   block_buffer = malloc(blocksize);
   89 /* but unfortunately this does not fill all the key so the rest bytes are
   90  * padded with zeros. Try to use large keys or convert them with mcrypt_gen_key().
   91  */
   92 
   93   IV=malloc(mcrypt_enc_get_iv_size(td));
   94 
   95 /* Put random data in IV. Note these are not real random data, 
   96  * consider using /dev/random or /dev/urandom.
   97  */
   98 
   99 /* srand(time(0)); */
  100   for (i=0; i < mcrypt_enc_get_iv_size(td); i++) {
  101     IV[i]=rand();
  102   }
  103 
  104   mcrypt_generic_init ( td key, keysize, IV);
  105 
  106   /* Encryption in CBC is performed in blocks */
  107   while ( fread (block_buffer, 1, blocksize, stdin) == blocksize ) {
  108       mcrypt_generic (td, block_buffer, blocksize);
  109 /*      mdecrypt_generic (td, block_buffer, blocksize); */
  110       fwrite ( block_buffer, 1, blocksize, stdout);
  111   }
  112   mcrypt_generic_end (td);
  113 
  114   return 0;
  115 
  116 }
  117 
  118 #endif