cryptest

Cryptography-related scribbles
git clone git://git.finwo.net/misc/cryptest
Log | Files | Refs | README

decrypt.c (824B)


      1 #ifdef __cplusplus
      2 extern "C" {
      3 #endif
      4 
      5 #include <stdio.h>
      6 
      7 #include "base.h"
      8 
      9 int decrypt( unsigned int mult, unsigned int modulo, unsigned int padding ) {
     10 
     11   // Initialize variables
     12   unsigned long long seed = 0;
     13   unsigned long long obuf = 0;
     14   int obuf_len = 0;
     15   int i, c;
     16 
     17   // Read padding & build seed
     18   padding--;
     19   seed = fgetc(stdin);
     20   while( padding-- ) {
     21     seed = seed * mult;
     22     seed = seed % modulo;
     23     c = (seed%256) ^ fgetc(stdin);
     24     seed ^= c;
     25   }
     26 
     27   while( 1 ) {
     28 
     29     // Handle stdin
     30     c = fgetc(stdin);
     31     if(c==EOF) break;
     32     seed = ((seed*mult)+c)%modulo;
     33     obuf = (obuf%(1<<obuf_len))<<7;
     34     obuf |= seed%128;
     35     obuf_len += 7;
     36     if(obuf_len>=8) {
     37       fputc( (obuf>>(obuf_len-8))%256, stdout );
     38       obuf_len-=8;
     39     }
     40   }
     41 
     42   return 0;
     43 }
     44 
     45 #ifdef __cplusplus
     46 } // extern "C"
     47 #endif