Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Encrypting a long string
Message
From
15/02/2019 16:28:06
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01666383
Message ID:
01666464
Views:
28
>>>>>Regarding Mike Gagnon's suggestion, remember that you have already a series of integer values, not of 32-bit integers, but of 8-bit integers. That's what a string is. To hold 250 different logical values, CEILING(250 / 8) = 32 characters would be enough, and a string like yours "YYYNYYNN" could be represented as a single CHR(236).
>>>>
>>>>Hi Antonio,
>>>>
>>>>Thank you for your suggestions. As far as your explanation of 250 logical values and 32-bit integers, I hang my head in shame. I don't understand it off the typed message (lack of formal computer education shows). But I will read it again and hopefully get it, at some point. Thank you.
>>>
>>>Dmitry, you'll see that it's not that complicated.
>>>
>>>Unfortunately, VFP does not have a native representation of binary numbers, but the functions that Thomas referred you to are what you need to question and set a bitmap of this sort, and that you can even incorporate in SQL queries if you want to.
>>
>>To encode() and decode() as Antonio suggests, it creates a binary string:
>>
seedRand(0)
>>lcString = SPACE(0)
>>FOR lnI = 1 TO 250
>>    lcString = lcString + IIF(ROUND((getRand() * 100), 0) % 2 = 1, "Y", "N")
>>NEXT
>>
>>CLEAR
>>lcEnc = encode(lcString)
>>lcDec = decode(lcEnc)
>>? "Encoded, do they match?", (lcDec = lcString)
>>
>>lcEnc = encrypt(encode(lcString))
>>lcDec = decode(decrypt(lcEnc))
>>? "Encrypted and encoded, do they match?", (lcDec = lcString)
>>
>>
>>
>>
>>FUNCTION encode
>>LPARAMETERS tcString
>>LOCAL lnI, lnBit, lnOffset, lcChar, lnBinChar, lcString
>>
>>    * Make sure our string is an even multiple of 8 (to round up to the nearest byte boundary)
>>    lnString = LEN(tcString)
>>    IF LEN(tcString) % 8 != 0
>>        tcString = tcString + SPACE(8 - (LEN(tcString) % 8))
>>    ENDIF
>>
>>    * Create our output string
>>    lcString = CHR(lnString) + REPLICATE(CHR(0), LEN(tcString) / 4)
>>
>>    * Process through each byte of the input string
>>    lnOffset = 2
>>    FOR lnI = 1 TO LEN(tcString) STEP 8
>>        lnBinChar   = ASC(SUBSTR(lcString, lnOffset, 1))
>>        lnBit       = 128
>>        FOR lnJ = 0 TO 7
>>            lcChar      = SUBSTR(tcString, lnI + lnJ, 1)
>>            lnBinChar   = BITOR(lnBinChar, IIF(lcChar $ "Yy", lnBit, 0))
>>            lnBit       = lnBit / 2
>>        NEXT
>>        lcString = STUFF(lcString, lnOffset, 1, CHR(lnBinChar))
>>        lnOffset = lnOffset + 1
>>    NEXT
>>    RETURN lcString
>>
>>
>>
>>
>>FUNCTION decode
>>LPARAMETERS tcString
>>LOCAL lcString
>>
>>    * Extract the total output string length
>>    lcString    = SPACE(ASC(SUBSTR(tcString, 1, 1)))
>>    lnOffset    = 2
>>    FOR lnI = 1 TO LEN(lcString)
>>        lcBinChar   = ASC(SUBSTR(tcString, lnOffset, 1))
>>        lnBit       = 128
>>        FOR lnJ = 0 TO 7
>>            lcString    = STUFF(lcString, 1 + ((lnI - 1) * 8) + lnJ, 1, IIF(BITAND(lcBinChar, lnBit) != 0, "Y", "N"))
>>            lnBit       = lnBit / 2
>>        NEXT
>>        lnOffset = lnOffset + 1
>>    NEXT
>>    RETURN lcString
>>
>>
>>
>>
>>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
>>
>
>Hi Rick,
>
>You probably meant this message for me and sent it to Antonio. Anyway, thank you for the code. I will review it, as an academic exercise and for learning.
>However, as far as actually implementing this in my application, I chose to use the Cipher() function, using the field PK as a seed. So far, in testing (on my computer) everything works pretty well.
>Just as FYI, I have this field (with "Y"s and "N"s) open (uncrypted) for 20+ years and not one time anybody knew or noticed it. The users don't see the data and the IT do not care how it works, as long as it works.
>I wanted to encrypt the field more because I felt it was not right, rather than the program was ever hacked.

I figured you'd see it. I replied to Antonio's statements on the content.

This code can be extended to allow characters other than Y and y for a 1, and other characters for a 0.

As people here say ... another tool in the toolbox. :-)
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform