encryption - Convert ecryption/decryption function from Python to PHP -


i have python script encrypt/decrypt urls:

# -*- coding: utf-8 -*- import base64 operator import itemgetter   class crypturl:     def __init__(self):         self.key = 'secret'      def encode(self, str):         enc = []         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)         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. example above url converted 29nx4p-joszs4czg2jpg4di= , decryption brings url.

now want convert php function. far encryption working fine....but decryption not....and dont know why.....so far 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();     ($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);     ($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 above decoding prints out : n>:tý\&™åª—væ not http://google.com :p said encoding function works though. why isnt decoding working ? missing ?

btw cant use other encoding/decoding function. have list of urls encoded python , want move whole system php based site....so need decode urls php function instead of python.

(use page execute python: http://www.compileonline.com/execute_python_online.php)

any appreciated. -thanks

double check syntax of strtr().

i'd suggest using in in following way:

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

make sure have your_replace_character!

also, i'm sure you'll handle reverse function, need flip values of replace array.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -