DELPHI 加密原代码,支持任意长度的密码,16字节密码加密7兆文件只需半秒
function Exchange(desaddr : pointer ; souaddr : pointer ; len : word ; toright : bool) : bool;
var
mypbyte1 : pbyte;
mypbyte2 : pbyte;
mybyte : byte;
i : integer;
begin
result := false;
if (len < 1) then exit;
if (dword(desaddr) = 0) then exit;
if (dword(souaddr) = 0) then exit;
if (dword(desaddr) = dword(souaddr)) then exit;
if (toright = true) then begin
i := 0;
end else begin
i := len - 1;
end;
while (i >= 0) and (i <= len - 1) do begin
mypbyte1 := pbyte(dword(desaddr) + i);
mypbyte2 := pbyte(dword(souaddr) + i);
mybyte := mypbyte1^;
mypbyte1^ := mypbyte2^;
mypbyte2^ := mybyte;
if (toright = true) then begin
i := i + 1;
end else begin
i := i - 1;
end;
end;
end;
function CountBinI(myaddr : pointer ; len : word) : dword;
var
mypbyte : pbyte;
myi : dword;
i : integer;
j : integer;
begin
result := 0;
if (len < 1) then exit;
if (dword(myaddr) = 0) then exit;
i := 0;
while (i >= 0) and (i <= len - 1) do begin
mypbyte := pbyte(dword(myaddr) + i);
myi := 1;
j := 0;
while (j >= 0) and (j <= 7) do begin
if (myi and mypbyte^ > 0) then result := result + 1;
myi := myi shl 1;
j := j + 1;
end;
i := i + 1;
end;
end;
function NotBinI( desaddr : pointer ; souaddr : pointer ; len : word ) : bool;
var
mypbyte1 : pbyte;
mypbyte2 : pbyte;
mybyte : byte;
myi : byte;
i : integer;
j : integer;
begin
result := false;
if (len < 1) then exit;
if (dword(desaddr) = 0) then exit;
if (dword(souaddr) = 0) then exit;
i := 0;
while (i >= 0) and (i <= len - 1) do begin
mypbyte1 := pbyte(dword(desaddr) + i);
mypbyte2 := pbyte(dword(souaddr) + i);
mybyte := 0;
myi := 1;
j := 0;
while (j >= 0) and (j <= 7) do begin
if (myi and mypbyte2^ = 0) then begin
mybyte := mybyte + (myi and mypbyte1^);
end else begin
mybyte := mybyte + myi - (myi and mypbyte1^);
end;
myi := myi shl 1;
j := j + 1;
end;
mypbyte1^ := mybyte;
i := i + 1;
end;
result := true;
end;
function RO( myaddr : pointer ; len : word ; rocount : word ; toright : bool ) : bool;
label bac1;
label bac2;
label poi1;
label poi2;
label poi3;
label poi4;
var
myaddress : dword;
myflag : dword;
begin
result := false;
if (len < 1) or (rocount < 1) then exit;
myaddress := dword(myaddr);
myflag := 0;
if (toright = true) then myflag := 1;
asm
push eax
push ebx
push ecx
push edx
push esi
push edi
mov esi , myflag
xor edx , edx
mov dx , rocount
mov edi , edx
bac1:
mov ebx , myaddress
test esi , esi
jne poi1
xor eax , eax
mov ax , len
add ebx , eax
dec ebx
poi1:
push ebx
xor ecx , ecx
mov cx , len
xor dx , dx
bac2:
xor ax , ax
test esi , esi
je poi2
mov ah , [ebx]
shr ax , 1
or ah , dl
mov dl , al
mov [ebx] , ah
inc ebx
jmp poi3
poi2:
mov al , [ebx]
shl ax , 1
or al , dl
mov dl , ah
mov [ebx] , al
dec ebx
poi3:
dec ecx
cmp ecx , 1
jge bac2
pop ebx
mov dh , [ebx]
or dh , dl
mov [ebx] , dh
dec edi
cmp edi , 1
jge bac1
pop edi
pop esi
pop edx
pop ecx
pop ebx
pop eax
end;
result := true;
end;
function RemoteEnCode( codaddr : pointer ; codlen : word ; keyaddr : pointer ; keylen : word ) : bool;
var
myaddr1 : dword;
myaddr2 : dword;
myaddr3 : dword;
myaddr4 : dword;
myshr : byte;
i : integer;
j : integer;
begin
result := false;
myaddr1 := dword(codaddr);
myaddr2 := dword(keyaddr);
if ( myaddr1 = 0 ) then exit;
if ( myaddr2 = 0 ) then exit;
if (codlen < 8) then exit;
if (keylen < 8) then exit;
i := 0;
while (i >= 0) and (i <= keylen - 2) do begin
j := 0;
while (j >= 0) and (j <= codlen - 2) do begin
myaddr1 := dword(keyaddr) + i;
myaddr2 := dword(keyaddr) + i + 1;
myaddr3 := dword(codaddr) + j;
myaddr4 := dword(codaddr) + j + 1;
NotBinI( pointer(myaddr1) , @j , 1 );
NotBinI( pointer(myaddr2) , @j , 1 );
myshr := CountBinI(pointer(myaddr1) , 2);
RO( pointer(myaddr1) , 2 , myshr , true );
NotBinI( pointer(myaddr3) , pointer(myaddr1) , 1 );
NotBinI( pointer(myaddr4) , pointer(myaddr2) , 1 );
myshr := CountBinI(pointer(myaddr3) , 2);
RO( pointer(myaddr3) , 2 , myshr , true );
Exchange(pointer(myaddr3) , pointer(myaddr4) , 1 , true);
j := j + 1;
end;
i := i + 1;
end;
i := keylen - 2;
while (i >= 0) and (i <= keylen - 2) do begin
j := codlen - 2;
while (j >= 0) and (j <= codlen - 2) do begin
myaddr1 := dword(keyaddr) + i;
myaddr2 := dword(keyaddr) + i + 1;
myshr := CountBinI(pointer(myaddr1) , 2);
RO( pointer(myaddr1) , 2 , myshr , false );
NotBinI( pointer(myaddr1) , @j , 1 );
NotBinI( pointer(myaddr2) , @j , 1 );
j := j - 1;
end;
i := i - 1;
end;
result := true;
end;
function RemoteDeCode( codaddr : pointer ; codlen : word ; keyaddr : pointer ; keylen : word ) : bool;
var
myaddr1 : dword;
myaddr2 : dword;
myaddr3 : dword;
myaddr4 : dword;
myshr : byte;
i : integer;
j : integer;
begin
result := false;
myaddr1 := dword(codaddr);
myaddr2 := dword(keyaddr);
if ( myaddr1 = 0 ) then exit;
if ( myaddr2 = 0 ) then exit;
if (codlen < 8) then exit;
if (keylen < 8) then exit;
i := 0;
while (i >= 0) and (i <= keylen - 2) do begin
j := 0;
while (j >= 0) and (j <= codlen - 2) do begin
myaddr1 := dword(keyaddr) + i;
myaddr2 := dword(keyaddr) + i + 1;
NotBinI( pointer(myaddr1) , @j , 1 );
NotBinI( pointer(myaddr2) , @j , 1 );
myshr := CountBinI(pointer(myaddr1) , 2);
RO( pointer(myaddr1) , 2 , myshr , true );
j := j + 1;
end;
i := i + 1;
end;
i := keylen - 2;
while (i >= 0) and (i <= keylen - 2) do begin
j := codlen - 2;
while (j >= 0) and (j <= codlen - 2) do begin
myaddr1 := dword(keyaddr) + i;
myaddr2 := dword(keyaddr) + i + 1;
myaddr3 := dword(codaddr) + j;
myaddr4 := dword(codaddr) + j + 1;
Exchange(pointer(myaddr3) , pointer(myaddr4) , 1 , false);
myshr := CountBinI(pointer(myaddr3) , 2);
RO( pointer(myaddr3) , 2 , myshr , false );
NotBinI( pointer(myaddr3) , pointer(myaddr1) , 1 );
NotBinI( pointer(myaddr4) , pointer(myaddr2) , 1 );
myshr := CountBinI(pointer(myaddr1) , 2);
RO( pointer(myaddr1) , 2 , myshr , false );
NotBinI( pointer(myaddr1) , @j , 1 );
NotBinI( pointer(myaddr2) , @j , 1 );
j := j - 1;
end;
i := i - 1;
end;
result := true;
end;