Bad encryption

Posted on 2002-07-18
Last Modified: 2010-04-04
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 ;
  C1  = 52845 ;
  C2  = 22719 ;

  I : BYTE ;
  x : string ;
  k : Integer ;

  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);
  S1, S2 : String ;
  // 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

Question by:pc_melsa

Accepted Solution

Cynna earned 200 total points
ID: 7162964

Firstly, thanks for this easy-to-inspect sample. I love when problem
is reduced to code sample like this. Helping you is *much* easier this

OK, on to your question...
Well, actually you don't really have a problem. To quickly see what I mean,
just replace line:

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


  ShowMessage( 'Encrypted:'+S1); ShowMessage('Decrypted:'+S2);


Your function is correctly encrypting string, but it happened that, for
this particular S/Sign combination, the first letter (ie. '0', or Chr(48)) in
your string S is encrypted as Chr(0).

Why? Simple:

x='00000'   ->  Byte(x[1]) = 48

Key=12345   ->  Key Shr 8 = 48


x[1]= Byte(x[1]) xor  (Key Shr 8) = 48 xor 48 = 0

As you probably know, Chr(0) is interpreted by wast majority of string
functions as termination character.
So, ShowMessage actually sees S1 as empty string, although it really isn't,
as you can easily see by placing a break line at ShowMessage(), and inspecting
S1. As a conseqence, string S1+' '+S2 is also saw as empty string.
If you're not quite clear on this, tell me and I'll try to explain it further.

You have to worry about all this only if you really want to print your
encoded string (I don't see why would you  want this, though, because your
encoded string will almost certanly contain nonprintable characters, and you'll
end up with unpredictable results most of the time...).

But...if you'd like to solve this anyway, one way is to simply replace Chr(0)
in encrypted string with some printable character.

For example, you could add this function:

function DisplayableString(s: String): String;
var i: Integer; // Replace termination char with 'X'
  for i := 1 To length(s) do
      if Result[i]=#0 then Result[i]:='X'

And replace line:

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


  ShowMessage( DisplayableString(S1)+  ' ' + S2 ) ;  // OK after all...


Author Comment

ID: 7163659
Thanks for your clear explanation, May God Bless you !

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi: how to implement a User Shortcut mapper? 1 95
Strange behavior when a form is closed 6 51
QRReport  TQrmemo vertical stretching 1 42
Working with hours 3 46
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now