文件加密算法如下:procedure EncryptFile(INFName, OutFName : String; Key : Word);
VAR
MS, SS : TMemoryStream;
X : Integer;
C : Byte;
begin
MS := TMemoryStream.Create;
SS := TMemoryStream.Create;
TRY
MS.LoadFromFile(INFName);
MS.Position := 0;
FOR X := 0 TO MS.Size - 1 DO
begin
MS.Read(C, 1);
C := (C xor (Key shr 8));
Key := (C + Key) * C1 + C2;
SS.Write(C,1);
end;
SS.SaveToFile(OutFName);
FINALLY
SS.Free;
MS.Free;
end;
end;
VAR
MS, SS : TMemoryStream;
X : Integer;
C : Byte;
begin
MS := TMemoryStream.Create;
SS := TMemoryStream.Create;
TRY
MS.LoadFromFile(INFName);
MS.Position := 0;
FOR X := 0 TO MS.Size - 1 DO
begin
MS.Read(C, 1);
C := (C xor (Key shr 8));
Key := (C + Key) * C1 + C2;
SS.Write(C,1);
end;
SS.SaveToFile(OutFName);
FINALLY
SS.Free;
MS.Free;
end;
end;
Key := (C + Key) * C1 + C2;
关键是上面两句,xor是异或,shr是什么?
c1,c2又是什么?
C1=55555;
C2=22222;
shr是右位移我也搞不懂这两行,word和byte类型的变量用什么替代呢?
我试过用char替代,但是不能做位操作和相加
{
FILE *MS, *SS;
int X;
unsigned char C, C1, C2;
if((MS = fopen(INFName, "rb")) == NULL)
{
printf("%s File not find!\n", INFName);
return;
}
if((SS = fopen(OutFName, "rw")) == NULL)
{
printf("%s File not create!\n", OutFName);
return;
}
while((C = fgetc(MS)) != EOF)
{
C ^= Key >> 8;
Key = (Key + C) * C1 + C2; // C1,C2干什么的?
fputc(C, SS);
}
fclose(SS);
fclose(MS);
}
#define C1 55555
#define C2 22222
void EncryptFile(char *INFName, char *OutFName, unsigned short Key)
{
FILE *MS, *SS;
int X;
unsigned char C;
if((MS = fopen(INFName, "rb")) == NULL)
{
printf("%s File not find!\n", INFName);
return;
}
if((SS = fopen(OutFName, "rw")) == NULL)
{
printf("%s File not create!\n", OutFName);
return;
}
while((C = fgetc(MS)) != EOF)
{
C ^= Key >> 8;
Key = (Key + C) * C1 + C2; // C1,C2干什么的?
fputc(C, SS);
}
fclose(SS);
fclose(MS);
}