0
0

More than 1 year has passed since last update.

AOJトライに関する知識知見の記録共有 (Volume0-0040)

Posted at

概要

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']]]]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0