Solved

Bad encryption

Posted on 2002-07-18
2
538 Views
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 ;
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;

0
Comment
Question by:pc_melsa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
Cynna earned 200 total points
ID: 7162964
pc_melsa,

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
way.

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 !!!

with:

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

See?


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

So,

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'
begin
  Result:=s;
  for i := 1 To length(s) do
      if Result[i]=#0 then Result[i]:='X'
end;

And replace line:

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

with:

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

0
 

Author Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

728 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