循环位移加密二进制数据
#include "stdafx.h"
void encode(char * p, int n)
{
for (int i = 0; i<n; i++)
{
unsigned char ch = p[i];//将值赋给无符号的变量,方便之后移位的操作 (首位移动用0补位)
ch = ((ch << 1) | (ch >> 7));
p[i] = ch;
}
}
void decode(char *p, int n)
{
for (int i = 0; i<n; i++)
{
unsigned char ch = p[i];
ch = ((ch >> 1) | (ch << 7));
p[i] = ch;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
FILE* fpr = fopen("2.wmv", "rb"); //需要改相应文件
if (fpr == NULL)
return -1;
FILE* fpw = fopen("3.wmv", "wb"); //需要改相应文件
if (fpw == NULL)
return -1;
int n = 0; char buf[1024];
while ((n = fread((void*)buf, sizeof(char), 1024, fpr))>0)
{
decode(buf, n);//encode(buf, n);
fwrite((void*)buf, n, 1, fpw);
}
fclose(fpr);
fclose(fpw);
return 0;
}