def decode(ciphertext, alphabet, increment, multiplier): modulo = len(alphabet) assert gcd(multiplier, modulo) == 1 # multiplier and length have to be co-primes multiplier_inverse = inverse(multiplier, modulo) # TODO plaintext = substitute(ciphertext, alphabet, lambda x: multiplier_inverse * (x - increment)) return plaintext
def encode(plaintext, alphabet, increment, multiplier,): assert gcd(multiplier, len(alphabet)) == 1 # multiplier and length have to be co-primes ciphertext = substitute(plaintext, alphabet, lambda x: x * multiplier + increment) return ciphertext
#Kabopan - Readable Algorithms. Public Domain, 2007-2009 from kbp._misc import ( byteswap, char_range, countmissing, gcd, getbinlen, getbinstr, gethexstr, gethyphenstr, getlongestcommon, getpadbinstr, getunkbinstr, getvaluefrombinarystring, bin2hex, ASCII, as_words, hex2bin, insert_string, int2bebin, int2lebin, lcm, md5, modifystring, nibbleswap, rol, ror, rorstr, setstr, zip_extend, lcm, slice_and_pad) import struct assert char_range("A", "Z") == "ABCDEFGHIJKLMNOPQRSTUVWXYZ" #gcd #returns the greatest common divisor of both parameters assert gcd(6, 8) == 2 #lcm #returns the least common multiplier of both parameters assert lcm(6, 9) == 18 assert lcm(6, 8) == 24 assert getbinlen(0) == 1 assert getbinlen(1) == 1 assert getbinlen(3) == 2 assert getvaluefrombinarystring("1000") == 8 assert getvaluefrombinarystring("001000") == 8 assert getpadbinstr(8, 8) == "00001000"
def coprimes(x): return [i for i in xrange(x) if (gcd(i, x) == 1)]
from kbp._misc import (byteswap, char_range, countmissing, gcd, getbinlen, getbinstr, gethexstr, gethyphenstr, getlongestcommon, getpadbinstr, getunkbinstr, getvaluefrombinarystring, bin2hex, ASCII, as_words, hex2bin, insert_string, int2bebin, int2lebin, lcm, md5, modifystring, nibbleswap, rol, ror, rorstr, setstr, zip_extend, lcm, slice_and_pad) import struct assert char_range("A", "Z") == "ABCDEFGHIJKLMNOPQRSTUVWXYZ" #gcd #returns the greatest common divisor of both parameters assert gcd(6, 8) == 2 #lcm #returns the least common multiplier of both parameters assert lcm(6, 9) == 18 assert lcm(6, 8) == 24 assert getbinlen(0) == 1 assert getbinlen(1) == 1 assert getbinlen(3) == 2 assert getvaluefrombinarystring("1000") == 8 assert getvaluefrombinarystring("001000") == 8 assert getpadbinstr(8, 8) == "00001000"