Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 11-06-2018, 02:20 PM
avinash.lkr avinash.lkr is offline
Member
 
Join Date: Jun 2009
Posts: 32
Smile Encrypt/Decrypt Method

Hi,
I have a Encryption and Decryption method. But the decryption method is not working for special characters like @, $ and #. Could you please suggest the changes so that it would work for @, $ and #. Below is the encryption/decryption routine.

function nValEncrypt(xVal, j: Integer) : Integer;
begin
RESULT := xVal;
if RESULT > 90 then
RESULT := RESULT - 60
else
if RESULT > 57 then
RESULT := RESULT - 54
else
RESULT := RESULT - 47;
RESULT := RESULT + 5 + j;
if RESULT > 62 then
RESULT := RESULT - 62;
if RESULT < 11 then
RESULT := RESULT + 47
else
if RESULT < 37 then
RESULT := RESULT + 54
else
RESULT := RESULT + 60;
end;


function fnEncryptPassword(psDescryptedPassword : String; pnMaxEncryptedLength : Integer) : String;
var nDL,nSpacing, i, j : Integer;
sEncryptedPassword,
sEncryptedKey :String;
begin
sEncryptedPassword := EmptyStr;
if psDescryptedPassword = EmptyStr then
begin
Result := EmptyStr;
Exit;
end;
sEncryptedKey := 'ai! psDescryptedPassword := Trim(psDescryptedPassword);
nDL := length(psDescryptedPassword);
// nSpacing:=StrToInt(format('%2.0f',[(pnMaxEncryptedLength-nDL)/nDL]));
nSpacing := Trunc((pnMaxEncryptedLength-nDL)/nDL);
j := 1;
while pnMaxEncryptedLength > 0 do
begin
sEncryptedPassword := sEncryptedPassword+
///CHR(Max(ord(psDescryptedPassword[j]),0) + ord(sEncryptedKey[j]));
//CHR(ord(psDescryptedPassword[j]) + 5 + j);
CHR(nValEncrypt(ord(psDescryptedPassword[j]), j));
pnMaxEncryptedLength := pnMaxEncryptedLength - 1;
i := 0;
while((i < nSpacing) and (pnMaxEncryptedLength > 0)) do
begin
sEncryptedPassword :=sEncryptedPassword+
IntToStr(ord(sEncryptedKey[j]));
i := i + 1;
pnMaxEncryptedLength := pnMaxEncryptedLength-1;
end;
j := j + 1;
end;
Result := sEncryptedPassword;
end;
------------------------------------------------------------------------------------
function nValDecrypt(xVal, j: Integer) : Integer;
begin
RESULT := xVal;

if RESULT > 90 then
RESULT := RESULT - 13
else
if RESULT > 57 then
RESULT := RESULT - 7;

RESULT := RESULT - 52 - j;

if RESULT < 1 then
RESULT := RESULT + 62;

if RESULT < 11 then
RESULT := RESULT - 13
else
if RESULT < 37 then
RESULT := RESULT - 6;

RESULT := RESULT + 60;
end;


function DecryptPassword(psEncryptedPassword : String): String;
var nChar, i : Integer;
sDecryptedPassword : String;
begin
RESULT := psEncryptedPassword;
if RESULT <> EmptyStr then
begin
//sEncryptKey :='ai! FOR i := 1 TO LENGTH(RESULT) DO
begin
nChar := nValDecrypt(ord(RESULT[i]),i); //Lennox 10/15/99
if nChar <> 0 then
sDecryptedPassword := sDecryptedPassword + Char(nChar);
end; //FOR
RESULT := sDecryptedPassword;
end;
end;

Last edited by avinash.lkr; 11-06-2018 at 02:25 PM.
Reply With Quote
  #2  
Old 11-07-2018, 10:51 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,307
Default

Let me guess, you use a Unicode Delphi version.
Above code is Ansi based, so change all string to AnsiString and Char to AnsiChar and this should be fine.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 10:43 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2018, vBulletin Solutions, Inc.