Suppose is a sequence of capital letters and spaces, and that does not begin with a space. We encode as a number in base as follows: a single space corresponds to 0 , the letter to , to , , to . Thus ``RUN NIKITA'' is a number written in base :
RUN NIKITA | ||
(in decimal) |
If , then any sequence of letters can be encoded as above using a positive integer . Thus if we can encrypt integers of size at most , then we must break our message up into blocks of size at most .
sage: def encode(s): ... s = str(s) # make input a string ... return sum(ord(s[i])*256^i for i in range(len(s))) sage: def decode(n): ... n = Integer(n) # make input an integer ... v = [] ... while n != 0: ... v.append(chr(n % 256)) ... n //= 256 # this replaces n by floor(n/256). ... return ''.join(v) sage: m = encode('Run Nikita!'); m 40354769014714649421968722 sage: decode(m) 'Run Nikita!'
William 2007-06-01