概述
用C语言实现一个简易的文件加密/解密系统。该系统采用对称加密体制,由用户指定自己的密钥key,加密函数可描述为M = P + key,对应的解密函数可描述为M = P - key。
代码如下:
#include <stdio.h>
#include <stdlib.h>
void encryption(char buffer[], int file_size, int key);
void decryption(char buffer[], int file_size, int key);
void Process(int a);
int openScrcFile(char **buffer);
void saveDstFile(char *buffer);
void menu();
char filename[20];
void main()
{
char flag;
menu();
flag = getchar();
getchar();
while(flag != 'Q' || flag != 'q')
{
switch(flag)
{
case 'e':
case 'E':
Process(0);
break;
case 'd':
case 'D':
Process(1);
break;
}
flag = getchar();
getchar();
}
if (flag == 'Q' || flag == 'q')
exit(0);
}
void menu()
{
printf("*********************************************************n");
printf("===========A STMPLE ENCRYPTION/DECRYPTION SYSTEM=========n");
printf(" ENCRYPTION press 'E' DECRYPTION press 'D' QUIT press 'Q'n");
printf("*********************************************************n");
}
int openScrcFile(char **buffer)
{
FILE *myfile_src;
//char filename[20];
long file_size;
printf("please input the path and filename of the file you want to processn");
scanf("%s", filename);
if (!(myfile_src = fopen(filename, "rb")))
{
printf("errorn");
}
fseek(myfile_src, 0, SEEK_END);
file_size = ftell(myfile_src);
fseek(myfile_src, 0, SEEK_SET);
*buffer = (char *)malloc(file_size);
fread(*buffer, 1, file_size, myfile_src);
fclose(myfile_src);
return file_size;
}
void saveDstFile(char *buffer, long file_size)
{
FILE *myfile_dst;
//printf("please input the path and filename of the file you have processedn");
//scanf("%s", filename);
if (!(myfile_dst = fopen(filename, "wb")))
{
printf("errorn");
}
fwrite(buffer, 1, file_size, myfile_dst);
printf("OK!n");
fclose(myfile_dst);
}
void Process(int a )
{
FILE *myfile_dst;
char * buffer;
int key;
long file_size;
file_size =openScrcFile(&buffer);
printf("please input the key (a integer) for encryption or decryptionn");
scanf("%d", &key);
if (a == 0)
{
encryption(buffer, file_size, key);
}
else
{
decryption(buffer, file_size, key);
}
saveDstFile(buffer, file_size);
}
void encryption(char buffer[], int file_size, int key)
{
int i;
for(i = 0; i < file_size; i ++)
{
buffer[i] = buffer[i] + key;
}
}
void decryption(char buffer[], int file_size, int key)
{
int i;
for(i = 0; i < file_size; i ++)
{
buffer[i] = buffer[i] - key;
}
}
最后
以上就是俊秀小甜瓜为你收集整理的简单的文件加密系统C语言代码的全部内容,希望文章能够帮你解决简单的文件加密系统C语言代码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复