0

I have this Python script to encrypt/decrypt URLs:

# -*- coding: utf-8 -*-
import base64
from operator import itemgetter


class cryptUrl:
    def __init__(self):
        self.key = 'secret'

    def encode(self, str):
        enc = []
        for i in range(len(str)):
            key_c = self.key[i % len(self.key)]
            enc_c = chr((ord(str[i]) + ord(key_c)) % 256)
            enc.append(enc_c)
        return base64.urlsafe_b64encode("".join(enc))

    def decode(self, str):
        dec = []
        str = base64.urlsafe_b64decode(str)
        for i in range(len(str)):
            key_c = self.key[i % len(self.key)]
            dec_c = chr((256 + ord(str[i]) - ord(key_c)) % 256)
            dec.append(dec_c)
        return "".join(dec)

url = "http://google.com";
print cryptUrl().encode(url.decode('utf-8'))

This works fine. For example the above url is converted to 29nX4p-joszS4czg2JPG4dI= and the decryption brings it back to the URL.

Now i want to convert this to a PHP function. So far encryption is working fine....but decryption is not....and i dont know why.....so far i got this:

function base64_url_encode($input) {
    return strtr(base64_encode($input), '+/=', '-_');
}

function base64_url_decode($input) {
    return strtr(base64_decode($input), '-_', '+/=');
}   

function encode ($str)
{
    $key = 'secret';

    $enc = array();
    for ($i;$i<strlen($str);$i++){
        $key_c = $key[$i % strlen($key)];
        $enc_c = chr((ord($str[$i]) + ord($key_c)) % 256);
        $enc[] = $enc_c;
    }
  return base64_url_encode(implode($enc));
}


function decode ($str)
{
    $key = 'secret';

    $dec = array();
    $str = base64_url_decode($str);
    for ($i;$i<strlen($str);$i++){
        $key_c = $key[$i % strlen($key)];
        $dec_c = chr((256 + ord($str[$i]) + ord($key_c)) % 256);
        $dec[] = $dec_c;
    }
  return implode($dec);
}


$str = '29nX4p-joszS4czg2JPG4dI=';
echo decode($str);

Now the above decoding prints out : N>:Tý\&™åª—Væ which is not http://google.com :p Like i said encoding function works though. Why isnt the decoding working ? What am i missing ?

Btw i cant use any other encoding/decoding function. I have a list of URLs encoded with python and i want to move the whole system to a PHP based site....so i need to decode those URLs with a PHP function instead of python.

(Use this page to execute Python: http://www.compileonline.com/execute_python_online.php)

1 Answer 1

1

Double check the syntax of strtr().

I'd suggest you using in in the following way:

strtr(
    base64_encode($input),
    array(
        '+' => '-',
        '/' => '_',
        '=' => YOUR_REPLACE_CHARACTER
    )
)

Make sure you have YOUR_REPLACE_CHARACTER!

Also, I'm sure you'll handle the reverse function, where you need to simply flip the values of the replace array.

Sign up to request clarification or add additional context in comments.

3 Comments

My above encode function works fine. Like you said i simply flip the values in my decode function and then do the decoding...like in the python script but thats not working.....THAT is my problem...not the encode function.
Please, read my answer again. You're using strtr() the wrong way. Use it like I've wrote and it'd be fine.
A shorter way to do the same: strtr(base64_encode($input), "+/=" ,"-_X"), where X is your replace character.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.