Solved

# Bad encryption

Posted on 2002-07-18

Dear Experts, please help !

Please have a look at the program, and tell me what is wrong.

Thanks from apin.

ps : I'm using D5 Enterprise and Windows ME

Function Encrypt(S : String ; Sign : Integer ; Key : Word ) : String ;

Const

C1 = 52845 ;

C2 = 22719 ;

Var

I : BYTE ;

x : string ;

k : Integer ;

Begin

x := S ;

IF Length(s) = 0 Then Begin

Result := '' ;

Exit ;

End ;

For I := 1 To Length(S) Do Begin

x[I] := Char(Byte(x[I]) xor ( Key Shr 8 )) ;

Case Sign OF

1 : Key := ( Byte(x[I]) + Key ) * C1 + C2 ; // encrypt

2 : Key := ( Byte(s[I]) + Key ) * C1 + C2 ; // Decrypt

End ; // Case

End ; // For

Result := x ;

End ;

procedure TForm1.Button1Click(Sender: TObject);

Var

S1, S2 : String ;

begin

// bad

S1 := Encrypt('00000',1,12345) ; // encrypt

S2 := Encrypt(S1,2,12345) ; // decrypt

ShowMessage( S1 + ' ' + S2 ) ; // Not OK !!!

// good, when I change the Key value >= 20000

S1 := Encrypt('00000',1,20000) ;

S2 := Encrypt(S1,2,20000) ;

ShowMessage( S1 + ' ' + S2 ) ; // OK

S1 := Encrypt('100000',1,12345) ;

S2 := Encrypt(S1,2,12345) ;

ShowMessage( S1 + ' ' + S2 ) ; // OK

S1 := Encrypt('Good day',1,222) ;

S2 := Encrypt(S1,2,222) ;

ShowMessage( S1 + ' ' + S2 ) ; // OK

end;