[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4872
  • Last Modified:

VFP Encrypt Decrypt

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))

Open in new window


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
formadmirer
Asked:
formadmirer
1 Solution
 
formadmirerAuthor Commented:
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"
password = "5551212"

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

enctxt = ENCRYPT(plaintext,password)

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

decpt = ENCRYPT(ENCRYPT(plaintext,password),password)

	WAIT WINDOW "your text decrypted: " + decpt

Open in new window


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
 
CaptainCyrilCommented:
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.

CHR(MOD(ASC(SUBSTR(cString,n,1))+ASC(SUBSTR(cPassword,n,1)),256))

You need the MOD 256 because the remainder is not more than 255.
0
 
Olaf DoschkeSoftware DeveloperCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
pcelbaCommented:
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)))

Open in new window

0
 
pcelbaCommented:
An answer to your second question: You have to add cipher50.fll into the project. More info: http://www.foxite.com/archives/cipher50fll-0000326377.htm

I would also recommend VFPencryption.FLL as suggested by Olaf.
0
 
formadmirerAuthor Commented:
This worked perfectly for my needs. Thank you so much!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now