1
 def bruteForce( dictionary = {}):
    key = 0

    for i in range(len(dictionary)):
        keyRank = 0
        for k in range(68719476736):
            attempt = decrypt(dictionary[i], k)
            if(i != attempt):
                keyRank = 0
                break
            else:
                keyRank += 1
                key = k
                print 'key attempt: {0:b}'.format(key)
        if(keyRank == len(dictionary)):
            print 'found key: {0:b}'.format(key)
            break
  1. The key is 36 bits
  2. I get a memory error on the for k in range() line of code
  3. Why is this a memory issue? Does python build an actual list of ints before running this line? Is there a better way to write this loop?
  4. I'm brand new to Python and this wouldn't be a problem in C or Java.
  5. This is a known-plaintext/ciphertext attack. dictionary is a mapping of P:C pairs.
  6. Its on a VM, I can up the memory if needed, but want to know both why its failing and a code-based workaround or better idiomatic approach.
avgvstvs
  • 6,196
  • 6
  • 43
  • 74

1 Answers1

3

In python 2, range() will build the entire list in memory.

xrange() is a sequence object that evaluates lazily.

In python 3, range() does what xrange() did.

Joshua Grigonis
  • 748
  • 5
  • 18
  • Is there any history as to why `range()` worked that way in Python2? – avgvstvs May 28 '15 at 17:26
  • It has utility that way, for instance, you can use slice on the return of range(), but eventually I guess, people realized that faster and simpler was better than having that extra utility. – Joshua Grigonis May 28 '15 at 17:30