Link to home
Start Free TrialLog in
Avatar of mtasking
mtasking

asked on

key based encrypt, decrypt string for both delphi and C#

This question has been asked and accepted here before, however the answer was not good enough since the encryption was a static shift based on an integer value.
I  am after two simple functions to encrypt and decrypt a string based on a key (salt, string) for both Delphi and C# without using any extra libraries. A simple character shift would be ok as long as it is a key based shift and encrypted string characters are ANSI compatible.
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

Sounds like homework, to me...
Avatar of mtasking
mtasking

ASKER

You are right Eddie, never thought about that - I should give you 500 points for this answer. I am sure you are a very successful programmer, proficient in several different languages that hates to waste his companys time and money by writing code that lives in somebodys snippet collection and only requires a copy and paste. Let me guess, you are a seasoned developer making lots of money with such comments, arent you? LOL
This is a link to file encryption in Delphi.
I  am after two simple functions to encrypt and decrypt a string based on a key (salt, string) for both Delphi and C# without using any extra libraries. A simple character shift would be ok as long as it is a key based shift and encrypted string characters are ANSI compatible.
Maybe this could help you.

I am not an encryption person at all; do not know exactly what is meant by a 'salt' but this might get you headed in a direction you want to go. The 'salt' key might be the 'StartKey' which is made up with some other 'salt' keys.

It does xor character shifts.

John
{*******************************************************
 * Standard Encryption algorithm - Copied from Borland *
 *******************************************************}
function Encrypt(const InString:string; StartKey,MultKey,AddKey:Integer): string;
var
  I : Byte;
begin
  Result := '';
  for I := 1 to Length(InString) do
  begin
    Result := Result + CHAR(Byte(InString[I]) xor (StartKey shr 8));
    StartKey := (Byte(Result[I]) + StartKey) * MultKey + AddKey;
  end;
end;
{*******************************************************
 * Standard Decryption algorithm - Copied from Borland *
 *******************************************************}
function Decrypt(const InString:string; StartKey,MultKey,AddKey:Integer): string;
var
  I : Byte;
begin
  Result := '';
  for I := 1 to Length(InString) do
  begin
    Result := Result + CHAR(Byte(InString[I]) xor (StartKey shr 8));
    StartKey := (Byte(InString[I]) + StartKey) * MultKey + AddKey;
  end;
end;

Open in new window

PS... this could be the same answer provided in whatever post you were referring to in your original Q. I don't know but links to previously found stuff can be helpful so no one gives you repeat information.

John
Thanks for your answer, this is certainly a start. I changed it up so it can be used with a string key instead of 3 numbers and tested:

  ShowMessage(Encrypt('123abc', '2kewl'));
  ShowMessage(Decrypt(Encrypt('123abc', '2kewl'), '2kewl'));

Anybody up to translate to C# for point split?
function Encrypt(const InString:string; Salt:string): string;
var
  i : Byte;
  StartKey, MultKey, AddKey: Word;
begin
  Result := '';
  if (Salt = '') then begin
    Result := InString;
  end
  else begin
    StartKey := Length(Salt);
    MultKey := Ord(Salt[1]);
    AddKey := 0;
    for i := 1 to Length(Salt) - 1 do AddKey := AddKey + Ord(Salt[i]);
    for i := 1 to Length(InString) do
    begin
      Result := Result + CHAR(Byte(InString[i]) xor (StartKey shr 8));
      StartKey := (Byte(Result[i]) + StartKey) * MultKey + AddKey;
    end;
  end;
end;
 
function Decrypt(const InString:string; Salt:string): string;
var
  i : Byte;
  StartKey, MultKey, AddKey: Word;
begin
  Result := '';
  if (Salt = '') then begin
    Result := InString;
  end
  else begin
    StartKey := Ord(Salt[1]);
    MultKey := Length(Salt);
    AddKey := 0;
    for i := 1 to Length(Salt) - 1 do AddKey := AddKey + Ord(Salt[i]);
    for i := 1 to Length(InString) do
    begin
      Result := Result + CHAR(Byte(InString[i]) xor (StartKey shr 8));
      StartKey := (Byte(InString[i]) + StartKey) * MultKey + AddKey;
    end;
  end;
end;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Johnjces
Johnjces
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great, thanks.

Does not work