Forum Moderators: coopster

Message Too Old, No Replies

How to jumble a number

loooking for the easiest way in php

         

Clark

5:06 pm on Jun 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd like to take an integer like:

4152390867

and jumble it beyond recognition but still be able to decrypt it. IOW, say every instance of

0=8
1=3
2=5
3=7
4=1
5=2
6=9
7=4
8=6
9=0

so if variable
$orignum=4152390867

How could I create a simple function to pass $orignum to the function and turn it into:

$orignum=1325708694

and then when I need the original number back for the function to return it to it's original?

Thanks.

daisho

6:07 pm on Jun 13, 2003 (gmt 0)

10+ Year Member



function Scramble ($numin) {

$key=array(8,3,5,7,1,2,9,4,5,0);

$numout="";
$len=strlen($numin)
for($x=0;$x<$len;$x++) {
$tmp=substr($numin,$x,1);
$numout.=$key[$tmp];
}

return $numout;
}

Clark

6:33 pm on Jun 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Excellent, Thanks I'll try it. One question though to help me with my other functions (I haven't made too many functions.)

Why

$numout="";

and

return $numout;

?

I would have probably just made sure there is no variable called $numout in the data, done a

global $numout;

and that's it...but wondering if my way is a security issue? Or a worse way?

Also, will I then be able to use $numout anywhere after I call the function or do I need to call the function a special way to access it?

Sorry, I know these are dummy questions but I get confused on functions and globals vs locals sometimes...

daisho

6:48 pm on Jun 13, 2003 (gmt 0)

10+ Year Member



Bigger chance of variable collision and polution.

This way just do:

$myvar=Scramble(123456789);

then you could do:

$test2=Scramble(768574656);

If you did it with global vars the second call would clear out the first.

As for setting the var to "" first I have a C background and it's been ingrained into me to _always_ initialize variables to a known state. In PHP I don't think this is required but I like to do it anyway :)

daisho.

jatar_k

6:52 pm on Jun 13, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



In PHP I don't think this is required

I think it mainly depends on the error_reporting level. I try to initialize because sometimes it throws a warning on some servers and for the C thing.

Clark

7:03 pm on Jun 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I *wish* it was ingrained in me... I never had security problems because of it but I've had times when I thought about security later and go through the whole code initializing variables that need to be unless they are set specifically...but I get confused sometimes. Self-taught programming is fun but there is a reason for those classes... You wouldn't believe how long I was coding using variables not even knowing what it was called and why. I intuitively "got it" but not knowing why. Then one day of reading somewhere about a "variable" and trying to understand what it meant, LIGHTBULB. Duuh, heh heh. Well, at least that was some years ago...

I digress..anyways, I didn't look at your code in detail yet, I will after I finally get my coffee and when I go back and try and implement it, but does it decrypt *as well as* encrypt? Or do I have to make an opposite function to decrypt?

Clark

6:47 am on Jun 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, finally got to this today and testing it right now. Boy I must have been totally out of it that day to ask those questions. Now that I looked at the code properly all makes sense. Thank you!