Solved

# VFP Encrypt Decrypt

Posted on 2012-08-29
3,737 Views
Hi all. I was trying to find a simple way to encrypt/decrypt text and came across the following on a website:

``````* usage to encrypt:  enCrypt ( text/variable/fields, number used in deCrypt )
* usage to decrypt:  deCrypt ( text/variable/fields, any number number )

mytext = "I love Lucy"
enc_number = 394820348

decTxt = enCrypt( mytext, enc_number)
encTxt = decCrypt( decTxt, enc_number)

WAIT WINDOW "Your Text: " + mytext
WAIT WINDOW "Your Encrypted Text: " + encTxt
WAIT WINDOW "Your Decrypted Text: " + decTxt

FUNCTION enCrypt

PARAMETER val1, val2
cVal = ""
FOR i = 1 to Len(val1)
cVal = cVal + CHR(ASC(SUBS(val1,i,1))+val2)
NEXT i

reTurn LEFT(cVal,Len(val1))

FUNCTION decCrypt

PARAMETER val1, val2
cVal = ""
FOR i = 1 to Len(val1)
cVal = cVal + CHR(ASC(SUBS(val1,i,1))-val2)
NEXT i

reTurn LEFT(cVal,Len(val1))
``````

The problem is it throws an error here:
cVal = cVal + CHR(ASC(SUBS(val1,i,1))+val2)
in the Encrypt.

I would imagine it will as well in the decrypt although I haven't tried it yet.
I was wondering if anyone can see what the problem is, or, perhaps know of a simple text encrypt/decrypt alternative. Thanks!
0

Author Comment

I also found an old fll named cipher50.fll and tried like so to use it:

``````SET LIBRARY TO pcLibrariesDir + "cipher50.fll"

plaintext = "I love Lucy"

WAIT WINDOW "your text is: " + plaintext + CHR(13) + ;

WAIT WINDOW "your text - I love Lucy encrypted = " + enctxt

WAIT WINDOW "your text decrypted: " + decpt
``````

The problem is on compile FP throws an error 'Unable to find unknown Encrypt'

The cipher50.fll is in the location specified by Set Library so I'm not sure what the problem is.

Still looking for a simple encrypt/decrypt solution...
0

LVL 27

Expert Comment

It is because the CHR function does not accept values more than 255.

This kind of encryption is weak because it increases the CHR by a constant. The first attempt would be to reverse it in a loop to get that constant.

I would suggest to take the nth character of the password and add it to the nth character of the string.

You need the MOD 256 because the remainder is not more than 255.
0

LVL 29

Expert Comment

Makes me wonder where you look around. Take VFPEncryption.fll or even look into the samples of VFP, you find samples using windows (strong!) cryptogrphic functions.

Bye, Olaf.
0

LVL 41

Accepted Solution

If you are happy with symmetric encryption then you may use BITXOR() function included in VFP. Following code requires VFP 9 because it converts strings into varbinary data (note both enCrypt and deCrypt functions are equal):
``````* usage to encrypt:  enCrypt ( text/variable/fields, text encryption key )
* usage to decrypt:  deCrypt ( text/variable/fields, text encryption key )

mytext = "I love Lucy"
enc_number = "Some text which must be longer than your encrypted text. It should preferably contain random characters."

decTxt = enCrypt( mytext, enc_number)
encTxt = decCrypt( decTxt, enc_number)

WAIT WINDOW "Your Text: " + mytext
WAIT WINDOW "Your Encrypted Text: " + decTxt
WAIT WINDOW "Your Decrypted Text: " + encTxt

FUNCTION enCrypt

PARAMETER val1, val2

reTurn '' + BITXOR(0h+val1,LEFT(0h+val2, LEN(val1)))

FUNCTION decCrypt

PARAMETER val1, val2

reTurn '' + BITXOR(0h+val1,LEFT(0h+val2, LEN(val1)))
``````
0

LVL 41

Expert Comment

I would also recommend VFPencryption.FLL as suggested by Olaf.
0

Author Closing Comment

This worked perfectly for my needs. Thank you so much!
0

## Featured Post

Microsoft Visual FoxPro (short VFP) is a programming language with it’s own IDE and database, ranking somewhat between Access and VB.NET + SQL Server (Express). Product Description: http://msdn.microsoft.com/en-us/vfoxpro/default.aspx (http://msd…
David Varnum recently wrote up his impressions of PRTG, based on a presentation by my colleague Christian at Tech Field Day at VMworld in Barcelona. Thanks David, for your detailed and honest evaluation!
how to add IIS SMTP to handle application/Scanner relays into office 365.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.