Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: How can I get my code to work like it used to with String types?


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 4 - Last Post: May 4, 2016 11:04 AM Last Post By: Remy Lebeau (Te... Threads: [ Previous | Next ]
Mark Tiede

Posts: 19
Registered: 5/17/98
How can I get my code to work like it used to with String types?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 4, 2016 3:27 AM
I wrote a bunch of encryption code a long time ago in Delphi 5 or 6. Now I currently have Delphi XE. I have to modify that code slightly. Can I do that without having to find an old version of Delphi? (I know, what I have is already old, but I haven't used Delphi in a few years).

So isn't there an option that I can just use the old String type instead of all the modern ansistring, widechar, pchar, pansichar, etc.?

I'm doing stuff where I converting Hex strings to binary and then encrypting using CryptoAPI and I can't find any combination of types that works right.

At one place, I read a file into an ansistring using TFileStream and all seems okay.

Then later I take a hex string that is in an input box and try the same thing with that ansistring and it looks like it has widechars with extra #0 values. So "09DB6E49C918" in the file reads and decrypts properly as "master". But var x : ansistring set to "09DB6E49C918" and calling the same routine comes out as m#0a#0s#0.

I'm guessing that the file makes the ansistring have some codepage somehow. And the string variable determines some different codepage.

Or something. It is all terribly confusing.

So IS there an option that says just do it like it used to work? Or will I have to scare up a copy of Delphi 5 and see if I can still install it?

FWIW, I think the Classes HexToBin is the particular routine that is screwing me up. It looks to be putting the #0 characters in somehow.

Of course, my boss wants this changed yesterday.

Edited by: Mark Tiede on May 4, 2016 3:28 AM

Edited by: Mark Tiede on May 4, 2016 4:06 AM
Mark Tiede

Posts: 19
Registered: 5/17/98
Re: How can I get my code to work like it used to with String types?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 4, 2016 4:09 AM   in response to: Mark Tiede in response to: Mark Tiede
I thought I isolated relevant code, but that probably wasn't it. so I deleted this.
Dalija Prasnikar

Posts: 2,325
Registered: 11/9/99
Re: How can I get my code to work like it used to with String types?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 4, 2016 7:06 AM   in response to: Mark Tiede in response to: Mark Tiede
Mark Tiede wrote:
I wrote a bunch of encryption code a long time ago in Delphi 5 or 6. Now I currently have Delphi XE. I have to modify that code slightly. Can I do that without having to find an old version of Delphi? (I know, what I have is already old, but I haven't used Delphi in a few years).

So isn't there an option that I can just use the old String type instead of all the modern ansistring, widechar, pchar, pansichar, etc.?

I'm doing stuff where I converting Hex strings to binary and then encrypting using CryptoAPI and I can't find any combination of types that works right.

At one place, I read a file into an ansistring using TFileStream and all seems okay.

Then later I take a hex string that is in an input box and try the same thing with that ansistring and it looks like it has widechars with extra #0 values. So "09DB6E49C918" in the file reads and decrypts properly as "master". But var x : ansistring set to "09DB6E49C918" and calling the same routine comes out as m#0a#0s#0.

I'm guessing that the file makes the ansistring have some codepage somehow. And the string variable determines some different codepage.

Or something. It is all terribly confusing.

So IS there an option that says just do it like it used to work? Or will I have to scare up a copy of Delphi 5 and see if I can still install it?

FWIW, I think the Classes HexToBin is the particular routine that is screwing me up. It looks to be putting the #0 characters in somehow.

Of course, my boss wants this changed yesterday.

Without specific code it is hard to say. If you need to preserve old behavior
without reinterpreting codepage then use RawByteString instead of AnsiString.

--
Dalija Prasnikar
https://twitter.com/dalijap
https://plus.google.com/+DalijaPrasnikar
Mark Tiede

Posts: 19
Registered: 5/17/98
Re: How can I get my code to work like it used to with String types?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 4, 2016 10:13 AM   in response to: Mark Tiede in response to: Mark Tiede
I figured out what was going on. The app encrypted a string that it got from the UI. The UI TEdit.Text was apparently widestring. Even though it was assigned to a String type in code from the TEdit.Text and the debugger showed it looking like a normal string, it really had two bytes per character and the second byte was a hex zero. Only when I did a little type casting on the string did I realize the #0s were in there.

So the encrypted value was wrong and showing in Hex and I couldn't tell it was not the same value that the old system encrypted it to. Later, there was a decryption phase and I kept getting the string with the #0s in it. So I thought the problem was with the DEcrypt. But really, the problem was with the the input to the Encrypt.

So I changed some code that was passing Strings around and made sure that I was just getting contiguous characters like the old String did.

I also made my only version of HexToBin because that did different things that I didn't want.

Now the encrypt and decrypt are working.

Thanks anyway.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: How can I get my code to work like it used to with String types?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 4, 2016 11:04 AM   in response to: Mark Tiede in response to: Mark Tiede
Mark wrote:

The app encrypted a string that it got from the UI. The UI TEdit.Text
was apparently widestring.

A UnicodeString, not a WideString.

Even though it was assigned to a String type in code from the TEdit.Text

The String type is an alias for UnicodeString.

and the debugger showed it looking like a normal string

Because it is.

it really had two bytes per character and the second byte was a hex zero.
Only when I did a little type casting on the string did I realize the #0s
were
in there.

That is normal for a Unicode string.

So the encrypted value was wrong and showing in Hex and I couldn't
tell it was not the same value that the old system encrypted it to.

Encryption deals in bytes, not in characters. You have to charset-encode
a string to bytes before then encrypting the encoded bytes. UTF-8 is a common
charset to use.

Later, there was a decryption phase and I kept getting the string with
the #0s in it.

Likewise, you have to charset-decode the decrypted bytes back to Unicode.

Unicode String -> charset bytes -> encryption -> decryption -> charset bytes
-> Unicode String

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02