文件加解密,文件操作



1fseek,ftell,fread,fwrite(简单文件加密)

#define _CRT_SECURE_NO_WARNINGS  //去掉安全检查

#include

#include

 

/*读取文件大小*/

int getfilesize(char *path)

{

    FILE *pf = fopen(path, "r");

    if (pf == NULL)

    {

        fclose(pf);

        return -1;

    }

    else

    {

        fseek(pf,0,SEEK_END);

        int length = ftell(pf);

        //获取文件大小

        return length;

    }

    fclose(pf);

}

 

/*实现文件复制*/

void copy(char *oldpath,char *newpath)

{

    FILE *pfr, *pfw;

    //以打开二进制文件的方式打开

    pfr = fopen(oldpath, "rb");

    //写入二进制模式

    pfw = fopen(newpath, "wb");

    if (pfr == NULL || pfw == NULL)

    {

        fclose(pfr);   //关闭文件

        fclose(pfw);

        return;

    }

    else

    {

        int length = getfilesize(oldpath);

        //分配内存,读取文件

        char *p = (char *)malloc(length * sizeof(char));

        //读取二进制到内存

        fread(p,sizeof(char),length,pfr);

        //写入二进制到文件

        fwrite(p,sizeof(char),length,pfw);

       

        //关闭文件

        fclose(pfr);

        fclose(pfw);

    }

}

 

void encryptyfile(char *oldpath,char *newpath)

{

    FILE *pfr, *pfw;

    pfr = fopen(oldpath, "rb");

    //写入二进制模式

    pfw = fopen(newpath, "wb");

    if (pfr == NULL || pfw == NULL)

    {

        //关闭文件

        fclose(pfr);

        fclose(pfw);

        return;

    }

    else

    {

        int length = getfilesize(oldpath);

        //分配内存,读取文件

        char *p = (char *)malloc(length*sizeof(char));

        //读取二进制到内存

        fread(p,sizeof(char),length,pfr);

        for (int i = 0; i < length;i++)

        {

            //加密方法是,与制定字符进行异或操作

            p[i] ^= 'A';

        }

        //写入二进制到文件

        fwrite(p, sizeof(char), length, pfw);

 

        //关闭文件

        fclose(pfr);

        fclose(pfw);

    }

}

 

/*解密文件*/

void decodefile(char *oldpath,char *newpath)

{

    FILE *pfr, *pfw;

    pfr = fopen(oldpath, "rb");

    //写入二进制模式

    pfw = fopen(newpath,"wb");

    if (pfr == NULL || pfw == NULL)

    {

        fclose(pfr);

        fclose(pfw);

        return;

    }

    else

    {

        int length = getfilesize(oldpath);

        //分配内存,读取文件

        char *p = (char *)malloc(length*sizeof(char));

        //读取二进制到内存

        fread(p,sizeof(char),length,pfr);

        int i;

        for (i = 0; i < length;i++)

        {

            p[i] ^= 'A';

        }

        //写入二进制到文件

        fwrite(p, sizeof(char), length, pfw);

    }

    //关闭文件

    fclose(pfr);

    fclose(pfw);

}

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

{

    char *path1 = "G:\\1.txt";

    char *path2 = "G:\\2.txt";

    char *path3 = "G:\\3.txt";

 

    encryptyfile(path1, path2);

    decodefile(path2, path3);

    //printf("%d\n",getfilesize(path1));

 

    system("pause");

    return 0;

}

上面的过程将会把加解密的文件输出到文件中。

#define _CRT_SECURE_NO_WARNINGS

#include

#include

 

char jiami(char ch)

{

    return ch ^ 123;

}

 

char jiemi(char ch)

{

    return ch ^ 123;

}

 

void jia(char *path, char *pathjia)

{

    FILE *pfr, *pfw;

    pfr = fopen(path, "r");//读取

    pfw = fopen(pathjia, "w");//写入

    if (pfr == NULL || pfw == NULL)

    {

        fclose(pfw);

        fclose(pfr);

        return;

    }

    else

    {

        //feof(FILE *pf) 到了文件末尾1,没有到就是0

        //下面的过程将把文件内容输出到屏幕上。

        while (!feof(pfr))

        {

            //读取字符

            char ch = fgetc(pfr);

            putchar(ch);

            //输出字符的时候将字符加密

            fputc(jiami(ch), pfw);

        }

    }

    fclose(pfr);

    fclose(pfw);//关闭文件

}

 

/*解密*/

void jie(char *path,char *pathjie)

{

    FILE *pfr, *pfw;

    //读取

    pfr = fopen(path, "r");

    //写入

    pfw = fopen(pathjie,"w");

    if (pfr == NULL || pfw == NULL)

    {

        fclose(pfr);

        fclose(pfw);

        return;

    }

    else

    {

        //到了文件末尾1,没有到就是0

        while (!feof(pfr))

        {

            char  ch = fgetc(pfr);//读取字符

            putchar(ch);

            fputc(jiemi(ch), pfw);//写入一个加密的字符

        }

    }

    fclose(pfr);

    //关闭文件

    fclose(pfw);

}

 

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

{

    char *path1 = "G:\\1.txt";

    char *path2 = "G:\\2.txt";

    char *path3 = "G:\\3.txt";

 

    //jia(path1, path2);

    jie(path2, path3);

 

    system("pause");

    return 0;

}

 

 

 

密码加密

头文件:

#define _CRT_SECURE_NO_WARNINGS

#include

#include

#include

 

//字符串加密

char * stringjiami(char *password, char *string);

//字符串解密

char * stringjiemi(char *password, char *jiastring);

void filejiami(char *path, char *pathjia, char *password);

void filejiemi(char *pathjia, char *pathjie, char *password);

 

 

 

 

 

#include "encrytiondecrytion.h"

 

int getfilesize(char *path)

{

    FILE *pf = fopen(path,"r");

    if (pf == NULL)

    {

        return -1;

    }

    else

    {

        fseek(pf, 0, SEEK_END);//文件末尾

        int length = ftell(pf);

        return length;//返回长度

    }

}

 

char * stringjiami(char *password, char *string)

{

    //获取加密长度

    int passlength = strlen(password);

    //获取字符串长度

    int stringlength = strlen(string);

    if (stringlength % passlength == 0)

    {

        //循环次数

        int ci = stringlength / passlength;

        int i,j;

        for (i = 0; i < ci;i++)   //循环次数

        {

            //循环密码

            for (j = 0; j < passlength;j++)

            {

                string[passlength*i + j] ^= password[j];

            }

        }

    }

    else

    {

        int ci = stringlength / passlength; //循环次数

        int i, j;

        for (int i = 0; i < ci;i++)

        {

            //循环密码

            for (j = 0; j < passlength;j++)

            {

                string[passlength * i + j] ^= password[j];

            }

        }

        int lastlength = stringlength % passlength;//剩下的长度

        for (int i = 0; i < lastlength;i++)

        {

            string[passlength*(stringlength / passlength) + i] ^= password[i];

        }

    }

    return string;

}

 

//字符串解密

char * stringjiemi(char *password, char *jiastring)

{

    //获取加密长度

    int passlength = strlen(password);

    //获取字符串长度

    int stringlength = strlen(jiastring);

    if (stringlength %passlength == 0)

    {

        int ci = stringlength / passlength;//循环次数

        int i, j;

        for (i = 0; i < ci; i++)

        {

            for (j = 0; j < passlength;j++)

            {

                //异或加密

                jiastring[passlength * i + j] ^= password[j];

            }

        }

    }

    else

    {

        //循环次数

        int ci = stringlength / passlength;

        int i, j;

        for (i = 0; i < ci;i++)

        {

            //循环密码

            for (j = 0; j < passlength;j++)

            {

                //异或加密

                jiastring[passlength*i + j] ^= password[j];

            }

        }

        //剩下的长度

        int lastlength = stringlength % passlength;

        for (int i = 0; j < lastlength;i++)

        {

            jiastring[passlength*(stringlength / passlength) + i] ^= password[i];

        }

    }

    return jiastring;

}

 

void filejiami(char *path, char *pathjia, char *password)

{

    FILE *pfr, *pfw;

    pfr = fopen(path, "r");

    pfw = fopen(pathjia,"w");

    if (pfr == NULL || pfw == NULL)

    {

        fclose(pfr);

        fclose(pfw);

        return;

    }

    else

    {

        int length = getfilesize(path);

        char *newstr = (char*)malloc(sizeof(char)*(length + 1));

        for (int i = 0; i < length;i++)

        {

            char ch = fgetc(pfr);  //获取一个字符

            newstr[i] = ch;//不断存入字符

        }

        //字符串处理为'\0'

        newstr[length] = '\0';

        //加密字符串

        stringjiami(password, newstr);

 

        int i;

        for (i = 0; i < length;i++)

        {

            //挨个写入字符

            fputc(newstr[i], pfw);

        }

    }

    fclose(pfr);

    //关闭文件

    fclose(pfw);

}

 

main.c

#define _CRT_SECURE_NO_WARNINGS

#include

#include

#include "encrytiondecrytion.h"

 

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

{

    char string[50] = "锄禾日当午";

    char *password = "123";

    printf("%s\n",stringjiami(password,string));

    printf("%s\n", stringjiami(password, string));

 

    char *path1 = "G:\\1.txt";

    char *path2 = "G:\\2.txt";

    char *path3 = "G:\\3.txt";

 

    filejiami(path1, path2, "ABCDE");

    filejiami(path2, path3, "ABCDE");

 

    system("pause");

}

 

fscanf

#define _CRT_SECURE_NO_WARNINGS

#include

#include

 

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

{

    char str[100] = { 0 };

    fscanf(stdin, "%s", str);

    fprintf(stdout, "str=%s\n", str);

 

    system(str);

    return 0;

}

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(#,C编程)