Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Encrypting a long string
Message
From
14/02/2019 19:38:25
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01666383
Message ID:
01666394
Views:
33
>>>>>Hi,
>>>>>
>>>>>This is a follow up my thread on the same subject. Just to refresh:
>>>>>I store (un-encrypted) a string that consists of "Y" and "N" in a 250 char field GRP_ACCESS (in SQL Server). Each position corresponds to a feature in the application. When a user logs in, based on his credentials, he may or may not have access to a feature (based on the value in the GRP_ACCESS field).
>>>>>
>>>>>I tried to encrypt the values in this field using the function cipher() (created by S. Berezniker). This function creates an encrypted string (based on the "YYYYNNNNYN..." But if one were to look at the encrypted field you can clearly see a pattern. For example, the users who have/had all "Y"s in the un-encrypted field get the same encrypted string. It is just the encrypted string has many non-ascii characters; so it looks unreadable. But if someone wants to, they can simply store the field value into a variable and then execute the UPDATE table and set this value in all records. I doubt my customer would do it. But, I would like to explore other ways to encrypt the field.
>>>>>
>>>>>If you have any suggestions, please let me know.
>>>>
>>>>When I've had situations like this in the past I've used a secondary input to bias the values. This can be PRNG, or a rectangle from an image (sampling each color and deriving a grayscale value 0..255), or any other source. It then biases each character by adding that value to it and wrapping around 255, so that a value of 'A' + something is stored in that position. If 'A' + something happens to go around 255, then it is stored as the result % 255 value. And then to undo, you go the other way to retrieve the original value. If the value you're subtracting is larger than the value you have, first add 255 to it.
>>>>
>>>>This biasing allows you to use a random source that is reproducible to construct a string that is not immediately decipherable.
>>>>
>>>>
* Use a fixed standard seed value
>>>>RAND(0)  && Be sure to use the same seed value on both encrypt and decrypt if using RAND()
>>>>lcEncryptedString = "ABCDEFG"
>>>>? "Before:", lcEncryptedString
>>>>FOR lnI = 1 TO LEN(lcEncryptedString)
>>>>    lcEncryptedString = STUFF(lcEncryptedString, lnI, 1, CHR((ASC(SUBSTR(lcEncryptedString, lnI, 1)) + ROUND(RAND() * 255.0, 0)) % 255))
>>>>NEXT
>>>>? "Encrypted:", lcEncryptedString
>>>>
>>>>* To un-encrypt:
>>>>RAND(0)  && Be sure to use the same seed value on both encrypt and decrypt if using RAND()
>>>>FOR lnI = 1 TO LEN(lcEncryptedString)
>>>>    lnRand = ROUND(RAND() * 255.0, 0)
>>>>    lcChar = SUBSTR(lcEncryptedString, lnI, 1)
>>>>    IF lnRand > ASC(lcChar)
>>>>        lcEncryptedString = STUFF(lcEncryptedString, lnI, 1, CHR(ASC(lcChar) + 255 - lnRand))
>>>>    ELSE
>>>>        lcEncryptedString = STUFF(lcEncryptedString, lnI, 1, CHR(ASC(lcChar) - lnRand))
>>>>    ENDIF
>>>>NEXT
>>>>? "Decrypted:", lcEncryptedString
>>>
>>>I have not gone through your code, line by line and understand it. But when I copy your code into a .PRG and run it, it works. But only on the second time. That is, the first time the Descrypted shows some Encrypted string. But if I run it again, the Descrypted shows the same and initial string.
>>>Also, if I replace the lcEncryptedString = "ABCDEFG" with lcEncryptedString = repl("Y",250), I never get the correct results.
>>>I will keep trying to figure out what I misunderstand.
>>
>>I wrote it off the top of my head and tested it from the command window. Maybe I wrote it wrong. Let me take a look.
>
>I take my hat off to you that you can just write it off the top. Please you don't have to do it right away. Only when you have some free time.
>Thank you!

Try this one. In various string inputs testing it, it seems to work:
CLEAR
? decrypt(encrypt("Test Data Example Test2 Data2 Example2 Test3 Data3 Example3"))



FUNCTION encrypt
LPARAMETERS tcString
LOCAL lcString

    RAND(0)
    lcString = SPACE(0)
    FOR lnI = 1 TO LEN(tcString)
        lnRand      = ROUND(RAND() * 255.0, 0)
*       ? lnRand
        lcChar      = SUBSTR(tcString, lnI, 1)
        lcChar      = CHR((ASC(lcChar) + lnRand) % 255)
        lcString    = lcString + lcChar
    NEXT
    RETURN lcString



FUNCTION decrypt
LPARAMETERS tcString
LOCAL lcString

    RAND(0)
    lcString = SPACE(0)
    FOR lnI = 1 TO LEN(tcString)
        lnRand      = ROUND(RAND() * 255.0, 0)
*       ? lnRand
        lcChar      = SUBSTR(tcString, lnI, 1)
        lcString    = lcString + CHR((ASC(lcChar) + 255 - lnRand) % 255)
    NEXT
    RETURN lcString
Previous
Reply
Map
View

Click here to load this message in the networking platform