def _attack_str(ct, lf): import frequency lab = frequency.alphabet(lf) tf = frequency.probability(ct) maxs = 0 candk = 0 for k in range(len(lf)): s = _sum_f(tf, lf, lab, k) if s > maxs: maxs = s candk = k return candk
def _attack_kn_str(ct, lf, maxkn=None, multh=0.01, minioc=0.01, mulstep=False, maxo=5): import frequency lab = frequency.alphabet(lf) if maxkn is None or maxkn > len(ct): maxkn = len(ct) candkn = 1 maxioc = 0 for kn in range(2, maxkn + 1): cioc = _ioc_str_kn(ct, lab, kn, maxo) if cioc > maxioc and cioc >= minioc: if kn % candkn != 0 or cioc - maxioc > multh: maxioc = cioc candkn = kn elif mulstep: maxioc = cioc return candkn
def decode(pt, k, ab): import frequency return _shift_str(pt, -k, frequency.alphabet(ab))
def decode(ct, k, ab): import frequency return _shift_str(ct, k, frequency.alphabet(ab), True)