Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Encrypting a long string
Message
From
14/02/2019 20:26:16
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01666383
Message ID:
01666406
Views:
33
>>>>The other version still doesn't seem to always work. I switched it to use the C Standard Library's RAND() and SRAND() functions:
>>>>
>>>>
CLEAR
>>>>lcEnc = encrypt("Test string here")
>>>>? lcEnc
>>>>lcDec = decrypt(lcEnc)
>>>>? lcDec
>>>>
>>>>
>>>>
>>>>FUNCTION encrypt
>>>>LPARAMETERS tcString
>>>>LOCAL lcString
>>>>
>>>>    seedRand(0)
>>>>    lcString = SPACE(0)
>>>>    FOR lnI = 1 TO LEN(tcString)
>>>>        lnRand      = ROUND(getRand() * 255.0, 0)
>>>>        lcChar      = SUBSTR(tcString, lnI, 1)
>>>>        lcChar      = CHR((ASC(lcChar) + lnRand) % 255)
>>>>        lcString    = lcString + lcChar
>>>>    NEXT
>>>>    RETURN lcString
>>>>
>>>>
>>>>
>>>>
>>>>FUNCTION decrypt
>>>>LPARAMETERS tcString
>>>>LOCAL lcString
>>>>
>>>>    seedRand(0)
>>>>    lcString = SPACE(0)
>>>>    FOR lnI = 1 TO LEN(tcString)
>>>>        lnRand      = ROUND(getRand() * 255.0, 0)
>>>>        lcChar      = SUBSTR(tcString, lnI, 1)
>>>>        lcString    = lcString + CHR((ASC(lcChar) + 255 - lnRand) % 255)
>>>>    NEXT
>>>>    RETURN lcString
>>>>
>>>>
>>>>
>>>>
>>>>FUNCTION seedRand
>>>>LPARAMETERS tnSeed
>>>>
>>>>    DECLARE INTEGER srand IN msvcr71.dll as cSrand INTEGER nSeed
>>>>    DECLARE INTEGER rand  IN msvcr71.dll AS cRand
>>>>    cSrand(tnSeed)
>>>>
>>>>
>>>>
>>>>
>>>>FUNCTION getRand
>>>>LOCAL lnRand
>>>>
>>>>    RETURN cRand() / 32767.0
>>>>
>>>
>>>This code works every time. Thank you. But (don't take it as a criticism of your approach).
>>>The decripted string looks the same every time the original string is the same. So, as I mentioned in one of the previous messages, the downside is that a user can simply copy a value from one record to another.
>>>It would be nice if the encrypted string would look different even for the same unencrypted string.
>>>I may just have to live with this.
>>>Thank you.
>>
>>Use the RECNO() as the seed.
>
>This is a SQL Server database. And each record has a PK, Identity. So, I am going to use the value of this PK fields as a seed.

That'll work. You can also implement that function as a server-side function and extract the field with the PK seed.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform