PoshCode Archive  Artifact [b730b2946b]

Artifact b730b2946b5a65eb18331c83f738b5a2ae8bc92160ca725bcff6171e1768da19:

  • File Product-key.ps1 — part of check-in [e39607e156] at 2018-06-10 13:30:04 on branch trunk — IronPython script for retrieving MS products. There is how to get Windows key in following example. (user: Mark NElson size: 1239)

# encoding: ascii
# api: powershell
# title: Product key
# description: IronPython script for retrieving MS products. There is how to get Windows key in following example.
# version: 0.1
# author: Mark NElson
# license: CC0
# x-poshcode-id: 3731
# x-archived: 2012-11-04T00:12:05
# x-published: 2012-10-31T06:30:00
#
#
from System import Array, Char, Console
from System.Collections import ArrayList
from Microsoft.Win32 import Registry

def DecodeProductKey(digitalProductID):
   map = ("BCDFGHJKMPQRTVWXY2346789").ToCharArray()
   key = Array.CreateInstance(Char, 29)
   raw = ArrayList()

   i = 52
   while i < 67:
      raw.Add(digitalProductID[i])
      i += 1

   i = 28
   while i >= 0:
      if (i + 1) % 6 == 0:
         key[i] = '-'
      else:
         k = 0
         j = 14
         while j >= 0:
            k = (k * 256) ^ raw[j]
            raw[j] = (k / 24)
            k %= 24
            key[i] = map[k]
            j -= 1
      i -= 1

   return key

reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion")
val = reg.GetValue("DigitalProductId")
Console.WriteLine(DecodeProductKey(val))

Hmm, Mark stole my code at http://poshcode.org/3545