Permalink
Browse files

decode and encode messages into numbers

  • Loading branch information...
0 parents commit a46e025d8588aa8a447ef4823d31550fb03398e8 @benthaman committed May 26, 2011
Showing with 140 additions and 0 deletions.
  1. +49 −0 decode.py
  2. +72 −0 encode.py
  3. +19 −0 original.py
@@ -0,0 +1,49 @@
+#!/usr/bin/python
+
+import sys
+from math import *
+
+def pfactor(x):
+ factorlist = []
+
+ if x == 1:
+ factorlist.append(x)
+
+ while x % 2 == 0:
+ factorlist.append(2)
+ x /= 2
+
+ limit = 1 + int(sqrt(x))
+ for factor in range(3, limit, 2):
+ while x % factor == 0:
+ factorlist.append(factor)
+ x /= factor
+
+ if x > 1:
+ factorlist.append(x)
+
+ return factorlist
+
+if len(sys.argv) != 1 + 2:
+ charset = ' Wadefhlnost'
+ # all 23 prime factors are of order 1
+ s = 13682311570832829480888979137834570837851469148689544502986
+else:
+ charset = sys.argv[1]
+ s = int(sys.argv[2])
+modulo = len(charset)
+while len(pfactor(modulo)) != 1:
+ modulo += 1
+
+num = iter(range(2, 9999))
+while s != 1 :
+ n = num.next()
+ if s % n == 0 :
+ sys.stdout.write("%s" % charset[(n - 1) % modulo])
+ print " n= %d, %d * %d + %d, pfactor(n - 1) = (%s)" % (
+ n,
+ (n - 1) / modulo,
+ modulo, (n - 1) % modulo,
+ str(pfactor(n - 1)),)
+ s /= n
+sys.stdout.write("\n")
@@ -0,0 +1,72 @@
+#!/usr/bin/python
+
+import sys
+import random
+import itertools
+from math import sqrt
+
+class Pfactor(object):
+ def __init__(self):
+ # bah, not implemented
+ self.cache = []
+
+ def __call__(self, x):
+ factorlist = []
+
+ if x == 1:
+ factorlist.append(x)
+ while x % 2 == 0:
+ factorlist.append(2)
+ x /= 2
+ limit = 1 + int(sqrt(x))
+ for factor in range(3, limit, 2):
+ while x % factor == 0:
+ factorlist.append(factor)
+ x /= factor
+ if x > 1:
+ factorlist.append(x)
+
+ return factorlist
+pfactor = Pfactor()
+
+if len(sys.argv) != 1 + 1:
+ msg = "What a load of nonsense"
+else:
+ msg = sys.argv[1]
+charset_letters = sorted(list(set(msg)))
+# optional, output different results
+random.shuffle(charset_letters)
+modulo = len(charset_letters)
+while len(pfactor(modulo)) != 1:
+ modulo += 1
+
+limit = 9999
+for charset in itertools.permutations(charset_letters):
+ factor = []
+ n = 0
+ for letter in msg:
+ index = charset.index(letter)
+ candidate = n * modulo + index + 1
+ # find the smallest n such that:
+ # factor[i] = n * modulo + index + 1 is prime and
+ # factor[i] > factor[i - 1]
+ while candidate < limit and not (
+ len(pfactor(candidate)) == 1 and (len(factor) == 0 or
+ candidate > factor[-1])):
+ n += 1
+ candidate = n * modulo + index + 1
+
+ if candidate >= limit:
+ continue
+ else:
+ print "factor= %d, %d * %d + %d + 1" % (candidate, n, modulo, index,)
+ factor.append(candidate)
+
+ if candidate >= limit:
+ print "next try"
+ continue
+ else:
+ print modulo
+ print "".join(charset)
+ print reduce(lambda o1, o2: o1 * o2, factor)
+ sys.exit()
@@ -0,0 +1,19 @@
+#!/usr/bin/python
+
+# http://lwn.net/Articles/443683/
+
+import sys
+
+charset = ' Wadefhlnost'
+modulo = 13
+s = 13682311570832829480888979137834570837851469148689544502986
+num = iter(range(2, 9999))
+while s != 1 :
+ n = num.next()
+ if s % n == 0 :
+ sys.stdout.write("%s" % charset[(n - 1) % modulo])
+ s /= n
+ #end if
+#end while
+sys.stdout.write("\n")
+

0 comments on commit a46e025

Please sign in to comment.