97

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kerridge0
  • 2,017
  • 19
  • 22
  • 11
    I think you could list out the sixteen combinations by hand, which may in the end take less time. – Waleed Khan Jan 30 '13 at 14:28
  • 17
    Such a cool question. +1 – Flavius Jan 30 '13 at 14:30
  • 13
    I really expect that you changed some of the recognized characters and you are not posting your real Windows key on the Internet... – rodrigo Jan 30 '13 at 14:39
  • @WaleedKhan I'm even more worried that he might have a lot more mutations and some poor soul now must test all those combinations. – bikeshedder Jan 30 '13 at 14:51
  • @bikeshedder - that's what I was thinking....! Also one more thanks to Microsoft you can only try 3 times on the web site before getting unceremoniously kicked off, but luckily there's a piece of software out there that lets you verify separately. – Kerridge0 Jan 30 '13 at 14:55
  • 1
    Heh, would've been a very interesting question to post to http://codegolf.stackexchange.com – Earlz Jan 30 '13 at 21:10
  • 4
    @rodrigo Yes I obfuscated the code :) – Kerridge0 Jan 31 '13 at 15:29

5 Answers5

167

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bikeshedder
  • 7,337
  • 1
  • 23
  • 29
  • 9
    I know that this is no python code. It just popped into my mind and I liked the idea of it. No need to downvote this answer. :-( – bikeshedder Jan 30 '13 at 14:39
  • Not python, but a beautiful solution nonetheless. – Yuval Adam Jan 30 '13 at 15:19
  • 2
    +1... and Windows (as far as I know) doesn't has this "expansion"... but you can always use Cygwin – Barranka Jan 30 '13 at 18:45
  • 55
    Well Windows may not have it, but if you ever need to iterate your Linux license key.. oh wait, that's another reason I love linux – Drake Clarris Jan 30 '13 at 20:20
  • 1
    Not only is it short and sweet, but it gave the answers in a very clean way for copy and paste. There was actually a 5th variable, and I got my key!! I actually gave the "write it down" task to a colleague, and he gave up! Thanks a lot. – Kerridge0 Jan 31 '13 at 15:50
  • Doesn't seem to work in MKS or KSH shell under couple of Unix (AIX+Solaris) that I tried :( – Alex Feb 05 '13 at 22:24
  • @Alex I just tried csh (20110502), ksh (93u+) and mksh (40.9.20120630) and it works for me. Do not put the pattern into quotes otherwise the pattern will not be expanded. – bikeshedder Feb 06 '13 at 12:44
  • @bikeshedder, I must have much older versions of shells on unix (we don't have linux). MKSH is different from MKS. It did work for me in cygwin, so I think I copied echo command correctly. I certainly +1'd your answer as it's very creative and works for vast community it seems. – Alex Feb 06 '13 at 15:12
  • the code can also be tested here : http://ideone.com/ (but the output is a bit buggy) – bendaizer Feb 06 '13 at 15:22
60
from itertools import product
for perm in product('8B', 'B8', 'HN', '6G'):
    print 'MPP6R-09RXG-2H%sMT-%sK%sM9-V%sC8R' % perm
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
17

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']
>>> 
Nick Dandoulakis
  • 42,588
  • 16
  • 104
  • 136
10

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']
bikeshedder
  • 7,337
  • 1
  • 23
  • 29
  • 1
    Nice! It never ocurred to me that you could use operator.mod to do string formatting too. – Tom Feb 04 '13 at 22:25
  • @MartijnPieters `partial` is not required when using `format` as it changes nothing when called without `*args` or `**kwargs`. When using `format` you can not pass the tuple directly but need to convert it to arguments first: `map(lambda args: 'MPP6R-09RXG-2H{}MT-{}K{}M9-V{}C8R'.format(*args), product('8B', 'B8', 'HN', '6G'))`. I still use the old `%` string formatting a lot for sentimental reasons. I really should consider using the `format` function more often. – bikeshedder Sep 23 '14 at 20:24
  • Yeah, somehow I pictured the arguments being applied as `*args`. My mistake. – Martijn Pieters Sep 23 '14 at 20:57
4

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.

Paul Collingwood
  • 9,053
  • 3
  • 23
  • 36
  • 2
    Unfortunately, the Windows installed on the laptop is not using the license key to be found on the underside of the laptop, but is installed using some kind of corporate volume key. Microsoft does not allow these codes to be used to validate the windows upgrade. – Kerridge0 Jan 30 '13 at 14:36