#include
#define BufLength 100
void Encrypted_Decrypt(char* filepath, char* sec, int seclegth)
{
FILE * file = fopen(filepath, "r+");
if (NULL == file)
{
std::cout << "打开文件出错" << std::endl;
return;
}
char buffer[BufLength];
char secret[BufLength];
int n = 0;
while ((n = fread(buffer, 1, BufLength, file))> 0)
{
for (int i = 0; i < n; i++)
secret[i] = buffer[i] ^ sec[i % (seclegth + 1)];//加密
fseek(file, -n, SEEK_CUR);//移动字符串头在文件中的位置
fwrite(secret, 1, n, file);//覆盖写入秘文
fseek(file, n, SEEK_CUR);//移动字符串尾在文件中的位置,下次循环读取下一串
}
fclose(file);
}
int main()
{
std::cout << "输入密码:";
char psw[256];
std::cin >> psw;
std::cout << "加密或者解密文件(全路径如d:/1.txt):";
char filepath[256];
std::cin >> filepath;
Encrypted_Decrypt(filepath, psw, strlen(psw));
return 0;
}