php rot13解密,用PHP实现ROT13

"这篇博客介绍了如何使用PHP解密ROT13加密的字符串。通过遍历字符串,对每个字符进行ASCII值的加减运算,判断是否需要翻转字母表,并输出解密后的结果。示例代码展示了如何实现这一过程,最终得到的解密文本是:""IfyouaskedBruceSchneiertodecryptthis,he’dcrushyourskullwithhislaugh.""
摘要由CSDN通过智能技术生成

我在阅读了有关Jon Skeet的有趣内容之后找到了一个字符串,我猜这是在ROT13中.在检查我的猜测之前,我想我会尝试用PHP解密它.这就是我的所作所为:

$string = "Vs lbh nfxrq Oehpr Fpuarvre gb qrpelcg guvf, ur'q pehfu lbhe fxhyy jvgu uvf ynhtu.";

$tokens = str_split($string);

for ($i = 1; $i <= sizeof($tokens); $i++) {

$char = $tokens[$i-1];

for ($c = 1; $c <= 13; $c++) {

$char++;

}

echo $char;

}

我的字符串回来了,如你自己aabakaead ABruacae Sacahnaeaiaer到adaeacrypt tahais,ahae’ad acrusah你的sakualal waitah ahais alaauagah.

我的逻辑似乎很接近,但显然是错的.你能帮帮我吗?

解决方法:

这是一个有效的实现,不使用嵌套循环.您也不需要将字符串拆分为数组,因为您可以像使用PHP中的字符串数组一样索引单个字符.

您需要知道ASCII大写字符的范围是65 – 99,小写字符的范围是97 – 122.如果当前字符在其中一个范围内,则将ASCII值加13.然后,检查是否应该转到字母表的开头.如果你应该翻身,减去26.

$string = "Vs lbh nfxrq Oehpr Fpuarvre gb qrpelcg guvf, ur'q pehfu lbhe fxhyy jvgu uvf ynhtu.";

for ($i = 0, $j = strlen( $string); $i < $j; $i++)

{

// Get the ASCII character for the current character

$char = ord( $string[$i]);

// If that character is in the range A-Z or a-z, add 13 to its ASCII value

if( ($char >= 65 && $char <= 90) || ($char >= 97 && $char <= 122))

{

$char += 13;

// If we should have wrapped around the alphabet, subtract 26

if( $char > 122 || ( $char > 90 && ord( $string[$i]) <= 90))

{

$char -= 26;

}

}

echo chr( $char);

}

这会产生:

If you asked Bruce Schneier to decrypt this, he’d crush your skull with his laugh.

标签:php,encryption,rot13

来源: https://codeday.me/bug/20190714/1455676.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值