Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6171369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:12:05+00:00 2026-05-23T23:12:05+00:00

The following Delphi routine is originally from a long-ago CompuServe posting, and is used

  • 0

The following Delphi routine is originally from a long-ago CompuServe posting, and is used to encrypt various information in our database. Below are both the Delphi 2007 and (thanks to some SO help with the Unicode differences) Delphi XE versions.

We have been trying to convert this to C#, and have gotten close-ish, but we’re missing something somewhere. Unfortunately, our Delphi guy (me) doesn’t know C#, and the C# guy is new to Delphi. C# doesn’t (appear to) have the concept of AnsiString, so the solution will probably involve byte or char arrays?

We’d greatly appreciate any help in converting this to C#.

Delphi 2007 Version (ASCII)

function EncodeDecode(Str: string): string;
const
  Hash: string = '^%12hDVjED1~~#29afdmSD`6ZvUY@hbkDBC3fn7Y7euF|R7934093*7a-|-  Q`';
var
  I: Integer;
begin
  for I := 1 to Length (Str) do
    Str[I] := chr (ord (Str[I]) xor not (ord (Hash[I mod Length (Hash) + 1])));
  Result := Str;
end;

Delphi XE Version (Unicode)

function TfrmMain.EncodeDecode(Str: AnsiString): AnsiString;
const
  Hash: string = '^%12hDVjED1~~#29afdmSD`6ZvUY@hbkDBC3fn7Y7euF|R7934093*7a-|-  Q`';
var
  I: Integer;
begin
  Result := Str;
  for I := 1 to Length (Result) do
    Result[I] := AnsiChar (ord (Result[I]) xor not (Ord (Hash[I mod Length (Hash) + 1])));
end;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:12:05+00:00Added an answer on May 23, 2026 at 11:12 pm

    I don’t know C# either, so this is probably seriously non-idiomatic.

    static string EncodeDecode(string str)
    {
        byte[] hash = new byte[63] { 94, 37, 49, 50, 104, 68, 86, 106, 69, 68, 49, 126, 
            126, 35, 50, 57, 97, 102, 100, 109, 83, 68, 96, 54, 90, 118, 85, 89, 64, 
            104, 98, 107, 68, 66, 67, 51, 102, 110, 55, 89, 55, 101, 117, 70, 124, 82, 
            55, 57, 51, 52, 48, 57, 51, 42, 55, 97, 45, 124, 45, 32, 32, 81, 96 };
    
        Encoding ANSI = Encoding.GetEncoding(1252);
        byte[] input = ANSI.GetBytes(str);
        byte[] output = new byte[input.Length];
        for (int i = 0; i < input.Length; i++)
            output[i] = (byte)(input[i] ^ ~hash[(i + 1) % hash.Length]);
        return ANSI.GetString(output);
    }
    

    I have assumed that your ANSI strings are encoded with Windows 1252, but it you happen to have encoded your legacy data with a different code page it is obvious enough how to change it.

    Since C# doesn’t have the equivalent of Delphi’s 8 bit string types, I personally would be sorely tempted to use byte[] rather than string.

    Done that way it looks like this:

    static byte[] EncodeDecode(byte[] input)
    {
        byte[] hash = new byte[63] { 94, 37, 49, 50, 104, 68, 86, 106, 69, 68, 49, 126, 
            126, 35, 50, 57, 97, 102, 100, 109, 83, 68, 96, 54, 90, 118, 85, 89, 64, 
            104, 98, 107, 68, 66, 67, 51, 102, 110, 55, 89, 55, 101, 117, 70, 124, 82, 
            55, 57, 51, 52, 48, 57, 51, 42, 55, 97, 45, 124, 45, 32, 32, 81, 96 };
    
        byte[] output = new byte[input.Length];
        for (int i = 0; i < input.Length; i++)
            output[i] = (byte)(input[i] ^ ~hash[(i + 1) % hash.Length]);
        return output;
    }
    

    @Groo makes the excellent point that the hash can be initialised more cleanly list this:

    byte[] hash = ANSI.GetBytes(@"^%12hDVjED1~~#29afdmSD`6ZvUY@hbkDBC3fn7Y7euF|R7934093*7a-|-  Q`");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code used to compile with Delphi 2007: constructor TMyFile.Create(const _Filename: string); begin
I used the following code in delphi for handle : procedure TForm1.ADOQuery1FetchProgress(DataSet: TCustomADODataSet; Progress,
Good day, TValue is a Delphi-2010 and up RTTI feature. Following on from my
I am trying to do the following in Delphi 2010: TDataConverter = class abstract
I'm trying to get the Global Interface Table by using the following code (Delphi):
In Delphi I can do the following with a boolean variable: If NOT bValue
I am looking at some code (Delphi 7) with following check is at the
Please forgive the verbosity of the following code example. Using Delphi 2009, I created
When looking for which version of Delphi 2010 to buy, we found the following
I have a file that was written with the following Delphi declaration ... Type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.