c#文件加密程序

来源:互联网 发布:seo公司哪家专业 编辑:程序博客网 时间:2024/05/17 01:41
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 1024
#define Success 1
#define Failed 0
#define LEN 300
typedef int state;


typedef struct
{
    char dirPath[LEN];
    char fileName[LEN];
    char new_file[LEN];


} fileInfo;


int count = 0;
//获取父目录、文件名
void getInfo(char *str, fileInfo ** info, int option)
{
    fileInfo * INFO = (fileInfo *)malloc(sizeof(fileInfo));
    memset(INFO, 0, sizeof(*INFO));
    int i;
    for(i=strlen(str); i>=0; i--)
    {
        if( str[i] == '\\')
        {
            strncpy(INFO->dirPath, str, i+1);
            strcpy(INFO->fileName, str+i+1);
            break;
        }


    }


    (option)?( sprintf(INFO->new_file, "%sybk-cyp_%s",INFO->dirPath,INFO->fileName))
    :( sprintf(INFO->new_file, "%sybk-dcy_%s",INFO->dirPath,INFO->fileName));


    *info = INFO;


}


//加密
state cyp(char *str, fileInfo * info)
{


    FILE *stream_org;
    FILE *stream_new;
    char buf[N] = {0};
    if (((stream_org = fopen(str, "rb")) == NULL)
            || ((stream_new = fopen(info->new_file, "wb")) == NULL))
    {
        printf("Cannot read or write file...\n");
        puts(str);
        return Failed;
    }
        fseek(stream_org, 0, SEEK_END);
        int len = ftell(stream_org);
        printf("%d\n",len);
   fseek(stream_org, 0, SEEK_SET);
    int record = - 1;
    while(len > 0)
    {
        record = fread(buf, 1,sizeof(buf),stream_org);


//        if(record == 0)
//        {
//            if(strlen(buf) < N)
//            {
//               // count += strlen(buf);
//                fwrite(buf, strlen(buf), 1, stream_new);
//              //  printf("pos1 count: %d\n",count);
//                fflush(stream_new);
//            }
//            break;
//        }
         if(record >0)
        {
            int i;
            for(i=0; i<record; i++)
            {
                buf[i] ^= 0x88;
            }
            //count += sizeof(buf);
            fwrite(buf, 1, strlen(buf), stream_new);
             //printf("pos2 count: %d\n",count);
            fflush(stream_new);
            memset(buf, 0, sizeof(buf));
            len -= record;
            printf("%d\n",len);
        }
        else if(record ==0)
        {
            break;
        }
        else
        {
            printf("fread() failed.\n");
            return Failed;
        }


    }
    printf("%d\n",len);
    fclose(stream_new);
    fclose(stream_org);


    return Success;


}






int main(int argc, char *argv[])
{




    if(argc < 3)
    {
        printf("Usage:%s [-c|-d] filename\n",argv[0]);
        return 1;
    }
    else
    {
        char str[LEN] = {0};
        strcpy(str, argv[2]);
        puts(str);
        fileInfo *info;


        switch(argv[1][1])
        {
        case 'c':
        {
            getInfo(str, &info, 1);
            if ( cyp(argv[2], info) == Success )
            {


                printf("Encrypted Successfully!\n");
                return 0;
            }
            else
            {


                printf("Encrypted Failed!\n");
                return 0;
            }
        }
        break;
        case 'd':
        {
            getInfo(str, &info, 0);
            if ( cyp(argv[2], info) == Success )
            {


                printf("Decrypted Successfully!\n");
                return 0;
            }
            else
            {


                printf("Decrypted Failed!\n");
                return 0;
            }
        }
        break;
        default:
            printf("Unknow Args!\n");
            return 0;
        }
    }
}



原创粉丝点击