[南邮OJ](密码学)骚年来一发吗
2016-10-05 本文已影响403人
王一航
题目链接:
骚年来一发吗 250
密文:iEJqak3pjIaZ0NzLiITLwWTqzqGAtW2oyOTq1A3pzqas

分析:
题目提供了一个密文还有一个加密代码 , 所以我们要做的只是需要将这段加密代码逆掉就可以啦
首先我们先来分析一下题目提供的加密函数 :
<?php
function_encode($str){
$_o = strrev($str); // 将传进的字符串反转
for($_0 = 0; $_0 < strlen($_o); $_0++){
$_c = substr($_o, $_0, 1); // 循环遍历反转后的字符串的所有字符
$__ = ord($_c) + 1; // 将每个字符串的ASCII加1
$_c = chr($__); // 然后将新的字符串赋值给一个新的变量
$_ = $_.$_c; //将这些新的字符进连接
}
return str_rot13(strrev(base64_encode($_))); // 将得到的新字符串Base64加密后再反转然后再求Rot-13 , 最后返回
}
?>
这里笔者将加密和解密函数都封装成了独立的函数 , 也可以直接拿来调用 , 废话不多说直接上代码 :
# coding:utf8
import base64
def rot13(words, OffSet=13):
'''
功能 :
Rot-13加密解密
参数 :
a :
OffSet : 偏移量
返回 :
Rot-13加密后的值
备注 :
由于Rot-13是循环的 , 因此一段密文进行了rot-13加密以后
再加密一次就可以得到明文 , 因此rot-13只有加密/解密一个函数
(类似于xor)
'''
def encodeCh(ch):
f=lambda x: chr((ord(ch)-x+OffSet) % 26 + x)
return f(97) if ch.islower() else (f(65) if ch.isupper() else ch)
return ''.join(encodeCh(ch) for ch in words)
def encode(str):
'''
功能 :
自定义加密函数
参数 :
str : 明文
返回 :
密文
'''
result = ""
str = str[::-1] # 使用Python中的字符串切片功能对字符串进行反转
for ch in str:
result = result + chr(ord(ch) + 1)
return rot13(base64.b64encode(result)[::-1])
def decode(words):
'''
功能 :
自定义解密函数
参数 :
str : 密文
返回 :
明文
'''
result = ""
words = rot13(words)
words = words[::-1] # 使用Python中的字符串切片功能对字符串进行反转
words = base64.b64decode(words)
for ch in words:
result = result + chr(ord(ch) - 1)
return result[::-1] # 使用Python中的字符串切片功能对字符串进行反转
print decode('iEJqak3pjIaZ0NzLiITLwWTqzqGAtW2oyOTq1A3pzqas')
注 :
关于Rot-13 , 这种加密方式其实就是利用凯撒加密的原理 ,
但是为什么得到的密文加密一次后又会变成明文呢 ?
正好因为英文有26个字母 ,
位移13位之后刚好又可以回到原来的位置
答案:
nctf{rot13_and_base64_and_strrev}
知识点:
- Rot-13加密
- 凯撒密码
- 在线Rot-13加密/解密
- Base64
- Python基础(或者任何一门编程语言的基础)