function UncrypKey(Src: string; Key: string): string;
var
KeyLen: Integer;
KeyPos: Integer;
offset: Integer;
dest: string;
SrcPos: Integer;
SrcAsc: Integer;
TmpSrcAsc: Integer;
begin
KeyLen := Length(Key);
if KeyLen = 0 then
key := 'FLDZ';
KeyPos := 0;
offset := StrToInt('$' + copy(src, 1, 2));
SrcPos := 3;
repeat
SrcAsc := StrToIntDef('$' + copy(src, SrcPos, 2), 0);
if KeyPos < KeyLen then
KeyPos := KeyPos + 1
else
KeyPos := 1;
TmpSrcAsc := SrcAsc xor Ord(Key[KeyPos]);
if TmpSrcAsc <= offset then
TmpSrcAsc := 255 + TmpSrcAsc - offset
else
TmpSrcAsc := TmpSrcAsc - offset;
dest := dest + chr(TmpSrcAsc);
offset := srcAsc;
SrcPos := SrcPos + 2;
until SrcPos >= Length(Src);
Result := Dest;
end;
var
KeyLen: Integer;
KeyPos: Integer;
offset: Integer;
dest: string;
SrcPos: Integer;
SrcAsc: Integer;
TmpSrcAsc: Integer;
begin
KeyLen := Length(Key);
if KeyLen = 0 then
key := 'FLDZ';
KeyPos := 0;
offset := StrToInt('$' + copy(src, 1, 2));
SrcPos := 3;
repeat
SrcAsc := StrToIntDef('$' + copy(src, SrcPos, 2), 0);
if KeyPos < KeyLen then
KeyPos := KeyPos + 1
else
KeyPos := 1;
TmpSrcAsc := SrcAsc xor Ord(Key[KeyPos]);
if TmpSrcAsc <= offset then
TmpSrcAsc := 255 + TmpSrcAsc - offset
else
TmpSrcAsc := TmpSrcAsc - offset;
dest := dest + chr(TmpSrcAsc);
offset := srcAsc;
SrcPos := SrcPos + 2;
until SrcPos >= Length(Src);
Result := Dest;
end;
copy -> strncpystrtoint之类的,可以用sscanf去实现,还有很多其他方法
照着翻译就可以了,--> 看懂了后用 c 实现
#include <stdio.h>//新增加
#include <assert.h>//新增加
#include <string.h> //新增char *substr(char *destination, const char *source, int start, int length) {
assert((destination != NULL) && (source != NULL));
assert(start > 0 && length > 0);
assert((start + length) <= strlen(source)); char *tmpAddress = destination;
int i;
int end = start + length;
for (i = start; i < end; i++) {
*destination++ = *(source + i);
}
*destination = '\0';
return tmpAddress;
}char *UncrypKey ( char* Src, char* Key )
{
int KeyLen;
int KeyPos;
int offset;
char* dest;
int SrcPos;
int SrcAsc;
int TmpSrcAsc;
char tmp[1024]; /*自定义一个临时变量*/
do
{
memset( tmp, 0x00, sizeof(tmp) );
KeyLen = strlen(Key);
if( KeyLen == 0 )
{
strcpy( Key, "FLDZ" );
KeyPos = 0;
// strncpy(tmp,Src,2);
sprintf( tmp, "$%s", strncpy(tmp,Src,2) );
offset = (int)(tmp);
SrcPos = 3;
}
sprintf(tmp, "$%s", substr(tmp,Src,SrcPos,2) );
SrcAsc= (int)(tmp);
// substr(SrcAsc,Src,SrcPos,2);
if( KeyPos < KeyLen )
{
KeyPos = KeyPos + 1;
}
else
{
KeyPos = 1;
}
TmpSrcAsc = SrcAsc ^(int)Key[KeyPos];
if( TmpSrcAsc <= offset )
{
TmpSrcAsc = 255 + TmpSrcAsc - offset;
}
else
{
TmpSrcAsc = TmpSrcAsc - offset;
}
dest = dest + char(TmpSrcAsc);
offset = SrcAsc;
SrcPos = SrcPos + 2;
}while( SrcPos >= strlen(Src) );return dest;
}
int _tmain(int argc, _TCHAR* argv[])
{char tmp1[4]={'6','A','D','8'};
char tmp2[2]={' ',' '};
UncrypKey(tmp1,tmp2 );
return 0;
}调试的时候有问题,请问哪里出错
char tmp2[2]={' ',' '};
定义有问题,没有结束符的空间sprintf(tmp, "$%s", substr(tmp,Src,SrcPos,2) );
SrcPos值未知
char tmp2[3]={' ',' ','\0'};
char*a,*b;
a=tmp1;
b=tmp2;
b=UncrypKey(a,b );还是错误