下面两个pascal函数,想转换为c代码的,自己写了一下老是有问题,请教一下高手,如何转换翻译:const
EDC1 = 11; // 常量
EDC2 = 22; // 常量function Encrypt(S:String;Key:Word):String;
var
I,j:Integer;
begin
Result:=S;
for I:=1 to Length(S) do
begin
Result[I]:=char(byte(S[I]) xor (Key shr 8));
Key:=(byte(Result[I])+Key)*EDC1+EDC2
end;
S:=Result;
Result:='';
for I:=1 to Length(S) do
begin
j:=Integer(s[i]);
Result:=Result+Char(65+(j div 26))+Char(65+(j mod 26));
end;
end;
function Decrypt(S:String;Key:Word):String;
var
I,j:Integer;
begin
Result:='';
for I:=1 to (length(S) div 2) do
begin
j:=(Integer(S[2*i-1])-65)*26;
j:=j+(Integer(S[2*i])-65);
Result:=Result+Char(j);
end;
S:=Result;
for I:=1 to Length(S) do
begin
Result[I]:=Char(byte(S[I]) xor (Key shr 8));
Key:=(byte(S[I])+Key)*EDC1+EDC2;
end;
end;
EDC1 = 11; // 常量
EDC2 = 22; // 常量function Encrypt(S:String;Key:Word):String;
var
I,j:Integer;
begin
Result:=S;
for I:=1 to Length(S) do
begin
Result[I]:=char(byte(S[I]) xor (Key shr 8));
Key:=(byte(Result[I])+Key)*EDC1+EDC2
end;
S:=Result;
Result:='';
for I:=1 to Length(S) do
begin
j:=Integer(s[i]);
Result:=Result+Char(65+(j div 26))+Char(65+(j mod 26));
end;
end;
function Decrypt(S:String;Key:Word):String;
var
I,j:Integer;
begin
Result:='';
for I:=1 to (length(S) div 2) do
begin
j:=(Integer(S[2*i-1])-65)*26;
j:=j+(Integer(S[2*i])-65);
Result:=Result+Char(j);
end;
S:=Result;
for I:=1 to Length(S) do
begin
Result[I]:=Char(byte(S[I]) xor (Key shr 8));
Key:=(byte(S[I])+Key)*EDC1+EDC2;
end;
end;
#define EDC2 22
void Encrypt(char *S,Word Key,int len,char *result)
{
int i;
byte j;
CopyMemory(result,S,len);
for(i=0;i<len;i++){
result[i]=S[i]^(Key>>8);
Key=(byte(result[i])+Key)*EDC1+EDC2;
}
CopyMemory(S,result,len);
for(i=0;i<len;i++){
j=S[i];
result[2*i]=65+(j/26);
result[2*i+1]=65+(j % 26);
}
}void Decrypt(char *S,Word Key,int len,char *result)
{
int i;
byte j;
for(i=0;i<len/2;i++){
j=(S[2*i]-65)*26;
j+=S[2*i+1]-65;
result[i]=j;
}
CopyMemory(S,result,len);
for(i=0;i<len/2;i++){
result[i]=byte(S[i]^(Key>>8));
Key=(byte(S[i])+Key)*EDC1+EDC2;
}
}
c中没有String 类型,用char *,调用:
加密:
char result[100];
ZeroMemory(result,100);
Encrypt(Edit1->Text.c_str(),1000,Edit1->Text.Length(),result);
Label1->Caption=result;
解密:
char result[100];
ZeroMemory(result,100);
Decrypt(Label1->Caption.c_str(),1000,Label1->Caption.Length(),result);
Label2->Caption=result;