Archived Forum Post

Index of archived forum posts

Question:

ASCII encryption incorrect?

Apr 21 '16 at 01:15

In examples (http://www.example-code.com/foxpro/crypt2_arc4.asp) there is example for ARC4 encryption:

loCrypt.CryptAlgorithm = "arc4"
loCrypt.EncodingMode = "hex"
lcKeyHex = "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"
loCrypt.SetEncodedKey(lcKeyHex,"hex")
lcEncStr = loCrypt.EncryptStringENC("The quick brown fox jumps over the lazy dog.")
? lcEncStr
lcDecStr = loCrypt.DecryptStringENC(lcEncStr)
? lcDecStr

and this code works perfectly. But when I change encoding type from "hex" to "ascii"

loCrypt.EncodingMode = "ascii"

then EncryptStringENC method does not return all characters (it should be the same number as in encrypted text). It is evident that characters with ASCII value higher than 127 are deleted.

Also DecryptStringENC does not accept encrypted char string that contains binary chars (chars with ASCII > 127).

Is this by design or is this a bug? How I can get binary coded string with encoded text? And how I can put binary string with encoded text to DecryptStringENC method? Normally ARC4 encryption is symmetrical and providing this code

loCrypt.EncryptStringENC(loCrypt.EncryptStringENC("Text to encrypt"))  && Yes, there is EncryptStringENC used twice

should return the original "Text to encrypt" string.

The only method I have found is recode string to hexa, use "hex" EncodingMode, use "hex" CharSet and encode again from hexa to char string. So my universal (encoding and/or decoding) function looks like:

FUNCTION aRC4(input_text, secure_key)
LOCAL lcTxt, lcOut
loCrypt.CryptAlgorithm = "arc4"
loCrypt.SetEncodedKey(secure_key,"ascii")
loCrypt.EncodingMode = "hex"    && Output will be in hexa
loCrypt.charset = "hex"     && Input will be in hexa
lcTxt=loCrypt.EncodeString(input_text,'ansi','hex') && recode text to hexa code
lcOut = loCrypt.EncryptStringENC(lcTxt)
lcOut = loCrypt.DecodeString(lcOut,'ansi','hex')    && Recode hexa output to binary text
RETURN lcOut

But this looks little bit complicated.

Thanks for your help.

Vladimir


Answer

It looks the original question was too complicated as nobody answered it yet.

So short clear question: Should the ASCII encoding (EncodingMode="ascii") return characters from the whole (extended ASCII) range (chars with ASCII codes 0 to 255) or only characters from the pure US ASCII (chars with ASCII codes from 0 to 127)?

Vladimir