All permutations of a Windows license key

PythonItertools

Python Problem Overview


I need to apply for a Windows 8 upgrade for my laptop, for which I need the Windows 7 license key on the underside of the laptop.

Because Microsoft decided in their infinite wisdom to create license labels that wear off, and I cannot read my license key clearly, it means I can't register my laptop for the windows upgrade offer using an automated process.

By holding the laptop at an angle to the light I have been able to verify most of the code but several of the letters are ambiguous (thanks again Microsoft for using easy to misread characters in your label).

I have the following (obfuscated) license key,

MPP6R-09RXG-2H[8B]MT-[B8]K[HN]M9-V[6G]C8R

where the characters in square brackets are ambiguous, so it is either 8 or B, B or 8, H or N, 6 or G.

Making 16 combinations.

Is it appropriate to generate the possible permutations of this license key using itertools or is there a better way?

I got the correct key with thanks to the contributors. A very convenient way to check if the key is valid is by using the Windows 7 product key checker.

Python Solutions


Solution 1 - Python

Disclaimer: Yes, I know that this is not Python code. It just popped into my mind and I simply had to write it down.

The simplest way is the use of shell expansion:

$ echo MPP6R-09RXG-2H{8,B}MT-{B,8}K{H,N}M9-V{6,G}C8R
MPP6R-09RXG-2H8MT-BKHM9-V6C8R
MPP6R-09RXG-2H8MT-BKHM9-VGC8R
MPP6R-09RXG-2H8MT-BKNM9-V6C8R
MPP6R-09RXG-2H8MT-BKNM9-VGC8R
MPP6R-09RXG-2H8MT-8KHM9-V6C8R
MPP6R-09RXG-2H8MT-8KHM9-VGC8R
MPP6R-09RXG-2H8MT-8KNM9-V6C8R
MPP6R-09RXG-2H8MT-8KNM9-VGC8R
MPP6R-09RXG-2HBMT-BKHM9-V6C8R
MPP6R-09RXG-2HBMT-BKHM9-VGC8R
MPP6R-09RXG-2HBMT-BKNM9-V6C8R
MPP6R-09RXG-2HBMT-BKNM9-VGC8R
MPP6R-09RXG-2HBMT-8KHM9-V6C8R
MPP6R-09RXG-2HBMT-8KHM9-VGC8R
MPP6R-09RXG-2HBMT-8KNM9-V6C8R
MPP6R-09RXG-2HBMT-8KNM9-VGC8R

Solution 2 - Python

from itertools import product
for perm in product('8B', 'B8', 'HN', '6G'):
    print 'MPP6R-09RXG-2H%sMT-%sK%sM9-V%sC8R' % perm

Solution 3 - Python

Another way to generate the combinations

>>> ['MPP6R-09RXG-2H%sMT-%sK%sM9-V%sC8R' % (a, b, c, d)
...  for a in '8B' for b in 'B8' for c in 'HN' for d in '6G']
['MPP6R-09RXG-2H8MT-BKHM9-V6C8R',
 'MPP6R-09RXG-2H8MT-BKHM9-VGC8R',
 'MPP6R-09RXG-2H8MT-BKNM9-V6C8R',
 'MPP6R-09RXG-2H8MT-BKNM9-VGC8R',
 'MPP6R-09RXG-2H8MT-8KHM9-V6C8R',
 'MPP6R-09RXG-2H8MT-8KHM9-VGC8R',
 'MPP6R-09RXG-2H8MT-8KNM9-V6C8R',
 'MPP6R-09RXG-2H8MT-8KNM9-VGC8R',
 'MPP6R-09RXG-2HBMT-BKHM9-V6C8R',
 'MPP6R-09RXG-2HBMT-BKHM9-VGC8R',
 'MPP6R-09RXG-2HBMT-BKNM9-V6C8R',
 'MPP6R-09RXG-2HBMT-BKNM9-VGC8R',
 'MPP6R-09RXG-2HBMT-8KHM9-V6C8R',
 'MPP6R-09RXG-2HBMT-8KHM9-VGC8R',
 'MPP6R-09RXG-2HBMT-8KNM9-V6C8R',
 'MPP6R-09RXG-2HBMT-8KNM9-VGC8R']
>>> 

Solution 4 - Python

How about using itertools and functools at the same time?

>>> from operator import mod
>>> from functools import partial
>>> from itertools import product
>>> map(partial(mod, 'MPP6R-09RXG-2H%sMT-%sK%sM9-V%sC8R'), product('8B', 'B8', 'HN', '6G'))
['MPP6R-09RXG-2H8MT-BKHM9-V6C8R', 'MPP6R-09RXG-2H8MT-BKHM9-VGC8R', 'MPP6R-09RXG-2H8MT-BKNM9-V6C8R', 'MPP6R-09RXG-2H8MT-BKNM9-VGC8R', 'MPP6R-09RXG-2H8MT-8KHM9-V6C8R', 'MPP6R-09RXG-2H8MT-8KHM9-VGC8R', 'MPP6R-09RXG-2H8MT-8KNM9-V6C8R', 'MPP6R-09RXG-2H8MT-8KNM9-VGC8R', 'MPP6R-09RXG-2HBMT-BKHM9-V6C8R', 'MPP6R-09RXG-2HBMT-BKHM9-VGC8R', 'MPP6R-09RXG-2HBMT-BKNM9-V6C8R', 'MPP6R-09RXG-2HBMT-BKNM9-VGC8R', 'MPP6R-09RXG-2HBMT-8KHM9-V6C8R', 'MPP6R-09RXG-2HBMT-8KHM9-VGC8R', 'MPP6R-09RXG-2HBMT-8KNM9-V6C8R', 'MPP6R-09RXG-2HBMT-8KNM9-VGC8R']

Solution 5 - Python

http://www.magicaljellybean.com/keyfinder/

> The Magical Jelly Bean Keyfinder is a freeware utility that retrieves your Product Key (cd key) used to install windows from your registry. It also has a community-updated configuration file that retrieves product keys for many other applications.

Just run it on the install you want the key for.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKerridge0View Question on Stackoverflow
Solution 1 - PythonbikeshedderView Answer on Stackoverflow
Solution 2 - PythonYuval AdamView Answer on Stackoverflow
Solution 3 - PythonNick DandoulakisView Answer on Stackoverflow
Solution 4 - PythonbikeshedderView Answer on Stackoverflow
Solution 5 - PythonPaul CollingwoodView Answer on Stackoverflow