Solved

Convert Delphi to c#

Posted on 2011-03-23
10
1,102 Views
Last Modified: 2012-05-11
I am having problems converting the following from Delphi to C#:

const
  cKey1 = 57287;
  cKey2 = 34321;

    function Decrypt(CONST s:  STRING; Key:  WORD):  STRING;
    var
      b:   BYTE;
      i:   INTEGER;
    begin
        b := 0;
        RESULT := '';
        FOR i := 1 TO LENGTH(s) DIV 2 DO
        begin
          try
            b :=  StrToInt('$' + COPY(s, 2*i-1, 2));
          except
            ON EConvertError DO b := 0
          end;
          RESULT := RESULT + CHAR( b XOR (key SHR 8) );
          key := (b + key) * Ckey1 + Ckey2
        end
    end;
0
Comment
Question by:markusr13
  • 4
  • 3
  • 2
  • +1
10 Comments
 

Author Comment

by:markusr13
ID: 35201810
use this as a test: "9DD6678ACD84FC0211DBCA543EE1F5"
0
 

Author Comment

by:markusr13
ID: 35201819
probably should note that they key = 51474
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35202419
I get "TheSkyIs" in C# - conversion from one to the other doesn't seem terribly difficult, the Delphi syntax seems relatively obvious (I've never even seen Delphi before, and was able to make some sense out of it).

Did you give it a try?  Was there something in particular you were stuck at?
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 24

Accepted Solution

by:
jimyX earned 63 total points
ID: 35202469
I am from Delphi zone and i do not know much about C#. The following is my trying:
public const int cKey1 = 57287;
public const int cKey2 = 34321;

public STRING Decrypt(string Name s, ushort Key)
{
    Byte b = 0;
    int i;
    string result = "";
    for (int i = 1; i <= s.Length / 2; i++)
    {
    try 
    {
      b = int.Parse(s.Substring(2*i-2, 2), NumberStyles.AllowHexSpecifier);
    }
    catch
    {
      b = 0;
    }
      result += (char)( b ^ (key >> 8) );
      key = (b + key) * Ckey1 + Ckey2;
    }
    return result;
}

Open in new window

0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 62 total points
ID: 35202484
Well, I had already done the work, I was just trying not to give it away for free, just in case this was a homework assignment or something. ;)

static string Decrypt(string encryptedString, ushort Key)
{
	ushort cKey1 = 57287, cKey2 = 34321;
	StringBuilder returnValue = new StringBuilder();

	for (int i = 0; i < encryptedString.Length / 2; i +=2)
	{
		byte b;
		try
		{
			b = Byte.Parse(encryptedString.Substring(i, 2), NumberStyles.HexNumber);
		}
		catch
		{
			b = 0;
		}

		returnValue.Append((char)(b ^ (Key >> 8)));

		Key = (ushort)((b + Key) * cKey1 + cKey2);
	}

	return returnValue.ToString();
}

Open in new window

0
 
LVL 24

Expert Comment

by:jimyX
ID: 35202608
@ tgerbert
Sorry mate, did not mean to spoil it.

Side comment ;-)
>   "Delphi syntax seems relatively obvious"
That's sure thing.

>   "I've never even seen Delphi before"
Oh man, Delphi is the best. Try using it, you will be amazed.

Cheers
0
 

Author Comment

by:markusr13
ID: 35203599
Man, this looks pretty much looks like what I did...

What was getting me was "key := (b + key) * Ckey1 + Ckey2"

A word is only 65,535 but this calculation goes beyond that. I didn't dig into Delphi to see exactly how it was handling it. C# (ushort) doesn't handle it as gracefully....I thought.

I will test more tomorrow. jimyX version works on my test string tgerbert falls a little short (I will see why tgerbert's only returned me 8 characters).

Points come tomorrow.

(Delphi is great, c# is Delphi on steroids as it was written even better by the person (Anders) that wrote Delphi) but i still miss the bookmarks which must have been patented by Borland. VS has something similar but can't be numbered and they cross units.

-Markus

0
 

Author Comment

by:markusr13
ID: 35203641
working solution (thanks to both of you). I don't know what I was smoking but I should have solved it myself. I had to have been doing something dumb on this line: Key = (ushort)((b + Key) * cKey1 + cKey2);

public string Decrypt(string s, ushort Key)
{
    Byte b = 0;

    string result = "";
    for (int i = 1; i <= s.Length / 2; i++)
    {
    try
    {
      b = (byte)int.Parse(s.Substring(2*i-2, 2), NumberStyles.AllowHexSpecifier);
    }
    catch
    {
      b = 0;
    }
    result += (char)(b ^ (Key >> 8));
    Key = (ushort)((b + Key) * cKey1 + cKey2);
    }
    return result;
}
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35203718
I made a typo (copied the "/ 2" from your Delphi code, realized I didn't need it, but forgot to take it out).
Couple notes...

1. Use meaningful variable names, particularly for method parameters, e.g. "encryptedString" instead of "s"

2. Instead of this goofy construct ".Substring(2*i-2, 2)", you can initialize the for loop's iterator, i, to 0, make it's condition that i is less than the string's length, and increment it by two each iteration, instead of 1, you can write ".Substring(i, 2)" - which changes the "for" loop from "for (int i = 1; i <= s.Length / 2; i++)" to "for (int i = 0; i < s.Length; i += 2)"

3. The byte "b" is of no use to me outside of the for loop, so I deliberately declared it inside the loop to limit it's scope.

4. Don't concatenate strings in loops like this using syntax such as "result += someOtherString" - since strings in .Net are immutable (meaning once created they can't be changed), you're actually creating new strings everytime you do that. That's what the StringBuilder is intended for.

static public string Decrypt(string encryptedString, ushort Key)
{
	ushort cKey1 = 57287, cKey2 = 34321;
	StringBuilder returnValue = new StringBuilder();

	for (int i = 0; i < encryptedString.Length; i += 2)
	{
		byte b;
		try
		{
			b = Byte.Parse(encryptedString.Substring(i, 2), NumberStyles.HexNumber);
		}
		catch
		{
			b = 0;
		}

		returnValue.Append((char)(b ^ (Key >> 8)));

		Key = (ushort)((b + Key) * cKey1 + cKey2);
	}

	return returnValue.ToString();
}

Open in new window


Funny how, even though I'd never seen Delphi code before, I could deduce that "key SHR 8" means shift-right by 8 bits - but if I had never seen C# before, the equivelant statement "key >> 8" would be almost meaningless. I still prefer C# though. ;)

Bookmarks (if they're what I think they are) would be a feature of the text editor Borland ships with their product, not the language.  There are a number of free text editors available that handle things like syntax highlighting for C# (and other languages) that might do what you want - there's nothing that says you have to use Visual Studio to write C#.
0
 
LVL 25

Expert Comment

by:epasquier
ID: 35205018
@tgerbert :  the Delphi syntax seems relatively obvious

Indeed it is. And that sample was an easy one, you would be amazed how readable it is for complex things (objects). Compared to C syntax (and all derived Java, C++, C#) Delphi is just pure vanilla
And to say that the framework is also simple AND effective is an understatement. If you like .Net, you have to LOVE Delphi if you give it a try

I would like to know why you still prefer C#, as you said quite correctly that Delphi is so readable that any good programmer with no specific knowledge of it can understand it. Maybe we can convince you otherwise :o)

> c# is Delphi on steroids
I wouldn't say C# (.Net in fact) is better than Delphi. Let's say it is Java redesigned for MS world in Delphi spirit but keeping C-style syntax. a kind of hydra with multiple heads if you ask me, a powerful one for sure. But it couldn't be compared with any native language, as far as performance is concerned.

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

777 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