#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

#define BLOCK_SIZE  10

void transition(FILE *fpi, FILE *fpo);

void main()
{
   FILE *fpi, *fblock, *funblock;

   clrscr(); /* Clear screen */
   if((fpi = fopen("plain.txt","r"))==NULL)  {
     printf("Can not Open File\n");
     exit(-1);
   }
   if((fblock = fopen("block.txt","w+"))==NULL)  {
     printf("Can not Write File\n");
     exit(-1);
   }
   if((funblock = fopen("unblock.txt","w"))==NULL)  {
     printf("Can not Write File\n");
     exit(-1);
   }
   transition(fpi, fblock);      /* Encrypt plain.txt to block.txt   */
   fseek(fblock, 0, SEEK_SET);
   transition(fblock, funblock); /* Decrypt block.txt to unblock.txt */
   fclose(fpi);
   fclose(fblock);
   fclose(funblock);
}

void transition(FILE *fpi, FILE *fpo)
{
   int  i, count;
   char buffer[BLOCK_SIZE], temp[BLOCK_SIZE];
   char ch;
   long fcur;

   fcur=ftell(fpi);
   fread(buffer, BLOCK_SIZE, 1, fpi);
   while( !feof(fpi) )
   {
      fcur=ftell(fpi);
      for(i=0; i<BLOCK_SIZE; i++)
        temp[i]=buffer[BLOCK_SIZE-i-1];
      fwrite(temp, BLOCK_SIZE, 1, fpo);
      fread(buffer, BLOCK_SIZE, 1, fpi);
   }

   if(fcur!=ftell(fpi))  {
     count=0;
     fseek(fpi,fcur,SEEK_SET);
     ch=getc(fpi);
     while(ch!=EOF) {
       buffer[count]=ch;
       ch=getc(fpi);
       count++;
     }
     for(i=0; i<count; i++)
       temp[i]=buffer[count-i-1];
     fwrite(temp, count, 1, fpo);
   }
}

