8

C# で Windows XP のプロダクト キーを取得するにはどうすればよいですか?

クライアント側からWindowsキーのようなキーを取得したい。

4

5 に答える 5

9

Windows プロダクト キー ファインダーと、Erij J. および他のユーザーがここで言及しているその他のソリューションは、Windows XP および Windows 7 でのみ機能します。Microsoft は、Windows 8 以降、キー暗号化アルゴリズムを変更しました。

ここのブログ投稿で Windows 8 以降のソリューションを見つけました: http://winaero.com/blog/how-to-view-your-product-key-in-windows-10-windows-8-and-windows -7/

ただし、VBS で書かれているので、C# に書き直しました。

完全なプロジェクトは GitHub で確認できます: https://github.com/mrpeardotnet/WinProdKeyFinder

Windows 8以降でプロダクトキーをデコードするコードは次のとおりです。

    public static string DecodeProductKeyWin8AndUp(byte[] digitalProductId)
    {
        var key = String.Empty;
        const int keyOffset = 52;
        var isWin8 = (byte)((digitalProductId[66] / 6) & 1);
        digitalProductId[66] = (byte)((digitalProductId[66] & 0xf7) | (isWin8 & 2) * 4);

        // Possible alpha-numeric characters in product key.
        const string digits = "BCDFGHJKMPQRTVWXY2346789";
        int last = 0;
        for (var i = 24; i >= 0; i--)
        {
            var current = 0;
            for (var j = 14; j >= 0; j--)
            {
                current = current*256;
                current = digitalProductId[j + keyOffset] + current;
                digitalProductId[j + keyOffset] = (byte)(current/24);
                current = current%24;
                last = current;
            }
            key = digits[current] + key;
        }
        var keypart1 = key.Substring(1, last);
        const string insert = "N";
        key = key.Substring(1).Replace(keypart1, keypart1 + insert);
        if (last == 0)
            key = insert + key;
        for (var i = 5; i < key.Length; i += 6)
        {
            key = key.Insert(i, "-");
        }
        return key;
    }

Windows のバージョンを確認して digitalProductId を取得するには、次のようなラッパー メソッドを使用します。

    public static string GetWindowsProductKey()
    {
            var key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
                                          RegistryView.Default);
            const string keyPath = @"Software\Microsoft\Windows NT\CurrentVersion";
            var digitalProductId = (byte[])key.OpenSubKey(keyPath).GetValue("DigitalProductId");

        var isWin8OrUp =
            (Environment.OSVersion.Version.Major == 6 && System.Environment.OSVersion.Version.Minor >= 2)
            ||
            (Environment.OSVersion.Version.Major > 6);

        var productKey = isWin8OrUp ? DecodeProductKeyWin8AndUp(digitalProductId) : DecodeProductKey(digitalProductId);
        return productKey;
    }

複数のマシンでテストしたところ、正しい結果が得られました。Windows 10 でも、一般的な Windows 10 コードを取得できました (アップグレードされたシステムで)。

注: 私にとっては、元のブログ投稿でコードが Win7 以降で動作すると記載されていたにもかかわらず、元の vbs スクリプトが間違った Win7 キーを返していました。そのため、私は常に Win7 以前のよく知られた古い方法にフォールバックします。

それが役に立てば幸い。

于 2015-08-21T11:52:37.577 に答える
3

Windowsのプロダクトキーファインダーを確認してください。

http://wpkf.codeplex.com/

ソースが利用可能です。

キーはレジストリに保存されており、ソースで利用可能なアルゴリズムを使用してデコードする必要があります。

于 2012-06-07T06:25:33.173 に答える
3

WMI には、SerialNumberWin32_OperatingSystemというプロパティを含む というクラスがあります。

オペレーティング システムの製品シリアル識別番号。

例: 「10497-OEM-0031416-71674」

名前System.Management空間には、.NET コードが WMI とやり取りできるようにするクラスが含まれています。

于 2012-06-07T06:30:19.023 に答える
1

mrpeardotnetの答えに行きます...

RegistryView.Default文字列に変換できないエラーが発生していました。私はこれを変更しました:

var key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default);

これに:

var key = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, Environment.MachineName);

そしてそれはうまくいくようです。

于 2016-06-30T23:39:57.373 に答える
0

このコードを使用すると、Microsoft 製品のプロダクト キーを見つけることができます。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
namespace MSKeyFinder
{
    public class KeyDecoder
    {
        public enum Key { XP, Office10, Office11 };
        public static byte[] GetRegistryDigitalProductId(Key key)
        {
            byte[] digitalProductId = null;
            RegistryKey registry = null;
            switch (key)
            {
                // Open the XP subkey readonly.
                case Key.XP:
                    registry =
                      Registry.LocalMachine.
                        OpenSubKey(
                          @"SOFTWARE\Microsoft\Windows NT\CurrentVersion",
                            false);
                    break;
                // Open the Office 10 subkey readonly.
                case Key.Office10:
                    registry =
                      Registry.LocalMachine.
                        OpenSubKey(
                          @"SOFTWARE\Microsoft\Office\10.0\Registration\" + 
                          @"{90280409-6000-11D3-8CFE-0050048383C9}",
                          false);
                    // TODO: Open the registry key.
                    break;
                // Open the Office 11 subkey readonly.
                case Key.Office11:
                    // TODO: Open the registry key.
                    break;
            }
            if (registry != null)
            {
                // TODO: For other products, key name maybe different.
                digitalProductId = registry.GetValue("DigitalProductId")
                  as byte[];
                registry.Close();
            }
            return digitalProductId;
        }
        public static string DecodeProductKey(byte[] digitalProductId)
        {
            // Offset of first byte of encoded product key in 
            //  'DigitalProductIdxxx" REG_BINARY value. Offset = 34H.
            const int keyStartIndex = 52;
            // Offset of last byte of encoded product key in 
            //  'DigitalProductIdxxx" REG_BINARY value. Offset = 43H.
            const int keyEndIndex = keyStartIndex + 15;
            // Possible alpha-numeric characters in product key.
            char[] digits = new char[]
      {
        'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'M', 'P', 'Q', 'R', 
        'T', 'V', 'W', 'X', 'Y', '2', '3', '4', '6', '7', '8', '9',
      };
            // Length of decoded product key
            const int decodeLength = 29;
            // Length of decoded product key in byte-form.
            // Each byte represents 2 chars.
            const int decodeStringLength = 15;
            // Array of containing the decoded product key.
            char[] decodedChars = new char[decodeLength];
            // Extract byte 52 to 67 inclusive.
            ArrayList hexPid = new ArrayList();
            for (int i = keyStartIndex; i <= keyEndIndex; i++)
            {
                hexPid.Add(digitalProductId[i]);
            }
            for (int i = decodeLength - 1; i >= 0; i--)
            {
                // Every sixth char is a separator.
                if ((i + 1) % 6 == 0)
                {
                    decodedChars[i] = '-';
                }
                else
                {
                    // Do the actual decoding.
                    int digitMapIndex = 0;
                    for (int j = decodeStringLength - 1; j >= 0; j--)
                    {
                        int byteValue = (digitMapIndex << 8) | (byte)hexPid[j];
                        hexPid[j] = (byte)(byteValue / 24);
                        digitMapIndex = byteValue % 24;
                        decodedChars[i] = digits[digitMapIndex];
                    }
                }
            }
            return new string(decodedChars);
        }
    }
}

参照元: http://www.codeproject.com/Articles/23334/Microsoft-Product-Key-Finder

于 2012-06-07T06:27:07.987 に答える