概要
Affine Cipher
コード
空白文字に定数-1を割り付け
def char2int(data, inv=False):
c_seq = "abcdefghijklmnopqrstuvwxyz "
if inv:
ret = c_seq[data]
else:
ret = -1
for ret, c in enumerate(c_seq):
if data == c:
break
return ret
def f(c, a, b):
if c == " ":
ret = -1
else:
ret = (a * char2int(c) + b) % 26
return ret
def core(enc_str):
ret = []
for a_inv in range(1, 26):
for b in range(26):
dec_str = "".join([char2int(f(c, a_inv, b), inv=True) for c in enc_str])
print(dec_str)
if " that " in dec_str or " this " in dec_str:
ret.append([[a_inv, b], dec_str])
return ret
def app(*args):
return [[arg, core(arg)] for arg in args]
print(app(
"y eazqyp pnop pngtg ye obmpngt xmybp mr lygw",
))
実行結果
デバッグログ出力部含む
[['y eazqyp pnop pngtg ye obmpngt xmybp mr lygw', [[[19, 20], 'i submit that there is another point of view']]]]