#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "time.h"
#define uint8_t unsigned char
#define uint16_t unsigned short int
#define uint32_t unsigned int
#define L_MOVE(c,n) ((c>>(8-n)) | (c<<n))
#define R_MOVE(c,n) ((c<<(8-n)) | (c>>n))
#define ROW_SIZE 128
void Get_Pk(void);
uint8_t Get_File_Config(void);
uint8_t FILE_PK[32]={0};
uint8_t File1_Way[128]={0};
uint8_t File2_Way[128]={0};
void main(void)
{
uint8_t Buf[ROW_SIZE+1]={0};
uint8_t i=0,n=0;
uint32_t File_Size=0;
FILE *Fp1;
FILE *Fp2;
// Get_Pk();
if(Get_File_Config()==0)
{
printf("配置文件错误!\n\t提示:main.txt必须与main.exe在统一文件夹下\n");
return ;
}
Fp1=fopen(File1_Way,"rb");
Fp2=fopen(File2_Way,"wb");
if(Fp1==NULL)
{
printf("文件1打开失败!\n\t提示:请假查该路径下文件是否存在\n");
return ;
}
if(Fp2==NULL)
{
printf("文件2打开/创建失败!\n");
return ;
}
while(1)
{
n = fread(Buf,1,ROW_SIZE,Fp1);
if(n==0)
break;
for(i=0;i<n;i++)
Buf[i] = L_MOVE(Buf[i],FILE_PK[i%32]);
n = fwrite(Buf,1,n,Fp2);
memset(Buf,0,sizeof(Buf));
File_Size = File_Size +n;
}
printf("本次加密文件大小:%d Byte\n\t%.2f KB\n",File_Size,(float)(File_Size)/1024);
fclose(Fp1);
fclose(Fp2);
printf("请输入随意字符结束!\n");
getch();
}
/* 生成32位随机数作为密码 */
void Get_Pk(void)
{
uint8_t i=0;
srand(time(NULL));
for(i=0;i<32;i++)
printf("%d,",rand()%8);
}
/* 获取配置信息 */
uint8_t Get_File_Config(void)
{
FILE *Fg;
uint8_t i,j=0,str=0;
uint8_t Row_Val[ROW_SIZE+1];
Fg = fopen(".\\main.txt","r");
if(Fg == NULL)
return 0;
while(fgets(Row_Val,ROW_SIZE,Fg)!=NULL)
{
for(i=0;i<ROW_SIZE;i++)
{
if(Row_Val[i] == ‘"‘)
{
str++;
j=0;
continue;
}
if(str == 1)
FILE_PK[j++]=Row_Val[i] - 0x30;
if(str == 3)
File1_Way[j++]=Row_Val[i];
if(str == 5)
File2_Way[j++]=Row_Val[i];
}
memset(Row_Val,0,sizeof(Row_Val));
}
fclose(Fg);
return 1;
}