Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Encrypting a long string
Message
 
 
To
14/02/2019 16:38:37
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01666383
Message ID:
01666391
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.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform