简体   繁体   English

PHP和Mcrypt仅用于字母隐窝吗?

[英]PHP and Mcrypt for alphabet only crypts?

Below is my current encryption function. 下面是我当前的加密功能。

define('ENCRYPT_KEY', 'ldkKKmeJddeFffKdjeddd'); 

function market_dock_api_encrypt($string) {
        $key = ENCRYPT_KEY; //preset key to use on all encrypt and decrypts.
        $result = '';
   for($i=0; $i<strlen($string); $i++) {
     $char = substr($string, $i, 1);
     $keychar = substr($key, ($i % strlen($key))-1, 1);
     $char = chr(ord($char)+ord($keychar));
     $result.=$char;
   }
   return urlencode(base64_encode($result));
}

I just realised that this sometimes results in keys with characters such as "%" in it, and I need a key that I can use in URLs. 我只是意识到,有时这会导致键中带有诸如“%”之类的字符,因此我需要可以在URL中使用的键。 How can I change this to allow for that? 我该如何更改才能做到这一点? I was under the impression that that's what the base64_encode part does. 我的印象是base64_encode部分就是这样做的。 Obviously, I'm wrong. 显然,我错了。

The string returned is safe to use in URLs - the "%"s come from it being passed through urlencode() . 返回的字符串可在URL中安全使用-“%”来自通过urlencode()传递的字符串。 The base64_encode() was designed to pass binary data through ASCII, not specifically for URLs, so it can use "=", "+" and "/". base64_encode()旨在通过ASCII(而不是专门用于URL base64_encode()传递二进制数据,因此可以使用“ =”,“ +”和“ /”。

From the function comments: 从函数注释:

This function supports "base64url" as described in Section 5 of RFC 4648, "Base 64 Encoding with URL and Filename Safe Alphabet" 此功能支持RFC 4648第5节“使用URL和文件名安全字母进行Base 64编码”中所述的“ base64url”

<?php
function base64url_encode($plainText)
{
    $base64 = base64_encode($plainText);
    $base64url = strtr($base64, '+/', '-_');
    return ($base64url);    
}
?>

You may wish to rtrim (or escape) trailing ='s for use in a URI. 您可能希望整理(或转义)尾随=以便在URI中使用。

If you use this you'd obviously have to create a decoder as well. 如果使用此选项,则显然也必须创建一个解码器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM