Solved

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

Posted on 2009-05-05
9
3,497 Views
Last Modified: 2012-05-06
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.
0
Comment
Question by:mtasking
9 Comments
 
LVL 26

Expert Comment

by:EddieShipman
ID: 24310714
Sounds like homework, to me...
0
 

Author Comment

by:mtasking
ID: 24310792
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
0
 
LVL 13

Expert Comment

by:ThievingSix
ID: 24310887
0
 

Author Comment

by:mtasking
ID: 24313868
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 18

Expert Comment

by:Johnjces
ID: 24328782
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

0
 
LVL 18

Expert Comment

by:Johnjces
ID: 24328859
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
0
 

Author Comment

by:mtasking
ID: 24329241
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

0
 
LVL 18

Accepted Solution

by:
Johnjces earned 500 total points
ID: 24329438
I don't guarantee these as perfect C# translations, but again, should be close to help get you started.

I cannot test them so hope you can.

JOhn
public static string Encrypt(string InString, string Salt)

        {

            string result;

            byte i;

            ushort StartKey;

            ushort MultKey;

            ushort AddKey;

            result = "";

            if ((Salt == ""))

            {

                result = InString;

            }

            else

            {

                StartKey = Salt.Length;

                MultKey = (int)(Salt[1]);

                AddKey = 0;

                for (i = 1; i < Salt.Length; i ++ )

                {

                    AddKey = AddKey + (int)(Salt[i]);

                }

                for (i = 1; i <= InString.Length; i ++ )

                {

                    result = result + ((char)((byte)InString[i]) ^ (StartKey >> 8));

                    StartKey = (((byte)result[i]) + StartKey) * MultKey + AddKey;

                }

            }

            return result;

        }
 

        public static string Decrypt(string InString, string Salt)

        {

            string result;

            byte i;

            ushort StartKey;

            ushort MultKey;

            ushort AddKey;

            result = "";

            if ((Salt == ""))

            {

                result = InString;

            }

            else

            {

                StartKey = (int)(Salt[1]);

                MultKey = Salt.Length;

                AddKey = 0;

                for (i = 1; i < Salt.Length; i ++ )

                {

                    AddKey = AddKey + (int)(Salt[i]);

                }

                for (i = 1; i <= InString.Length; i ++ )

                {

                    result = result + ((char)((byte)InString[i]) ^ (StartKey >> 8));

                    StartKey = (((byte)InString[i]) + StartKey) * MultKey + AddKey;

                }

            }

            return result;

        }

Open in new window

0
 

Author Comment

by:mtasking
ID: 24330005
Great, thanks.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Linq help 1 16
C# Export DataGridView 4 38
FMX and jaudiotracker playing memory stream 29 32
String manipulation 15 49
There are many reasons malware will stay around and continue to grow as a business.  The biggest reason is the expanding customer base.  More than 40% of people who are infected with ransomware, pay the ransom.  That makes ransomware a multi-million…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

17 Experts available now in Live!

Get 1:1 Help Now