 # Encrypting with Arrays

I am an absolute neophyte, so please bear with me. I am trying to decipher a bit of code that is used on our website that changes a number to a code for use with a shortened URL. We start with a number that links to a specific page on the website, encode it and then append it to a URL. I cannot find how the encoding function works. The number to encode is anything between a one and seven digit number. Here is the function that encodes the number:

``````function encryptZip(\$zip) {
\$theSource = Array("8","v","w","M","P","I","N","o","Z","h","g","_","u","W","D","-","b","O","k","a","G","t","U","j","6","7","K","A","r","y","l","5","X","p","n","z","F","4","Y","d","2","9","q","V","s","0",".","e","H","c","x","Q","B","S");
\$r = '';
while (\$zip>0){
\$num = \$zip % 53;
\$zip = (\$zip - \$num) / 53;
\$r .= \$theSource[\$num];
}
return \$r;
}
``````

Thanks in advance for any help,
RDW

What do you think it does?

You have the encryption key, and how it arrives at the values, how it builds the new string.

This is my problem. Somehow this takes a number like 1234567 and returns it as a short code, for example “1471463” becomes “6s.h” (no quotes). I feel like I’m missing something here but it is basically the same function that decodes it on the other end.

Step thru it.

It pulls the number apart; does some math on it; then uses that as a key to what character to replace it with.

This is good…a start for my education in array usage. But I am truly a neophyte and while I understand the part about doing the math on it (although I don’t know where 53 came from), I don’t get how that array is sequentially converting a one to seven digit number to a uniform four digit code that can be de-encrypted back to however many digits. I’m baffled.

The goal of this is, first of all to understand how it works but temporarily to build a function or a formula in Excel that I can give some of my workmates to do some ad hoc conversions for producing some one-off URLs that can be decoded by our website.

Run the code in its individual parts to see what each things does.

Thanks Astonecypher, I will try that

Sponsor our Newsletter | Privacy Policy | Terms of Service