使用 base64_encode() 进行文本混淆

发布于 2024-08-28 09:59:25 字数 1263 浏览 5 评论 0原文

我正在 php 中玩加密/解密编码。有趣的东西!
但是,我遇到了一些涉及文本加密内容的问题。

这是加密和解密字符串的两个函数。它使用一个加密密钥,我将其设置为模糊的东西。 我实际上是从一本 php 书中得到的。我稍微修改了它,但没有改变它的主要目标。

我在下面创建了一个任何人都可以测试的小示例。
但是,我注意到某些字符显示为“加密”字符串。 “=”和“+”等字符。 有时我通过 url 传递这个加密的字符串。这可能不太适合我的接收脚本。我猜测如果看到某些字符,浏览器会对字符串执行某些操作。我真的只是猜测。

我可以使用另一个函数来确保浏览器不会触及字符串吗?或者有人知道足够的 php bas64_encode() 来禁止使用某些字符吗?我真的不认为后者是一种可能性。但是,我确信有解决方法。

享受代码,无论谁需要它!

define('ENCRYPTION_KEY', "sjjx6a"); 

function encrypt($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);

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 encrypt($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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南汐寒笙箫 2024-09-04 09:59:25

您可以使用 PHP 的 urlencodeurldecode 函数使您的加密结果可以安全地在 URL 中使用,例如

echo $encrypted = urlencode(encrypt("reaplussign.jpg"));
echo "<br>";
echo decrypt(urldecode($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));
月朦胧 2024-09-04 09:59:25

您应该查看 urlencode() 正确转义字符串以便在询问。

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

最笨的告白 2024-09-04 09:59:25

如果您担心 +、= 等类似字符,您应该查看 http ://php.net/manual/en/function.urlencode.php 以及“另请参阅”部分的朋友。在 encrypt() 中对其进行编码,并在解密() 的开头进行解码。

如果这对您不起作用,也许可以进行一些简单的替代?

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

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); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文