PHP 中的 HexDec 到 Char 到密码解密
好的,所以我试图弄清楚如何从十六进制加密到字符串再返回,但由于某种原因,当我加密时,我没有得到传入的相同密码,但它接近......奇怪正确的?我确信我的语法在某个地方是错误的。帮助?
这是一个测试随机密码的链接。我已将“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仔细看看这一行:
您将值减少
$i
。因此,每次增量,值将是$literals[$i] - $i
。这是上面的逆过程:
Look closer at this line:
You're decreasing the value by
$i
. So every increment, the value will be$literals[$i] - $i
.This is the reverse of the above:
感谢上面 Lekensteyn 提供的反转代码。在我的测试中,似乎 Dreamweaver 也需要大写字母,因此我添加了一个 strtoupper,如下所示。我使用此代码从域和 ftp 凭据列表动态生成 Dreamweaver 站点定义文件 (.ste)。
还要感谢 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.
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!
十六进制不是一种加密,充其量只是一种编码(实际上它主要是一个数字基数)。看起来您正在混淆基本转换和字符编码本身的十六进制编码。
hexdec
会将 0、1、9、A、F、10 分别转换为 0、1、9、10、15、16。chr
会将十进制数字转换为字符(使用 ASCII 字符编码)$i
。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)$i
in the value you got.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 ofchr(hexdec(...))
.Overall, it's not clear what kind of password protection you're trying to achieve here, since it's just encoding.