Lizenzfile entschlüsseln

Deutschsprachiges X#-Forum – German language forum

Moderator: wriedmann

Post Reply
lagraf
Posts: 450
Joined: Thu Jan 18, 2018 9:03 am
Location: A

Lizenzfile entschlüsseln

Post by lagraf »

Hallo,
um das Raubkopieren zu erschweren, habe ich in meinen VO Apps seit 1.0 eine Routine, welche einen OEM String aus einer Lizenzdatei wie folgt decodiert:

Code: Select all

FUNCTION GetLicense(cStr AS STRING, cKey AS STRING) AS STRING PASCAL
LOCAL x, y, z AS DWORD

x := y := 1
DO WHILE x <= Len(cStr)
	z := 256 + Asc(SubStr(cStr,x,1)) - Asc(SubStr(cKey,y,1)) - x - y
	z -= iif(z>=256, 256, 0)
	cStr := Stuff(cStr, x, 1, Chr(z))
	x += 1
	y := iif(y >= Len(cKey), 1, y+1)
ENDDO
RETURN cStr
In X# kommt jedoch nur Blödsinn heraus, hängt wahrscheinlich mit irgendwelchen internen Zeichensätzen zusammen. Wie muß die Routine umgeschrieben werden, damit sie wieder einen korrekten Returnwert liefert?
User avatar
Chris
Posts: 4899
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Lizenzfile entschlüsseln

Post by Chris »

Hi Franz,

That's because Asc() and Chr() are designed to work with unicode strings (that are used in .Net), while you are assuming 8-bit strings (as in VO). Try using AscW() and ChrW() instead, which do not do any ansi/unicode translation, does it work now as you'd expect?

If not, please give as some sample contents of the key and value strings, so we can propose an alternative way of writing this code.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
lagraf
Posts: 450
Joined: Thu Jan 18, 2018 9:03 am
Location: A

Lizenzfile entschlüsseln

Post by lagraf »

Hi Chris,
AscW / ChrW does not bring the correct output. I've attached one license file and for this the output should be

Code: Select all

WIN_RBKASSA_9999_01_Gärtnerei Rachbauer (01)_31.12.2999_J_XXXXXXXXXXXXXXXX_201326w
The _ is a visual filler for HT chr(9)
The cKey param is CORAONLY in uppercases
Read the file simply with cStr := MemoRead("01RBKASSA.WIN")
Attachments
01RBKASSA.zip
(206 Bytes) Downloaded 58 times
User avatar
robert
Posts: 4518
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Lizenzfile entschlüsseln

Post by robert »

Franz,
If you read the file with MemoRead then the characters are converted from bytes to unicode.
If you want to work directly with the bytes then I would advise to not use MemoRead() but use MemoReadBinary().
This returns the contents of the file as a byte[]. Inside GetLicense you can then walk the array of bytes and instead of Substr(cStr,x,1) you can then simply use aBytes[x] (assuming you have named the parameter aBytes).
Of course Len(cStr) must then also be replaced with aBytes:Length.
Afterwards you can convert the bytes to a unicode string with System.Text.Encoding.GetEncoding(1252).GetString(aBytes)
You can replace 1252 with another codepage number when needed.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
lagraf
Posts: 450
Joined: Thu Jan 18, 2018 9:03 am
Location: A

Lizenzfile entschlüsseln

Post by lagraf »

Hi Robert,
thank you for your infos!
Now the funktion looks like

Code: Select all

FUNCTION GetLicense(aStr AS BYTE[], cKey AS STRING) AS STRING PASCAL
// aStr is read with MemoReadBinary
	LOCAL nCode, x, y, z AS DWORD
	LOCAL cStr := AS STRING

	y := 1
	FOR x := 1 TO aStr:length
		z := 256 + aStr[x] - AscW(SubStr(cKey,y,1)) - x - y
		z -= iif(z>=256, 256, 0)
		cStr += ChrW(z)
		y := iif(y >= Len(cKey), 1, y+1)
	NEXT
RETURN cStr
The output looks good, but the ä in Gärtnerei is not correct decoded!
User avatar
Chris
Posts: 4899
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Lizenzfile entschlüsseln

Post by Chris »

Hi Franz,

All the characters in strings that are "standard" Latin have the same code in both ansi/oem and unicode. So "A" is always 65, "C" is 67 etc, so for this case it is easy to convert "CORAONLY" to 8-bit. The issue is with non-standard chars, for example "ä" has a unicode value of 228 and "α" is 391.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Lizenzfile entschlüsseln

Post by wriedmann »

Hallo Franz,
vielleicht hilft das beim Verständnis ein bisschen weiter:
https://docs.xsharp.it/doku.php?id=string_char_byte
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

Lizenzfile entschlüsseln

Post by Karl-Heinz »

Hi Franz,

When i try:

Code: Select all

SetAnsi ( true ) 
? GetLicense ( MemoRead ( "d:test1RBKASSA.WIN" ) , "CORAONLY" )


the word [Gärtnerei] is shown as [G"rtnerei]. But after a additional Oem2Ansi() the "ä" appears.

Code: Select all

? Oem2Ansi(GetLicense ( MemoRead ( "d:test1RBKASSA.WIN" ) , "CORAONLY" ) )

Code: Select all

WIN     RBKASSA 9999    01      Gärtnerei Rachbauer (01)        31.12.2999
J       XXXXXXXXXXXXXXXX        201326w
Is this the correct result ?

BTW. The GetLicense() func i´m using still uses Chr()/Asc() and not ChrW()/AscW()

regards
Karl-Heinz
lagraf
Posts: 450
Joined: Thu Jan 18, 2018 9:03 am
Location: A

Lizenzfile entschlüsseln

Post by lagraf »

Hallo Karl-Heinz,
das funktioniert so, die Lizenz kommt aus einem Harbour (DOS) Programm, daher ist Oem2Ansi notwendig!
Danke!
Post Reply