Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Type of field to store encrypted value
Message
 
 
To
03/04/2019 09:47:08
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01667839
Message ID:
01667880
Views:
49
>>>>Hi,
>>>>
>>>>I am using cipher() function to encrypt a long string (250 char). Then I need to store the encrypted value into a field of a SQL Server table. Can the field be type CHAR(250)? or it must be NCHAR(250)?
>>>>
>>>>TIA
>>>
>>>I would ditto on Naomi, it should be binary, unless you also convert it to base64 encoded value. Otherwise, you are in danger of losing data, be it char or nchar. BTW cipher() function, what is that? I only assumed it was something doing encryption using one the known algorithms.
>>
>>Yes, cipher() is a function that does encryption. Sergey Berezniker converted it from C to VFP some many years ago. I don't know who wrote the original (C-version) of this function.
>
>Is that a special encryption algorithm that never return non-printable characters and also with an exact size? If so you can use char(N). nchar() uses unicode.
>
>If it might return a non-printable character and size is not fixed (both case are the usual cases with encryption algorithms) then varbinary(N) or varbinary(MAX) is the choice. You might instead use varchar(Max), provided you are converting the value to base64 encoded - StrConv(encrypted, 13).

Just in case you are curious, here is the code of the cipher function:
FUNCTION cipher
LPARAMETERS tcStr, tcPassword

#define   ERRORNEGL    -1L
#define   PW_MIN_LEN   3        	&&  /* number of chars in password */
#define   PW_MIN_NUM   1000 	  	&&  /* number of digits in password return number */

LOCAL lnStrLen, lnPassLen, lnPassNum, laPassword[1,2], lcPassword
LOCAL lcStrOut, lnPassPos, lnNum01, lcStrOut, lnInPos, lnPassPos

IF TYPE("tcStr") <> "C" ;
		OR TYPE("tcPassword") <> "C" ;
		OR LEN(tcPassword) < PW_MIN_LEN
	Error 11
ENDIF

lnStrLen = LEN(tcStr)

* Because of the bug in the original C code we've to add CHR(0) to the password
* 		and use it later
lcPassword = tcPassword + CHR(0)
lnPassLen = LEN(lcPassword)
DIMENSION laPassword[lnPassLen+1,2]
FOR lnPassPos=1 TO lnPassLen
	laPassword[lnPassPos,2] = SUBSTR(lcPassword,lnPassPos,1)
	laPassword[lnPassPos,1] = ASC(laPassword[lnPassPos,2])
ENDFOR

* Get seed value
lnPassNum = INT((((CipherGetPnum(lcPassword)/997) - 1) % 254) + 1 )
lcStrOut = ""
lnPassPos = 1

* Encode/decode each character
FOR lnInPos=0 TO lnStrLen-1
	* Get new seed value
	lnNum01 = (( lnPassNum + (lnInPos - lnStrLen)) - 1)
	lnPassNum = (ABS(lnNum01) % 254) * SIGN(lnNum01) + 1
	* Encode current character
	lnByte = BITXOR( ASC(SUBSTR(tcStr,lnInPos+1,1)), ;
		BITXOR(lnPassNum, laPassword[lnPassPos,1]))
	* Convert signed value to unsigned, if necessary
	lnByte = BITAND(lnByte, 0xFF)
	* If result is zero, use current character
	lcStrOut = lcStrOut + IIF(lnByte = 0, SUBSTR(tcStr,lnInPos+1,1), CHR(lnByte))
	* Advance to the next password character
	lnPassPos = IIF( lnPassPos => lnPassLen, 1, lnPassPos + 1)
ENDFOR

RETURN lcStrOut

* Returns a seed value based on the string passed as parameter
FUNCTION CipherGetPnum(tcStr)
	LOCAL liRet, lnPos
	liRet = 1
	FOR lnPos=0 TO LEN(tcStr ) - 1
		liRet = liRet + ASC(SUBSTR(tcStr,lnPos+1,1)) + lnPos
	ENDFOR
	DO WHILE (liRet < PW_MIN_NUM)
		liRet = BITLSHIFT(liRet,1)
	ENDDO
	RETURN liRet
ENDFUNC
"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