Home > Archive > Clipper > August 2006 > RATECRYPT() Version 1.2 included below
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
RATECRYPT() Version 1.2 included below
|
|
| Mel Smith 2006-08-18, 7:55 am |
| Dear Friends,
Here is Version 1.2 of RateCrypt() below.
Thanks to all
-Mel Smith
------------------- snip -----------------------------
FUNCTION RATECRYPT(cCRYPT,nRATE,cEMPNUM)
// Author: Mel Smith, Aug 17, 2006
// With Lots of Help from the Guys on the c.l.g newsgroup
// Aug 18/06 -- Version 1.2
// 1. Removed Duplicate Code / Re-structured
// 2. Changed NUROUND() to standard ROUND()
// (Thanks to BillRobertson)
// 3. Changed the nMININC 'define' to lessen 'loops'
// (But would like to find a way to eliminate this step)
/*
My wish with this function is to create an encrypted rate which will
'fit' in the same field as the actual rate, but will have a value
that will be obvious (to my function) as either encrypted (or actual).
Thus, this function allows the programmer to 'obscure' an employee's
hourly
Rate from casual viewing (using database viewing software) by his peers.
The technique uses a pseudo Random Number generator copied/modified
from NanForum's rand1.prg
The mods include using the '%' operator (instead of the MOD() function),
and later including an organization's own code (nORGCODE) to
differentiate
it from other orgs who would also use this function.
I also have to deal with variously-structured 'Employee Numbers' which
require some extra coding below. *My* Employee numbers are (for now)
either, for example, 'R1234' or '59028'. So I have to use either
*all* digits or the right-most 4 digits.
*/
#define nB 31415621 // Good old 'PI' is used here
#define nM 100000000 // The widest possible range of values
#define nMIN 60000 // Minimum returned value from encryption
#define nMAX 99999 // Maximum returned value from encryption
#define nMININC 10000 // To 'boost' the nCODEBASE to within limits ----
Version 1.2 change
#define nORGCODE 1234 // Choose Your own private org's code here --
NEVER CHANGE !
#define nMINLENEMP 5 // Minimum input allowable length of employee
number
#define nMINCODERATE 400 // Any value > nMINCODERATE is already encrypted
LOCAL I,nSEED,nEMPVAL,nCODEBASE,nCODERATE,cDIG
STR,nDECRYPRATE
GABORT := .T. // Global 'Abort' indicator for Calling routine
// Assume failure at the outset
IF EMPTY(nRATE) // I won't encrypt a 'zero' Rate
RETURN nRATE
ENDIF
cEMPNUM := ALLTRIM(cEMPNUM)
IF EMPTY(cEMPNUM) .OR. LEN(cEMPNUM) < nMINLENEMP // Faulty Employee Number
RETURN nRATE
ENDIF
// Try to use *both* the n-digit Employee Numbers and also
// the letter + (n-1)-digit Employee Numbers used by other related orgs.
IF ISDIGIT(cEMPNUM) // THEN WE CAN USE ALL n DIGITS
nEMPVAL := VAL(cEMPNUM)
ELSE // ELSE ONLY USE THE RIGHT-MOST n-1 DIGITS
IF .NOT. ISDIGIT(cDIGSTR:=SUBSTR(cEMPNUM,2,10)) // THEN WE HAVE A
NON-CONFORMING EMPNUM
RETURN nRATE
ENDIF
nEMPVAL := VAL(cDIGSTR)
ENDIF
// Now we have a proper Employee 'Number' to work with as part of our seed
// nCodeBase calcs was moved and structure below changed from 1st version
// Modify the 'Seed' for your own organization's purposes
nSEED := nEMPVAL + nORGCODE // The seed is different for each different
// Employee Number (+ the Org's own code)
// This next line is from the NanForum 'Rand1.prg' module and
// authors Gary Baren and 'Glenn'
nCODEBASE := nMAX * (( (nSEED * nB + 1) % nM ) / nM)
// Now bring the nCODEBASE within your org's limits
DO WHILE nCODEBASE < nMIN
nCODEBASE += nMININC // I don't like this loop but what else can I do
?
ENDDO
// Version 1.2 uses the 'normal' round() function below (not Fleming
Ho's) -- Kudos to Bill Robertson
nCODEBASE := ROUND(nCODEBASE*.01,2) // Result 500.00 thru 999.99
cCRYPT := ALLTRIM(UPPER(cCRYPT))
IF cCRYPT == "E" // Means to 'E'ncrypt
IF nRATE > nMINCODERATE // Already encrypted
RETURN nRATE // It is 'already' encrypted -- a Programming Fault
:((
ENDIF
nCODERATE := nCODEBASE - nRATE // and establish the Coded Rate
GABORT := .F. // Signal globally that this encryption worked.
RETURN nCODERATE // and Return this Coded Rate
ELSEIF cCRYPT="D" // MEANS 'D'ecrypt
IF nRATE <= nMINCODERATE // Already encrypted
RETURN nRATE // It is 'already' encrypted -- a Programming Fault
:((
ENDIF
nDECRYPRATE := ABS(nRATE - nCODEBASE) // and establish the
Coded Rate
GABORT := .F. // Signal globally that this encryption worked.
RETURN nDECRYPRATE // and Return this Coded Rate
ENDIF
GABORT := .T. // Version 1.2 addition here
RETURN nRATE
| |
| bill robertson 2006-08-18, 6:55 pm |
| Mel Smith wrote:
> Dear Friends,
>
> Here is Version 1.2 of RateCrypt() below.
Hi Mel
Perhaps you will be interested in a mathematically equivalent version of
your routine. I took the rand1.prg function as you did and scaled it so
that it would generate numbers in the 60000 - 99999 range that you
wanted to use. The method uses no round() function and like the xor
function requires no separate encrypt/decrypte routine.
It is as secure as the rand1() function which isn't all that secure but
certainly should suffice to obscure the rate. It might be slightly more
secure if the EmpNo was a prime number. Some of the lines may wrap but I
cut and pasted so it should compile OK and show you some typical results
for a fixed rate of 12.50
/*----------------------------------------------------------*/
#define nMin 60000
#define nMax 99999
#define nOrg 1234
Function main()
LOCAL nRow:= 1, nCol:= 0
LOCAL nRate:= 12.50, nEmpNo:= 21343
Clear screen
While nCol < 80
@ nRow++, nCol say ( nRate:= ft_rand1( nRate, nEmpNo ) ) Picture
"#####.##"
@ nRow++, nCol say ( nRate:= ft_rand1( nRate, nEmpNo ) ) Picture
"#####.##"
++nEmpNo
If nRow > 20
nRow := 1
nCol += 10
END if
END while
return nil
function ft_rand1(nRate, nEmpNo)
LOCAL nSeed:= nOrg + nEmpNo
LOCAL m := 100000000, b := 31415621
LOCAL nDiff:= nMax - nMin
Return Int(nMin+nDiff*((nSeed:=(nSeed*b+1)%m))/m)-nRate
| |
| Mel Smith 2006-08-19, 7:55 am |
| Bill,
It looks short and elegant.
I'll give it a try this afternoon.
If it works under my own org's circumstances I'll certainly use it.
Thank you !
-Mel Smith
|
|
|
|
|