循环移位的应用

Author: 上 海 孙逊 Date: 1993-11-05

        例如:O2H循环左移2位就变成08H,再循环左移6位就又恢复到O2H。
        由于文本文件中出现OOH和FFH的可能性较少,所以通过对文本文件中各字节进行循环移位就能够对文件实现相应的加密与解密。
        下面给出用TURBO C 2.0写的程序shifts.c。其中移位的位数i可在1~7之间任选。考虑到有的文件较长,不能一次全部读入内存,所以程序中对长度大于32K字节的文件进行了分块处理,每32K字节为一块,分批读入内存。本程序在8088上通过。
        /*shifts.c*/
        #include<stdlib.h>
        #include<alloc.h>
        #include<stdio.h>
        #include<io.h>
        main(intargc,char*argv[I)
        {
        inti,j,blocklength;
        longl;
        char*haed,*temp;
        FILE*f;
        if(argc!=2)
        {puts(“\tEnter:[path]shifts[path]filename”);exit(0);}
        if((f=fopen(argv[1],“r+b”))==NULL)
        {puts(“\tFilecan'topened”);exit(0);}do{
        puts(“\tEnterthenumber(1--7).\n\tPress0toexit.");
        scanf(“%d”,&i);
        }while(i>7||i<0);
        if(i==0)
        exit(0);
        l=filelength(f->fd);
        if(1<32767)
        blocklength=(int)l;
        else
        blocklength=32767;
        if((head=(char*)malloc(blocklength))==NULL)
        {puts(“outofmemory”);exit(0);}
        for(;l;l-=blocklength)
        {if(l<blocklength)}
        blocklength=(int)l;
        fread(head,l,blocklength,f);
        fseek(f,-blocklength,SEEK-CUR);
        for(temp=head;temp!=head+blocklength;++temp)
        {for(j=i;j>0;--j)
        if(*temp>=0)
        *temp<<=1;
        dlse{*temp<<=1;
        *temp+=1;
        }
        }
        fwrite(head,lblocklength,f);
        fseek(f,0,SEEK-CUR);
        }
        fclose(f);
        free(head);
        }
        (上海  孙逊)