要分吗?快来这里!!!! (100分)

  • 主题发起人 主题发起人 博天
  • 开始时间 开始时间

博天

Unregistered / Unconfirmed
GUEST, unregistred user!
{
下面是我用于加解密字符的源码(抄来的)
我发现其中一个问题:比如说对字符串888888加密,但有时解密时却得到88888,有时却正常.
我已作了努力并已在离线大富翁中找答案,都不理想.
1.请给出解决的源码;2.请给出其它加解密的源码,我不要控件.
}

unit RlEncrypt;

interface
uses
Classes;
const
C1 = 52845;
C2 = 22719;

function Encrypt(const S: String; Key: Word): String;
function Decrypt(const S: String; Key: Word): String;

implementation

function Encrypt(const S: String; Key: Word): String;
var
I: Integer;
begin
Result := S;
for I := 1 to Length(S) do
begin
Result := char(byte(S) xor (Key shr 8));
Key := (byte(Result) + Key) * C1 + C2;
end;
end;

function Decrypt(const S: String; Key: Word): String;
var
I: Integer;
begin
Result := S;
for I := 1 to Length(S) do
begin
Result := char(byte(S) xor (Key shr 8));
Key := (byte(S) + Key) * C1 + C2;
end;
end;



end.

 
跟踪一下,是不是因为加解密时,出现了 char(0)
 
给你一段加密及解密程序:
function EncryptPassword(tempString:string) : string;
var
i : longint;
modval : variant;
PassString : string;
quotient : longint;
stri : array [0..11] of longint;
begin
for i := 0 to length( trim( tempString ) )-1 do begin
stri[ i ] := integer( ord( copy( tempString , i+1 , 1 ) ) );
end;
// Randomize, not recognised
Randomize;
stri[ 11 ] := integer( random( 1 ) * 17371 );
// Randomize, not recognised
Randomize;
stri[ 10 ] := integer( random( 1 ) * 13791 );
// Randomize, not recognised
Randomize;
stri[ 9 ] := integer( random( 1 ) * 11111 );
for i := 0 to 2 do begin
stri[ 0+i ] := ( stri[0+i]-i * 13733)Xor stri[9];
stri[ i+3 ] := ( stri[i+3]-i * 13733)Xor stri[10];
stri[ i+6 ] := ( stri[i+6]-i * 13733)Xor stri[11];
end;
for i := 0 to 11 do begin
quotient := stri And $FF00;
modval := stri And $FF;
stri[ i ] := integer( ( modval And $F0)/ $10);
modval := modval And $F;
modval := modval * $10+stri;
stri[ i ] := modval+quotient;
end;
PassString := stri[0];
for i := 1 to 11 do begin
PassString := PassString+'#'+CStr( stri );
end;
EncryptPassword := PassString;

end;

function DisencryptPassword(tempString:string) : string;
var
EndPosition : longint;
ErrFlag : boolean;
freeString : string;
i : variant;
modval : longint;
quotient : variant;
StartPosition : variant;
stri : array [0..11] of longint;
begin
ErrFlag := False;
StartPosition := 1;
for i := 0 to 10 do begin
EndPosition := pos( StartPosition , tempString , '#' );
if (EndPosition = 0 Or EndPosition = Null ) then begin
ErrFlag := True;
break;
end;
stri[ i ] := integer( copy( tempString , StartPosition , EndPosition-StartPosition ) );
StartPosition := EndPosition+1;
end;
if (Not ErrFlag ) then begin
stri[ 11 ] := integer( copy( tempString , StartPosition , length( tempString )+1-StartPosition ) );
end
else begin
application.messagebox( 'dkdk' , vbCritical+vbSystemModal , '提示' );
halt;
end;
//'------------------------------------------------------------------
for i := 0 to 11 do begin
quotient := stri And $FF00;
modval := stri And $FF;
stri[ i ] := integer( ( modval And $F0)/ $10);
modval := modval And $F;
modval := modval * $10+stri;
stri[ i ] := modval+quotient;
end;
for i := 0 to 2 do begin
stri[ 0+i ] := ( stri[0+i] Xor stri[9])+i * 13733;
stri[ i+3 ] := ( stri[i+3] Xor stri[10])+i * 13733;
stri[ i+6 ] := ( stri[i+6] Xor stri[11])+i * 13733;
end;
freeString := Empty;
for i := 0 to 7 do begin
if (stri = 0 ) then
break;
freeString := freeString+chr( stri );
end;
DisencryptPassword := trim( freeString );
end;
 
你是不是把加密后的字符串存到数据库中了,
然后再解密,
如果是这样,那就是你的你存放该字段的长度不够
我记得我就遇上过 ;)
 
to 雪中漫步
长度没有问题

另:
我觉得我的那个方法很不稳定,同样对一个字符串加解密得到的结果不一样,有时少一个字符.
 
[:D]我已自己找到原因了,不知道分能否收回(白说^-^)
上面的答复都不如意!平分吧!
 

Similar threads

I
回复
0
查看
577
import
I
I
回复
0
查看
600
import
I
I
回复
0
查看
647
import
I
后退
顶部