开发者

Text Obfuscation using base64_encode()

I'm playing around with encrypt/decrypt coding in php. Interesting stuff!

However, I'm coming across some issues involving what text gets encrypted into.

Here's 2 functions that encrypt and decrypt a string. It uses an Encryption Key, which I set as something obscure. I actually got this from a php book. I modified it slightly, but not to change it's main goal.

I created a small example below that anyone can test.

But, I notice that some characters show up as the "encrypted" string. Characters like "=" and "+". Sometimes I pass this encrypted string via the url. Which may not quite make it to my receiving scripts. I'm guessing the browser does something to the string if certain characters are seen. I'm really only guessing.

is there another function I can use to ensure the browser doesn't touch the string? or does anyone know enough php bas64_encode() to disallow certain characters from being used? I'm really not going to expect the latter as a possibility. But, I'm sure there's a work-around.

enjoy the code, whomever needs it!

define('ENCRYPTION_KEY', "sjjx6a"); 

function encryp开发者_如何学Got($string) {

  $result = '';
  for($i=0; $i<strlen($string); $i++) {
    $char = substr($string, $i, 1);
    $keychar = substr(ENCRYPTION_KEY, ($i % strlen(ENCRYPTION_KEY))-1, 1);
    $char = chr(ord($char)+ord($keychar));
    $result.=$char;
  }
  return base64_encode($result)."/".rand();
}

function decrypt($string){
    $exploded = explode("/",$string);
    $string = $exploded[0];
  $result = '';
  $string = base64_decode($string);
  for($i=0; $i<strlen($string); $i++) {
    $char = substr($string, $i, 1);
    $keychar = substr(ENCRYPTION_KEY, ($i % strlen(ENCRYPTION_KEY))-1, 1);
    $char = chr(ord($char)-ord($keychar));
    $result.=$char;
  }

  return $result;
}

echo $encrypted = encrypt("reaplussign.jpg");
echo "<br>";
echo decrypt($encrypted);


You could use PHP's urlencode and urldecode functions to make your encryption results safe for use in URLs, e.g

echo $encrypted = urlencode(encrypt("reaplussign.jpg"));
echo "<br>";
echo decrypt(urldecode($encrypted));


You should look at urlencode() to escape the string correctly for use in the query.


If you are worried about +,= etc. similar characters, you should have a look at http://php.net/manual/en/function.urlencode.php and it's friends from "See also" section. Encode it in encrypt() and decode at the beginning of decrypt().

If this doesn't work for you, maybe some simple substitution?

$text = str_replace('+','%20',$text); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜