Simple Text Encrypt/Decrypt Function

delphibr
delphibr used Ask the Experts™
on
Hi Experts!

I need to "encrypt" (is it the right term?) the text messages in the source-code, in order to avoid been changed with some kind of "decompiler" or "resource hacker" tool. I already use a function for this, but the texts get 3 times longer (and so my app size). I do NOT want any complex function, just something simple as possible, to make texts unreadable, and with the same length as before (or less, if possible ;-))

Ohhh... I forgot to say... the messages have accents and other special characters, like á, ô, ç...

Thanks!

Delphi BR
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Here is an example, for making things clear:

Original message:
- ShowMessage('Segue boleto bancário para seu controle! Estamos à disposição.');

Encrypted message:
ShowMessage(DecodeText('73F073F65387B56FEE5742290C0B5C89C171A481D50D182CD40455F4DFC67'));

I need both original and encrypted text have the same length.

Thanks again!

Delphi BR
Pass these functions a pointer or pchar. EncryptText(PChar(MyString));
procedure DecryptText(Data: Pointer; Size: Integer);
//[Data = EDX] [Size = ECX]
asm
  push esi;
  push edi;
  mov esi,ecx;
  mov edi,edx;
  @decrloop:
    mov al,byte ptr ds:[edi];
    mov dl,al;
    shr dl,3;
    shl al,5;
    or dl,al;
    not dl;
    mov byte ptr ds:[edi],dl;
    inc edi
    dec esi
  jnz @decrloop;
  pop edi;
  pop esi;
end;

procedure EncryptText(Data: Pointer; Size: Integer);
//[Data = EDX] [Size = ECX]
asm
  push esi;
  push edi;
  mov esi,ecx;
  mov edi,edx;
  @decrloop:
    mov al,byte ptr ds:[edi];
    ror al,6
    xor al,$F5;
    add al,$C6;
    xor al,$9D;
    sub al,$7A;
    mov byte ptr ds:[edi],al;
    inc edi
    dec esi
  jnz @decrloop;
  pop edi;
  pop esi;
end;

Open in new window

Sorry, forgot to edit it. This is the correct version:
procedure DecryptText(Data: Pointer; Size: Integer);
//[Data = EDX] [Size = ECX]
asm
  push esi;
  push edi;
  mov esi,ecx;
  mov edi,edx;
  @decrloop:
    mov al,byte ptr ds:[edi];
    mov dl,al;
    shr dl,3;
    shl al,5;
    or dl,al;
    not dl;
    mov byte ptr ds:[edi],dl;
    inc edi
    dec esi
  jnz @decrloop;
  pop edi;
  pop esi;
end;

procedure EncryptText(Data: Pointer; Size: Integer);
//[Data = EDX] [Size = ECX]
asm
  push esi;
  push edi;
  mov esi,ecx;
  mov edi,edx;
  @decrloop:
    mov al,byte ptr ds:[edi];
    mov dl,al;
    shr dl,5;
    shl al,3;
    or dl,al;
    not dl;
    mov byte ptr ds:[edi],al;
    inc edi
    dec esi
  jnz @decrloop;
  pop edi;
  pop esi;
end;

Open in new window

Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

CodedKSenior Software Engineer

Commented:
Hi DelphiBr

Try this :

Function Change(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 ;
-----------------------------------------------------------------------------------
Usage :

Edit2.Text:= Change(Edit1.Text,1,12345); //Encrypt
Edit2.Text:= Change(Edit2.Text,2,12345); //Decrypt

Author

Commented:
Thanks for both of you!

ThievingSix:
I tried Memo1.Text := EncryptText(PChar(Memo1.Text), SizeOf(Memo1.Text)); but did not work. Can you give me a working example?

CodedK:
Sorry, but your function do not work with accents (á, ô, ...)

Thanks again!

Delphi BR

Author

Commented:
CodedK

Your function DO WORK with accents. I made I mistake before, sorry!

Well, I will wait ThievingSix example and make some more tests. Thanks!

Delphi BR
CodedKSenior Software Engineer

Commented:
Sorry DelphiBr.
I don't have Delphi to check this out, another one :

function Encrypt(aFrase: String; key: Integer): String;
var
    i : Integer;
begin
   Result:='';
   RandSeed:=key;
   for i:=1 to Length(aFrase) do
       Result:=Result+Chr(Byte(aFrase[i]) xor random(256));
end;

function Decrypt(aFrase: String; key: Integer): String;
var
    i : Integer;
begin
   Result:='';
   RandSeed:=key;
   for i:=1 to Length(aFrase) do
       Result:=Result+Chr(Byte(aFrase[i]) xor random(256));
end;
-----------------------------------------------------------------------------------
Usage :

procedure TForm4.Button1Click(Sender: TObject);
begin
 Edit2.Text := Encrypt(Edit1.Text,6474);
end;

procedure TForm4.Button2Click(Sender: TObject);
begin
 Edit2.Text := Decrypt(Edit2.Text,6474);
end;

procedure DecryptText(var Text: String);
var
  I : Integer;
  _AL, _DL : Byte;
begin
  For I := 1 To Length(Text) Do
    begin
    _AL := Byte(Text[I]);
    _DL := _AL;
    _DL := _DL SHR 3;
    _AL := _AL SHL 5;
    _DL := _DL OR _AL;
    _DL := NOT _DL;
    Text[I] := Char(_DL);
  end;
end;

procedure EncryptText(var Text: String);
var
  I : Integer;
  _AL, _DL : Byte;
begin
  For I := 1 To Length(Text) Do
    begin
    _AL := Byte(Text[I]);
    _DL := _AL;
    _DL := _DL SHR 5;
    _AL := _AL SHL 3;
    _DL := _DL OR _AL;
    _DL := NOT _DL;
    Text[I] := Char(_DL);
  end;
end;

Open in new window

Author

Commented:
Hello!

CodedK: I do not get the same text when decrypt some messages. I do not know if there is some special character causing the problem, or maybe because some of the texts are bigger (more than 255 char length).

ThievingSix: Your last solution worked just fine! But it does not have a encrypt "key". I am wondering if I can make some changes on it, to make it different from others found on internet. Can you help me?

Thanks!

Delphi BR
CodedKSenior Software Engineer

Commented:
DelphiBr do you have the problem with the first code or the second one i posted ?

Author

Commented:
Hello CodedK,

The problem I talked about happened with the first code. The second not even worked with accents.

Thanks!

Delphi BR

procedure DecryptText(var Text: String; const Key: DWORD);
var
  I : Integer;
  Key1, Key2 : DWORD;
  _AL, _DL : Byte;
begin
  Key1 := Key;
  Key2 := NOT Key;
  For I := 1 To Length(Text) Do
    begin
    _AL := Byte(Text[I]);
    _AL := _AL XOR Key2;
    _AL := _AL XOR Key1;
    _DL := _AL;
    _DL := _DL SHR 3;
    _AL := _AL SHL 5;
    _DL := _DL OR _AL;
    _DL := NOT _DL;
    Text[I] := Char(_DL);
  end;
end;

procedure EncryptText(var Text: String; const Key: DWORD);
var
  I : Integer;
  Key1, Key2 : DWORD;
  _AL, _DL : Byte;
begin
  Key1 := Key;
  Key2 := NOT Key;
  For I := 1 To Length(Text) Do
    begin
    _AL := Byte(Text[I]);
    _DL := _AL;
    _DL := _DL SHR 5;
    _AL := _AL SHL 3;
    _DL := _DL OR _AL;
    _DL := NOT _DL;
    _DL := _DL XOR Key1;
    _DL := _DL XOR Key2;
    Text[I] := Char(_DL);
  end;
end;

Open in new window

Author

Commented:
I can not make your last solution (with crypt key) work, but this is just good enough (simple and solved my problem). Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial