PHP 中的 HexDec 到 Char 到密码解密

发布于 2024-09-24 05:02:49 字数 929 浏览 5 评论 0原文

好的,所以我试图弄清楚如何从十六进制加密到字符串再返回,但由于某种原因,当我加密时,我没有得到传入的相同密码,但它接近......奇怪正确的?我确信我的语法在某个地方是错误的。帮助?

这是一个测试随机密码的链接。我已将“pw”设置为十六进制版本并将其传递给: 查看演示

$encoded  = (string)$_GET['pw'];  
$literals = explode(' ', wordwrap($encoded, 2, ' ', 2));  
$password = '';  

for ($i = 0; $i < count($literals); $i++) {  
    $password .= chr(hexdec($literals[$i]) - $i);  
}

echo $password . '<br />';
print_r($literals) . '<br />';
$passarray = str_split($password);

echo '<pre>';
print_r($passarray);
echo '</pre>';

for($i = 0; $i < count($passarray); $i++) {
    $newpassword .= bin2hex($passarray[$i]);
}
echo $newpassword;

更新:

我实际上并不是为了安全而尝试“加密”...我实际上是在尝试自动创建 Dreamweaver 站点定义文件,这是他们用来半隐藏的“加密”密码。我已经可以通过复制文件并插入不同的用户名来做到这一点,但我也想每次都发出新密码。仅供参考

Ok, so I'm trying to figure out how to go from hexadecimal encryption to string and back, but for some reason the when I encrypt, i'm not getting the same password that was passed in, but its close...weird right? I'm sure my syntax is wrong somewhere. Help?

Here's a link to test a little random password. I've set 'pw' as the hexadecimal version and pass that in: view demo

$encoded  = (string)$_GET['pw'];  
$literals = explode(' ', wordwrap($encoded, 2, ' ', 2));  
$password = '';  

for ($i = 0; $i < count($literals); $i++) {  
    $password .= chr(hexdec($literals[$i]) - $i);  
}

echo $password . '<br />';
print_r($literals) . '<br />';
$passarray = str_split($password);

echo '<pre>';
print_r($passarray);
echo '</pre>';

for($i = 0; $i < count($passarray); $i++) {
    $newpassword .= bin2hex($passarray[$i]);
}
echo $newpassword;

UPDATE:

.I'm actually not trying to "encrypt" for security per se...I'm actually trying to automate the creation of Dreamweaver Site Definition files and this is the "encryption" they use to semi-hide the password. I can do this already by copying the file and inserting different usernames, but I'd like to issue and new password each time as well. just FYI

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

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

发布评论

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

评论(3

夜访吸血鬼 2024-10-01 05:02:49

仔细看看这一行:

$password .= chr(hexdec($literals[$i]) - $i);

您将值减少 $i。因此,每次增量,值将是 $literals[$i] - $i

这是上面的逆过程:

$newpassword .= dechex(ord($passarray[$i]) + $i);

Look closer at this line:

$password .= chr(hexdec($literals[$i]) - $i);

You're decreasing the value by $i. So every increment, the value will be $literals[$i] - $i.

This is the reverse of the above:

$newpassword .= dechex(ord($passarray[$i]) + $i);
╰つ倒转 2024-10-01 05:02:49

感谢上面 Lekensteyn 提供的反转代码。在我的测试中,似乎 Dreamweaver 也需要大写字母,因此我添加了一个 strtoupper,如下所示。我使用此代码从域和 ftp 凭据列表动态生成 Dreamweaver 站点定义文件 (.ste)。

function dw_encode_pw($pw='') {
    $password = '';
    $passarray = str_split($pw);

    for($i = 0; $i < count($passarray); $i++) {
        $password .= strtoupper(dechex(ord($passarray[$i]) + $i));
    }
    return $password;
}

function dw_decode_pw($pw='') {
    if (empty($pw)) {
        return false;
    }

    $encoded  = (string) $pw;
    $literals = explode(' ', wordwrap($encoded, 2, ' ', 2));
    $password = '';
    for ($i = 0; $i < count($literals); $i++) {
        $password .= chr(hexdec($literals[$i]) - $i);
    }

    return $password;
}

还要感谢 http://blog .affirmix.com/2008/08/28/encoded-passwords-in-ste-site-definition-files/http://www.matthewratzloff.com/blog/2007/04/18/decrypting-a-dreamweaver-site-definition- password/ 也探讨了这个主题...

希望这可以帮助其他人寻找这个解决方案!

Thanks to Lekensteyn's above for the reversal code. In my testing it seemed dreamweaver was also expecting uppercase letters, so i added a strtoupper as shown below. I was using this code to dynamically generate dreamweaver site definition files (.ste) from a list of domains and ftp credentials.

function dw_encode_pw($pw='') {
    $password = '';
    $passarray = str_split($pw);

    for($i = 0; $i < count($passarray); $i++) {
        $password .= strtoupper(dechex(ord($passarray[$i]) + $i));
    }
    return $password;
}

function dw_decode_pw($pw='') {
    if (empty($pw)) {
        return false;
    }

    $encoded  = (string) $pw;
    $literals = explode(' ', wordwrap($encoded, 2, ' ', 2));
    $password = '';
    for ($i = 0; $i < count($literals); $i++) {
        $password .= chr(hexdec($literals[$i]) - $i);
    }

    return $password;
}

also thanks to http://blog.affirmix.com/2008/08/28/encoded-passwords-in-ste-site-definition-files/ and http://www.matthewratzloff.com/blog/2007/04/18/decrypting-a-dreamweaver-site-definition-password/ which also explore this topic...

Hope this helps somebody else looking for this solution!

沉睡月亮 2024-10-01 05:02:49

十六进制不是一种加密,充其量只是一种编码(实际上它主要是一个数字基数)。看起来您正在混淆基本转换和字符编码本身的十六进制编码。

  • hexdec 会将 0、1、9、A、F、10 分别转换为 0、1、9、10、15、16。
  • chr 会将十进制数字转换为字符(使用 ASCII 字符编码)
  • 不确定为什么要在得到的值中减去 $i
  • 另一方面,bin2hex 有效地为您提供了二进制数据序列的十六进制表示(至少可以打印出来)。它不一定与 chr(hexdec(...)) 相反。

总的来说,目前尚不清楚您要在这里实现哪种密码保护,因为它只是编码。

Hexadecimal isn't an encryption, but an encoding at best (it's mostly a number base in fact). It looks like you're mixing up the base conversion, and the hexadecimal encoding of the character encoding itself.

  • hexdec will turn 0, 1, 9, A, F, 10 into 0, 1, 9, 10, 15, 16, respectively.
  • chr will turn the decimal number into a character (using the ASCII character encoding)
  • Not sure why you're substracting $i in the value you got.
  • On the other side, bin2hex effectively gives you an hexadecimal representation of a binary sequence of data (at least so that it can be printed out). It's not necessarily the opposite of chr(hexdec(...)).

Overall, it's not clear what kind of password protection you're trying to achieve here, since it's just encoding.

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