├── LicenseEngine
├── KeyGenerateUI
│ ├── icon.ico
│ ├── img
│ │ ├── icon.ico
│ │ └── icon.png
│ ├── App.config
│ ├── App.xaml.cs
│ ├── Properties
│ │ ├── Settings.settings
│ │ ├── Settings.Designer.cs
│ │ ├── AssemblyInfo.cs
│ │ ├── Resources.Designer.cs
│ │ └── Resources.resx
│ ├── packages.config
│ ├── App.xaml
│ ├── MainWindow.xaml.cs
│ ├── MainWindow.xaml
│ └── KeyGenerateUI.csproj
├── KeyVerify
│ ├── KeyResult.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── KeyVerify.csproj
│ ├── ActivationKeyFileCheck.cs
│ ├── ActivationKeyDecryption.cs
│ └── KeyCheck.cs
├── KeyTest
│ ├── packages.config
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── KeyTest.csproj
│ └── KeyTests.cs
├── KeyGenerate
│ ├── KeyByteSetComparer.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── KeyGenerate.csproj
│ └── KeyGenerator.cs
├── KeyCommon
│ ├── KeyByteSet.cs
│ ├── Exceptions.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── KeyCommon.csproj
└── LicenseEngine.sln
├── README.md
├── LICENSE.md
├── .gitattributes
└── .gitignore
/LicenseEngine/KeyGenerateUI/icon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jcf-dev/LicenseEngine/HEAD/LicenseEngine/KeyGenerateUI/icon.ico
--------------------------------------------------------------------------------
/LicenseEngine/KeyGenerateUI/img/icon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jcf-dev/LicenseEngine/HEAD/LicenseEngine/KeyGenerateUI/img/icon.ico
--------------------------------------------------------------------------------
/LicenseEngine/KeyGenerateUI/img/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jcf-dev/LicenseEngine/HEAD/LicenseEngine/KeyGenerateUI/img/icon.png
--------------------------------------------------------------------------------
/LicenseEngine/KeyGenerateUI/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/LicenseEngine/KeyVerify/KeyResult.cs:
--------------------------------------------------------------------------------
1 | namespace KeyVerify
2 | {
3 | public enum LicenseKeyResult
4 | {
5 | KeyGood = 0,
6 | KeyInvalid = 1,
7 | KeyBlackListed = 2,
8 | KeyPhoney = 3
9 | }
10 | }
--------------------------------------------------------------------------------
/LicenseEngine/KeyGenerateUI/App.xaml.cs:
--------------------------------------------------------------------------------
1 | using System.Windows;
2 |
3 | namespace KeyGenerateUI
4 | {
5 | ///
6 | /// Interaction logic for App.xaml
7 | ///
8 | public partial class App : Application
9 | {
10 | }
11 | }
--------------------------------------------------------------------------------
/LicenseEngine/KeyGenerateUI/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LicenseEngine/KeyTest/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/LicenseEngine/KeyGenerateUI/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/LicenseEngine/KeyGenerate/KeyByteSetComparer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections;
2 | using KeyCommon;
3 |
4 | namespace KeyGenerate
5 | {
6 | public class KeyByteSetComparer : IComparer
7 | {
8 | public int Compare(object x, object y)
9 | {
10 | var kbs1 = (KeyByteSet) x;
11 | var kbs2 = (KeyByteSet) y;
12 |
13 | if (kbs1.KeyByteNo > kbs2.KeyByteNo) return 1;
14 |
15 | if (kbs1.KeyByteNo < kbs2.KeyByteNo) return -1;
16 |
17 | return 0;
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/LicenseEngine/KeyCommon/KeyByteSet.cs:
--------------------------------------------------------------------------------
1 | namespace KeyCommon
2 | {
3 | public class KeyByteSet
4 | {
5 | public KeyByteSet(int keyByteNo, byte keyByteA, byte keyByteB, byte keyByteC)
6 | {
7 | KeyByteNo = keyByteNo;
8 | KeyByteA = keyByteA;
9 | KeyByteB = keyByteB;
10 | KeyByteC = keyByteC;
11 | }
12 |
13 | public int KeyByteNo { get; }
14 | public byte KeyByteA { get; }
15 | public byte KeyByteB { get; }
16 | public byte KeyByteC { get; }
17 | }
18 | }
--------------------------------------------------------------------------------
/LicenseEngine/KeyTest/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.InteropServices;
3 |
4 | [assembly: AssemblyTitle("KeyTest")]
5 | [assembly: AssemblyDescription("")]
6 | [assembly: AssemblyConfiguration("")]
7 | [assembly: AssemblyCompany("")]
8 | [assembly: AssemblyProduct("KeyTest")]
9 | [assembly: AssemblyCopyright("Copyright © 2020")]
10 | [assembly: AssemblyTrademark("")]
11 | [assembly: AssemblyCulture("")]
12 |
13 | [assembly: ComVisible(false)]
14 |
15 | [assembly: Guid("1d7bdb7b-c752-4581-a4d7-5e53995d620f")]
16 |
17 | // [assembly: AssemblyVersion("1.0.*")]
18 | [assembly: AssemblyVersion("1.0.0.0")]
19 | [assembly: AssemblyFileVersion("1.0.0.0")]
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # License Engine UI
2 |
3 | License Engine UI is a simple GUI version of the [AppSoftware's .NET License Generator](https://github.com/appsoftwareltd/dotnet-licence-key-generator) optimized for .NET 4.5 and above.
4 |
5 | ## Usage
6 | Download the [binaries here](https://github.com/joweenflores/LicenseEngine/releases/tag/1.0.0).
7 |
8 | Add **KeyCommon.dll** and **KeyVerify.dll** to your project references or use **KeyGenerateUI** to generate license keys.
9 |
10 | ## Contributing
11 | Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
12 |
13 | Please make sure to update the tests as appropriate.
14 |
15 | ## Credits
16 | [AppSoftware](https://github.com/appsoftwareltd/dotnet-licence-key-generator) for the original License Generator.
17 |
18 | [MahApps.Metro](https://github.com/MahApps/MahApps.Metro) for the UI Look and Feel.
19 |
20 | ## License
21 | [MIT](https://choosealicense.com/licenses/mit/)
22 |
--------------------------------------------------------------------------------
/LicenseEngine/KeyCommon/Exceptions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace KeyCommon
4 | {
5 | ///
6 | /// Exception indicating that the activation key file was not found in the correct location.
7 | ///
8 | public class ActivationKeyFileNotPresentException : Exception
9 | {
10 | public ActivationKeyFileNotPresentException(string message) : base(message)
11 | {
12 | }
13 | }
14 |
15 | ///
16 | /// Exception indicating that the trial period for this application has expired.
17 | ///
18 | public class TrialPeriodExpiredException : Exception
19 | {
20 | public TrialPeriodExpiredException(string message) : base(message)
21 | {
22 | }
23 | }
24 |
25 | ///
26 | /// Exception indicating that value in activation key file was invalid.
27 | ///
28 | public class ActivationKeyInvalidException : Exception
29 | {
30 | public ActivationKeyInvalidException(string message) : base(message)
31 | {
32 | }
33 | }
34 | }
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Joween Flores
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/LicenseEngine/KeyGenerateUI/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.42000
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace KeyGenerateUI.Properties
12 | {
13 |
14 |
15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
18 | {
19 |
20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
21 |
22 | public static Settings Default
23 | {
24 | get
25 | {
26 | return defaultInstance;
27 | }
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/LicenseEngine/KeyGenerateUI/App.xaml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/LicenseEngine/KeyCommon/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.InteropServices;
3 |
4 | // General Information about an assembly is controlled through the following
5 | // set of attributes. Change these attribute values to modify the information
6 | // associated with an assembly.
7 | [assembly: AssemblyTitle("KeyCommon")]
8 | [assembly: AssemblyDescription("")]
9 | [assembly: AssemblyConfiguration("")]
10 | [assembly: AssemblyCompany("")]
11 | [assembly: AssemblyProduct("KeyCommon")]
12 | [assembly: AssemblyCopyright("Copyright © 2020")]
13 | [assembly: AssemblyTrademark("")]
14 | [assembly: AssemblyCulture("")]
15 |
16 | // Setting ComVisible to false makes the types in this assembly not visible
17 | // to COM components. If you need to access a type in this assembly from
18 | // COM, set the ComVisible attribute to true on that type.
19 | [assembly: ComVisible(false)]
20 |
21 | // The following GUID is for the ID of the typelib if this project is exposed to COM
22 | [assembly: Guid("2024f82c-3306-41b5-951c-0b08f7c65590")]
23 |
24 | // Version information for an assembly consists of the following four values:
25 | //
26 | // Major Version
27 | // Minor Version
28 | // Build Number
29 | // Revision
30 | //
31 | // You can specify all the values or you can default the Build and Revision Numbers
32 | // by using the '*' as shown below:
33 | // [assembly: AssemblyVersion("1.0.*")]
34 | [assembly: AssemblyVersion("1.0.0.0")]
35 | [assembly: AssemblyFileVersion("1.0.0.0")]
--------------------------------------------------------------------------------
/LicenseEngine/KeyVerify/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.InteropServices;
3 |
4 | // General Information about an assembly is controlled through the following
5 | // set of attributes. Change these attribute values to modify the information
6 | // associated with an assembly.
7 | [assembly: AssemblyTitle("KeyVerify")]
8 | [assembly: AssemblyDescription("")]
9 | [assembly: AssemblyConfiguration("")]
10 | [assembly: AssemblyCompany("")]
11 | [assembly: AssemblyProduct("KeyVerify")]
12 | [assembly: AssemblyCopyright("Copyright © 2020")]
13 | [assembly: AssemblyTrademark("")]
14 | [assembly: AssemblyCulture("")]
15 |
16 | // Setting ComVisible to false makes the types in this assembly not visible
17 | // to COM components. If you need to access a type in this assembly from
18 | // COM, set the ComVisible attribute to true on that type.
19 | [assembly: ComVisible(false)]
20 |
21 | // The following GUID is for the ID of the typelib if this project is exposed to COM
22 | [assembly: Guid("808a2a81-2113-4eaa-b595-9c2cd3d61595")]
23 |
24 | // Version information for an assembly consists of the following four values:
25 | //
26 | // Major Version
27 | // Minor Version
28 | // Build Number
29 | // Revision
30 | //
31 | // You can specify all the values or you can default the Build and Revision Numbers
32 | // by using the '*' as shown below:
33 | // [assembly: AssemblyVersion("1.0.*")]
34 | [assembly: AssemblyVersion("1.0.0.0")]
35 | [assembly: AssemblyFileVersion("1.0.0.0")]
--------------------------------------------------------------------------------
/LicenseEngine/KeyGenerate/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.InteropServices;
3 |
4 | // General Information about an assembly is controlled through the following
5 | // set of attributes. Change these attribute values to modify the information
6 | // associated with an assembly.
7 | [assembly: AssemblyTitle("KeyGenerate")]
8 | [assembly: AssemblyDescription("")]
9 | [assembly: AssemblyConfiguration("")]
10 | [assembly: AssemblyCompany("")]
11 | [assembly: AssemblyProduct("KeyGenerate")]
12 | [assembly: AssemblyCopyright("Copyright © 2020")]
13 | [assembly: AssemblyTrademark("")]
14 | [assembly: AssemblyCulture("")]
15 |
16 | // Setting ComVisible to false makes the types in this assembly not visible
17 | // to COM components. If you need to access a type in this assembly from
18 | // COM, set the ComVisible attribute to true on that type.
19 | [assembly: ComVisible(false)]
20 |
21 | // The following GUID is for the ID of the typelib if this project is exposed to COM
22 | [assembly: Guid("df29a928-ab54-4752-9ea6-a05cc8fa6f3c")]
23 |
24 | // Version information for an assembly consists of the following four values:
25 | //
26 | // Major Version
27 | // Minor Version
28 | // Build Number
29 | // Revision
30 | //
31 | // You can specify all the values or you can default the Build and Revision Numbers
32 | // by using the '*' as shown below:
33 | // [assembly: AssemblyVersion("1.0.*")]
34 | [assembly: AssemblyVersion("1.0.0.0")]
35 | [assembly: AssemblyFileVersion("1.0.0.0")]
--------------------------------------------------------------------------------
/LicenseEngine/KeyGenerateUI/MainWindow.xaml.cs:
--------------------------------------------------------------------------------
1 | using System.Diagnostics;
2 | using System.Windows;
3 | using KeyCommon;
4 | using KeyGenerate;
5 | using MahApps.Metro.Controls;
6 |
7 | namespace KeyGenerateUI
8 | {
9 | ///
10 | /// Interaction logic for MainWindow.xaml
11 | ///
12 | public partial class MainWindow : MetroWindow
13 | {
14 | public MainWindow()
15 | {
16 | InitializeComponent();
17 | }
18 |
19 | private void BtnGenerate_Click(object sender, RoutedEventArgs e)
20 | {
21 | var prodID = TxtProdID.Text;
22 | TxtLicense.Text = prodID != ""
23 | ? int.TryParse(prodID, out var ID) ? GenerateKey(ID) : "ERROR: Product ID must be an integer."
24 | : "ERROR: Please provide a Product ID Integer.";
25 | }
26 |
27 | private string GenerateKey(int ID)
28 | {
29 | var keyGenerator = new KeyGenerator();
30 |
31 | var keyBytes = new[]
32 | {
33 | new KeyByteSet(1, 254, 122, 96),
34 | new KeyByteSet(2, 54, 124, 222),
35 | new KeyByteSet(3, 119, 142, 132),
36 | new KeyByteSet(4, 128, 122, 10),
37 | new KeyByteSet(5, 165, 15, 132),
38 | new KeyByteSet(6, 128, 175, 213),
39 | new KeyByteSet(7, 7, 244, 132),
40 | new KeyByteSet(8, 128, 122, 251)
41 | };
42 |
43 | var key = keyGenerator.MakeKey(ID, keyBytes);
44 | return key;
45 | }
46 |
47 | private void BtnReset_Click(object sender, RoutedEventArgs e)
48 | {
49 | TxtLicense.Text = "";
50 | TxtProdID.Text = "";
51 | }
52 |
53 | private void BtnHelp_OnClick(object sender, RoutedEventArgs e)
54 | {
55 | Process.Start("https://github.com/joweenflores/LicenseEngine");
56 | }
57 | }
58 | }
--------------------------------------------------------------------------------
/LicenseEngine/KeyGenerateUI/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.InteropServices;
3 | using System.Windows;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("KeyGenerateUI")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("KeyGenerateUI")]
13 | [assembly: AssemblyCopyright("Copyright © 2020")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | //In order to begin building localizable applications, set
23 | //CultureYouAreCodingWith in your .csproj file
24 | //inside a . For example, if you are using US english
25 | //in your source files, set the to en-US. Then uncomment
26 | //the NeutralResourceLanguage attribute below. Update the "en-US" in
27 | //the line below to match the UICulture setting in the project file.
28 |
29 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
30 |
31 |
32 | [assembly: ThemeInfo(
33 | ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
34 | //(used if a resource is not found in the page,
35 | // or application resource dictionaries)
36 | ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
37 | //(used if a resource is not found in the page,
38 | // app, or any theme specific resource dictionaries)
39 | )]
40 |
41 |
42 | // Version information for an assembly consists of the following four values:
43 | //
44 | // Major Version
45 | // Minor Version
46 | // Build Number
47 | // Revision
48 | //
49 | // You can specify all the values or you can default the Build and Revision Numbers
50 | // by using the '*' as shown below:
51 | // [assembly: AssemblyVersion("1.0.*")]
52 | [assembly: AssemblyVersion("1.0.0.0")]
53 | [assembly: AssemblyFileVersion("1.0.0.0")]
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/LicenseEngine/KeyCommon/KeyCommon.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {2024F82C-3306-41B5-951C-0B08F7C65590}
8 | Library
9 | Properties
10 | KeyCommon
11 | KeyCommon
12 | v4.5.2
13 | 512
14 | true
15 |
16 |
17 | true
18 | full
19 | false
20 | bin\Debug\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 |
25 |
26 | pdbonly
27 | true
28 | bin\Release\
29 | TRACE
30 | prompt
31 | 4
32 |
33 |
34 | true
35 |
36 |
37 | bin\Final Release\
38 | TRACE
39 | true
40 | pdbonly
41 | AnyCPU
42 | 7.3
43 | prompt
44 | MinimumRecommendedRules.ruleset
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/LicenseEngine/KeyGenerateUI/Properties/Resources.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.42000
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace KeyGenerateUI.Properties
12 | {
13 |
14 |
15 | ///
16 | /// A strongly-typed resource class, for looking up localized strings, etc.
17 | ///
18 | // This class was auto-generated by the StronglyTypedResourceBuilder
19 | // class via a tool like ResGen or Visual Studio.
20 | // To add or remove a member, edit your .ResX file then rerun ResGen
21 | // with the /str option, or rebuild your VS project.
22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
25 | internal class Resources
26 | {
27 |
28 | private static global::System.Resources.ResourceManager resourceMan;
29 |
30 | private static global::System.Globalization.CultureInfo resourceCulture;
31 |
32 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
33 | internal Resources()
34 | {
35 | }
36 |
37 | ///
38 | /// Returns the cached ResourceManager instance used by this class.
39 | ///
40 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
41 | internal static global::System.Resources.ResourceManager ResourceManager
42 | {
43 | get
44 | {
45 | if ((resourceMan == null))
46 | {
47 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("KeyGenerateUI.Properties.Resources", typeof(Resources).Assembly);
48 | resourceMan = temp;
49 | }
50 | return resourceMan;
51 | }
52 | }
53 |
54 | ///
55 | /// Overrides the current thread's CurrentUICulture property for all
56 | /// resource lookups using this strongly typed resource class.
57 | ///
58 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
59 | internal static global::System.Globalization.CultureInfo Culture
60 | {
61 | get
62 | {
63 | return resourceCulture;
64 | }
65 | set
66 | {
67 | resourceCulture = value;
68 | }
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/LicenseEngine/KeyGenerate/KeyGenerate.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {DF29A928-AB54-4752-9EA6-A05CC8FA6F3C}
8 | Library
9 | Properties
10 | KeyGenerate
11 | KeyGenerate
12 | v4.5.2
13 | 512
14 | true
15 |
16 |
17 | true
18 | full
19 | false
20 | bin\Debug\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 |
25 |
26 | pdbonly
27 | true
28 | bin\Release\
29 | TRACE
30 | prompt
31 | 4
32 | true
33 |
34 |
35 | bin\Final Release\
36 | TRACE
37 | true
38 | pdbonly
39 | AnyCPU
40 | 7.3
41 | prompt
42 | MinimumRecommendedRules.ruleset
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 | {2024f82c-3306-41b5-951c-0b08f7c65590}
62 | KeyCommon
63 |
64 |
65 |
66 |
--------------------------------------------------------------------------------
/LicenseEngine/LicenseEngine.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.29728.190
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KeyGenerateUI", "KeyGenerateUI\KeyGenerateUI.csproj", "{69A27771-A916-45AB-8C6F-5E015404EFCF}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KeyGenerate", "KeyGenerate\KeyGenerate.csproj", "{DF29A928-AB54-4752-9EA6-A05CC8FA6F3C}"
9 | EndProject
10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KeyVerify", "KeyVerify\KeyVerify.csproj", "{808A2A81-2113-4EAA-B595-9C2CD3D61595}"
11 | EndProject
12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KeyCommon", "KeyCommon\KeyCommon.csproj", "{2024F82C-3306-41B5-951C-0B08F7C65590}"
13 | EndProject
14 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KeyTest", "KeyTest\KeyTest.csproj", "{1D7BDB7B-C752-4581-A4D7-5E53995D620F}"
15 | EndProject
16 | Global
17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
18 | Debug|Any CPU = Debug|Any CPU
19 | Release|Any CPU = Release|Any CPU
20 | EndGlobalSection
21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
22 | {69A27771-A916-45AB-8C6F-5E015404EFCF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23 | {69A27771-A916-45AB-8C6F-5E015404EFCF}.Debug|Any CPU.Build.0 = Debug|Any CPU
24 | {69A27771-A916-45AB-8C6F-5E015404EFCF}.Release|Any CPU.ActiveCfg = Release|Any CPU
25 | {69A27771-A916-45AB-8C6F-5E015404EFCF}.Release|Any CPU.Build.0 = Release|Any CPU
26 | {DF29A928-AB54-4752-9EA6-A05CC8FA6F3C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27 | {DF29A928-AB54-4752-9EA6-A05CC8FA6F3C}.Debug|Any CPU.Build.0 = Debug|Any CPU
28 | {DF29A928-AB54-4752-9EA6-A05CC8FA6F3C}.Release|Any CPU.ActiveCfg = Release|Any CPU
29 | {DF29A928-AB54-4752-9EA6-A05CC8FA6F3C}.Release|Any CPU.Build.0 = Release|Any CPU
30 | {808A2A81-2113-4EAA-B595-9C2CD3D61595}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31 | {808A2A81-2113-4EAA-B595-9C2CD3D61595}.Debug|Any CPU.Build.0 = Debug|Any CPU
32 | {808A2A81-2113-4EAA-B595-9C2CD3D61595}.Release|Any CPU.ActiveCfg = Release|Any CPU
33 | {808A2A81-2113-4EAA-B595-9C2CD3D61595}.Release|Any CPU.Build.0 = Release|Any CPU
34 | {2024F82C-3306-41B5-951C-0B08F7C65590}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35 | {2024F82C-3306-41B5-951C-0B08F7C65590}.Debug|Any CPU.Build.0 = Debug|Any CPU
36 | {2024F82C-3306-41B5-951C-0B08F7C65590}.Release|Any CPU.ActiveCfg = Release|Any CPU
37 | {2024F82C-3306-41B5-951C-0B08F7C65590}.Release|Any CPU.Build.0 = Release|Any CPU
38 | {1D7BDB7B-C752-4581-A4D7-5E53995D620F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39 | {1D7BDB7B-C752-4581-A4D7-5E53995D620F}.Debug|Any CPU.Build.0 = Debug|Any CPU
40 | {1D7BDB7B-C752-4581-A4D7-5E53995D620F}.Release|Any CPU.ActiveCfg = Release|Any CPU
41 | {1D7BDB7B-C752-4581-A4D7-5E53995D620F}.Release|Any CPU.Build.0 = Release|Any CPU
42 | EndGlobalSection
43 | GlobalSection(SolutionProperties) = preSolution
44 | HideSolutionNode = FALSE
45 | EndGlobalSection
46 | GlobalSection(ExtensibilityGlobals) = postSolution
47 | SolutionGuid = {3AB11BA6-64C4-4084-BDBC-EFDCA38ED0B7}
48 | EndGlobalSection
49 | EndGlobal
50 |
--------------------------------------------------------------------------------
/LicenseEngine/KeyVerify/KeyVerify.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {808A2A81-2113-4EAA-B595-9C2CD3D61595}
8 | Library
9 | Properties
10 | KeyVerify
11 | KeyVerify
12 | v4.5.2
13 | 512
14 | true
15 |
16 |
17 | true
18 | full
19 | false
20 | bin\Debug\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 |
25 |
26 | pdbonly
27 | true
28 | bin\Release\
29 | TRACE
30 | prompt
31 | 4
32 | true
33 |
34 |
35 | bin\Final Release\
36 | TRACE
37 | true
38 | pdbonly
39 | AnyCPU
40 | 7.3
41 | prompt
42 | MinimumRecommendedRules.ruleset
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 | {2024f82c-3306-41b5-951c-0b08f7c65590}
64 | KeyCommon
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/LicenseEngine/KeyVerify/ActivationKeyFileCheck.cs:
--------------------------------------------------------------------------------
1 | using System.IO;
2 | using System.Xml;
3 | using KeyCommon;
4 |
5 | namespace KeyVerify
6 | {
7 | // GB 2013/05/09 - Activation Key File functionality is not used in installer
8 | // or program execution as it complicates install, and utilisation of this tool.
9 | // Class made internal so as not to expose as part of API
10 |
11 | ///
12 | /// Provides means of verifying a licence key file
13 | ///
14 | internal /*public*/ class ActivationKeyFileCheck
15 | {
16 | ///
17 | /// Check activation key file for valid licence key or trial period value
18 | ///
19 | /// The absolute local path where the activation key file exists
20 | ///
21 | /// An encryption key which the licence key XML will be decrypted with
22 | /// A friendly application name for error reporting e.g. 'My Licenced Application'
23 | ///
24 | public void CheckActivationKeyFile(string activationKeyFileFullPath, KeyByteSet keyByteSet1,
25 | KeyByteSet keyByteSet2, string licenceKeyFileEncryptionString, string friendlyApplicationName)
26 | {
27 | if (!File.Exists(activationKeyFileFullPath))
28 | throw new ActivationKeyFileNotPresentException(
29 | "The product activation key file '" + activationKeyFileFullPath + "' was expected at '" +
30 | activationKeyFileFullPath +
31 | "', but was not found. Please visit the vendor website to obtain a product activation key file.");
32 |
33 | var xmlDoc = new XmlDocument();
34 | xmlDoc.Load(activationKeyFileFullPath);
35 |
36 | var activationCodeEncrypted = xmlDoc.GetElementsByTagName("Key");
37 |
38 | var activationKeyEncrypted = activationCodeEncrypted[0].InnerText;
39 |
40 | if (!string.IsNullOrEmpty(activationKeyEncrypted))
41 | {
42 | string clearTextActivationKey;
43 |
44 | try
45 | {
46 | clearTextActivationKey =
47 | ActivationKeyDecryption.Decrypt(activationKeyEncrypted, licenceKeyFileEncryptionString);
48 | }
49 | catch
50 | {
51 | throw new ActivationKeyInvalidException(string.Format(
52 | "The licence key that allows {0} to run is invalid. Please check that the key file exists in the executing directory, and has not been modified.",
53 | friendlyApplicationName));
54 | }
55 |
56 | // If a check is made, that means that the free trial build period has expired
57 |
58 | if (clearTextActivationKey.ToLower() == "trial")
59 | throw new TrialPeriodExpiredException(string.Format("The trial period for {0} has expired.",
60 | friendlyApplicationName));
61 |
62 | var pkvLicenceKeyResult =
63 | new KeyCheck().CheckKey(clearTextActivationKey, new[] {keyByteSet1, keyByteSet2}, 8, null);
64 |
65 | if (pkvLicenceKeyResult != LicenseKeyResult.KeyGood)
66 | throw new ActivationKeyInvalidException(string.Format(
67 | "The licence key that allows {0} to run is invalid. Please check that the key file exists in the executing directory, and has not been modified.",
68 | friendlyApplicationName));
69 | }
70 | }
71 | }
72 | }
--------------------------------------------------------------------------------
/LicenseEngine/KeyGenerateUI/MainWindow.xaml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
31 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
65 |
71 |
79 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
104 |
111 |
112 |
113 |
114 |
--------------------------------------------------------------------------------
/LicenseEngine/KeyVerify/ActivationKeyDecryption.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using System.Security.Cryptography;
4 | using System.Text;
5 |
6 | namespace KeyVerify
7 | {
8 | internal class ActivationKeyDecryption
9 | {
10 | ///
11 | /// Decrypt a string into a string using a custom password. Uses Decrypt(byte[], byte[], byte[])
12 | ///
13 | ///
14 | ///
15 | ///
16 | public static string Decrypt(string cipherText, string password)
17 | {
18 | // First we need to turn the input string into a byte array.
19 | // We presume that Base64 encoding was used
20 |
21 | var cipherBytes = Convert.FromBase64String(cipherText);
22 |
23 | // Then, we need to turn the password into Key and IV
24 | // We are using salt to make it harder to guess our key
25 | // using a dictionary attack -
26 | // trying to guess a password by enumerating all possible words.
27 |
28 | var pdb = new PasswordDeriveBytes(password,
29 | new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
30 |
31 | // Now get the key/IV and do the decryption using
32 | // the function that accepts byte arrays.
33 | // Using PasswordDeriveBytes object we are first
34 | // getting 32 bytes for the Key
35 | // (the default Rijndael key length is 256bit = 32bytes)
36 | // and then 16 bytes for the IV.
37 | // IV should always be the block size, which is by
38 | // default 16 bytes (128 bit) for Rijndael.
39 | // If you are using DES/TripleDES/RC2 the block size is
40 | // 8 bytes and so should be the IV size.
41 | // You can also read KeySize/BlockSize properties off
42 | // the algorithm to find out the sizes.
43 |
44 | var decryptedData = Decrypt(cipherBytes,
45 | pdb.GetBytes(32), pdb.GetBytes(16));
46 |
47 | // Now we need to turn the resulting byte array into a string.
48 | // A common mistake would be to use an Encoding class for that.
49 | // It does not work
50 | // because not all byte values can be represented by characters.
51 | // We are going to be using Base64 encoding that is
52 | // designed exactly for what we are trying to do.
53 |
54 | return Encoding.Unicode.GetString(decryptedData);
55 | }
56 |
57 | ///
58 | /// Decrypt a byte array into a byte array using a key and an IV
59 | ///
60 | ///
61 | ///
62 | ///
63 | ///
64 | public static byte[] Decrypt(byte[] cipherData, byte[] key, byte[] iv)
65 | {
66 | // Create a MemoryStream that is going to accept the
67 | // decrypted bytes
68 |
69 | var ms = new MemoryStream();
70 |
71 | // Create a symmetric algorithm.
72 | // We are going to use Rijndael because it is strong and
73 | // available on all platforms.
74 | // You can use other algorithms, to do so substitute the next
75 | // line with something like
76 | // TripleDES alg = TripleDES.Create();
77 |
78 | var alg = Rijndael.Create();
79 |
80 | // Now set the key and the IV.
81 | // We need the IV (Initialization Vector) because the algorithm
82 | // is operating in its default
83 | // mode called CBC (Cipher Block Chaining). The IV is XORed with
84 | // the first block (8 byte)
85 | // of the data after it is decrypted, and then each decrypted
86 | // block is XORed with the previous
87 | // cipher block. This is done to make encryption more secure.
88 | // There is also a mode called ECB which does not need an IV,
89 | // but it is much less secure.
90 |
91 | alg.Key = key;
92 | alg.IV = iv;
93 |
94 | // Create a CryptoStream through which we are going to be
95 | // pumping our data.
96 | // CryptoStreamMode.Write means that we are going to be
97 | // writing data to the stream
98 | // and the output will be written in the MemoryStream
99 | // we have provided.
100 |
101 | var cs = new CryptoStream(ms,
102 | alg.CreateDecryptor(), CryptoStreamMode.Write);
103 |
104 | // Write the data and make it do the decryption
105 |
106 | cs.Write(cipherData, 0, cipherData.Length);
107 |
108 | // Close the crypto stream (or do FlushFinalBlock).
109 | // This will tell it that we have done our decryption
110 | // and there is no more data coming in,
111 | // and it is now a good time to remove the padding
112 | // and finalize the decryption process.
113 |
114 | cs.Close();
115 |
116 | // Now get the decrypted data from the MemoryStream.
117 | // Some people make a mistake of using GetBuffer() here,
118 | // which is not the right way.
119 |
120 | var decryptedData = ms.ToArray();
121 |
122 | return decryptedData;
123 | }
124 | }
125 | }
--------------------------------------------------------------------------------
/LicenseEngine/KeyTest/KeyTest.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Debug
7 | AnyCPU
8 | {1D7BDB7B-C752-4581-A4D7-5E53995D620F}
9 | Library
10 | Properties
11 | KeyTest
12 | KeyTest
13 | v4.5.2
14 | 512
15 | {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
16 | 15.0
17 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)
18 | $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages
19 | False
20 | UnitTest
21 |
22 |
23 |
24 |
25 | true
26 | full
27 | false
28 | bin\Debug\
29 | DEBUG;TRACE
30 | prompt
31 | 4
32 |
33 |
34 | pdbonly
35 | true
36 | bin\Release\
37 | TRACE
38 | prompt
39 | 4
40 | true
41 |
42 |
43 | bin\Final Release\
44 | TRACE
45 | true
46 | pdbonly
47 | AnyCPU
48 | 7.3
49 | prompt
50 | MinimumRecommendedRules.ruleset
51 |
52 |
53 |
54 | ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll
55 |
56 |
57 | ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 | {2024f82c-3306-41b5-951c-0b08f7c65590}
72 | KeyCommon
73 |
74 |
75 | {df29a928-ab54-4752-9ea6-a05cc8fa6f3c}
76 | KeyGenerate
77 |
78 |
79 | {808a2a81-2113-4eaa-b595-9c2cd3d61595}
80 | KeyVerify
81 |
82 |
83 |
84 |
85 |
86 |
87 | This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.
88 |
89 |
90 |
91 |
92 |
93 |
--------------------------------------------------------------------------------
/LicenseEngine/KeyGenerateUI/Properties/Resources.resx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 | text/microsoft-resx
107 |
108 |
109 | 2.0
110 |
111 |
112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
113 |
114 |
115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
116 |
117 |
--------------------------------------------------------------------------------
/LicenseEngine/KeyTest/KeyTests.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using KeyCommon;
3 | using KeyGenerate;
4 | using KeyVerify;
5 | using Microsoft.VisualStudio.TestTools.UnitTesting;
6 |
7 | namespace KeyTest
8 | {
9 | [TestClass]
10 | public class KeyTests
11 | {
12 | [TestMethod]
13 | public void Test_pkv_licence_key_generation_and_verification()
14 | {
15 | var pkvLicenceKey = new KeyGenerator();
16 |
17 | var pkvKeyCheck = new KeyCheck();
18 |
19 | var key = string.Empty;
20 |
21 | KeyByteSet[] keyByteSets =
22 | {
23 | new KeyByteSet(1, 58, 6, 97),
24 | new KeyByteSet(2, 96, 254, 23),
25 | new KeyByteSet(3, 11, 185, 69),
26 | new KeyByteSet(4, 2, 93, 41),
27 | new KeyByteSet(5, 62, 4, 234),
28 | new KeyByteSet(6, 200, 56, 49),
29 | new KeyByteSet(7, 89, 45, 142),
30 | new KeyByteSet(8, 6, 88, 32)
31 | };
32 |
33 | // Change these to a random key byte set from the above array to test key verification with
34 |
35 | var kbs1 = keyByteSets[3];
36 | var kbs2 = keyByteSets[7];
37 | var kbs3 = keyByteSets[4];
38 |
39 | // The check project also uses a class called KeyByteSet, but with
40 | // separate name spacing to achieve single self contained dll
41 |
42 | var keyByteSet1 =
43 | new KeyByteSet(kbs1.KeyByteNo, kbs1.KeyByteA, kbs1.KeyByteB, kbs1.KeyByteC); // Change no to test others
44 | var keyByteSet2 = new KeyByteSet(kbs2.KeyByteNo, kbs2.KeyByteA, kbs2.KeyByteB, kbs2.KeyByteC);
45 | var keyByteSet3 = new KeyByteSet(kbs3.KeyByteNo, kbs3.KeyByteA, kbs3.KeyByteB, kbs3.KeyByteC);
46 |
47 | for (var i = 0; i < 10000; i++)
48 | {
49 | var seed = new Random().Next(0, int.MaxValue);
50 |
51 | key = pkvLicenceKey.MakeKey(seed, keyByteSets);
52 |
53 | // Check that check sum validation passes
54 | Assert.IsTrue(pkvKeyCheck.CheckKeyChecksum(key, keyByteSets.Length));
55 |
56 | // Check using full check method
57 | Assert.IsTrue(pkvKeyCheck.CheckKey(
58 | key,
59 | new[] {keyByteSet1, keyByteSet2, keyByteSet3},
60 | keyByteSets.Length,
61 | null
62 | ) == LicenseKeyResult.KeyGood, "Failed on iteration " + i
63 | );
64 |
65 | // Check that erroneous check sum validation fails
66 | Assert.IsFalse(pkvKeyCheck.CheckKeyChecksum(key.Remove(23, 1) + "A",
67 | keyByteSets.Length)); // Change key by replacing 17th char
68 | }
69 |
70 | // Check a few random inputs
71 | Assert.IsFalse(pkvKeyCheck.CheckKey("adcsadrewf",
72 | new[] {keyByteSet1, keyByteSet2},
73 | keyByteSets.Length,
74 | null) == LicenseKeyResult.KeyGood
75 | );
76 | Assert.IsFalse(pkvKeyCheck.CheckKey("",
77 | new[] {keyByteSet1, keyByteSet2},
78 | keyByteSets.Length,
79 | null) == LicenseKeyResult.KeyGood
80 | );
81 | Assert.IsFalse(pkvKeyCheck.CheckKey("123",
82 | new[] {keyByteSet1, keyByteSet2},
83 | keyByteSets.Length,
84 | null) == LicenseKeyResult.KeyGood
85 | );
86 | Assert.IsFalse(pkvKeyCheck.CheckKey("*()",
87 | new[] {keyByteSet1, keyByteSet2},
88 | keyByteSets.Length,
89 | null) == LicenseKeyResult.KeyGood
90 | );
91 | Assert.IsFalse(pkvKeyCheck.CheckKey("dasdasdasgdjwqidqiwd21887127eqwdaishxckjsabcxjkabskdcbq2e81y12e8712",
92 | new[] {keyByteSet1, keyByteSet2},
93 | keyByteSets.Length,
94 | null) == LicenseKeyResult.KeyGood
95 | );
96 | }
97 |
98 | [TestMethod]
99 | public void
100 | Test_PKV_licence_key_generation_and_verification_with_random_key_bytes_key_byte_qty_and_verification_key_byte_selection()
101 | {
102 | var pkvLicenceKey = new KeyGenerator();
103 |
104 | var pkvKeyCheck = new KeyCheck();
105 |
106 | for (var i = 0; i < 10000; i++)
107 | {
108 | var randomKeyByteSetsLength = new Random().Next(2, 400);
109 |
110 | var keyByteSets = new KeyByteSet[randomKeyByteSetsLength];
111 |
112 | for (var j = 0; j < randomKeyByteSetsLength; j++)
113 | {
114 | var random = new Random();
115 |
116 | var kbs = new KeyByteSet
117 | (
118 | j + 1,
119 | (byte) random.Next(0, 256),
120 | (byte) random.Next(0, 256),
121 | (byte) random.Next(0, 256)
122 | );
123 |
124 | keyByteSets[j] = kbs;
125 | }
126 |
127 | // Select a random key byte set to test key verification with
128 |
129 | var kbs1 = keyByteSets[new Random().Next(0, randomKeyByteSetsLength)];
130 | var kbs2 = keyByteSets[new Random().Next(0, randomKeyByteSetsLength)];
131 |
132 | // The check project also uses a class called KeyByteSet, but with
133 | // separate name spacing to achieve single self contained dll
134 |
135 | var keyByteSet1 =
136 | new KeyByteSet(kbs1.KeyByteNo, kbs1.KeyByteA, kbs1.KeyByteB,
137 | kbs1.KeyByteC); // Change no to test others
138 | var keyByteSet2 = new KeyByteSet(kbs2.KeyByteNo, kbs2.KeyByteA, kbs2.KeyByteB, kbs2.KeyByteC);
139 |
140 | var seed = new Random().Next(0, int.MaxValue);
141 |
142 | var key = pkvLicenceKey.MakeKey(seed, keyByteSets);
143 |
144 | // Check that check sum validation passes
145 |
146 | Assert.IsTrue(pkvKeyCheck.CheckKeyChecksum(key, keyByteSets.Length));
147 |
148 | // Check using full check method
149 |
150 | Assert.IsTrue(pkvKeyCheck.CheckKey(
151 | key,
152 | new[] {keyByteSet1, keyByteSet2},
153 | keyByteSets.Length,
154 | null
155 | ) == LicenseKeyResult.KeyGood, "Failed on iteration " + i
156 | );
157 | }
158 | }
159 | }
160 | }
--------------------------------------------------------------------------------
/LicenseEngine/KeyGenerateUI/KeyGenerateUI.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {69A27771-A916-45AB-8C6F-5E015404EFCF}
8 | WinExe
9 | KeyGenerateUI
10 | KeyGenerateUI
11 | v4.5.2
12 | 512
13 | {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
14 | 4
15 | true
16 |
17 |
18 | AnyCPU
19 | true
20 | full
21 | false
22 | bin\Debug\
23 | DEBUG;TRACE
24 | prompt
25 | 4
26 |
27 |
28 | AnyCPU
29 | pdbonly
30 | true
31 | bin\Release\
32 | TRACE
33 | prompt
34 | 4
35 | true
36 |
37 |
38 | bin\Final Release\
39 | TRACE
40 | true
41 | pdbonly
42 | AnyCPU
43 | 7.3
44 | prompt
45 | MinimumRecommendedRules.ruleset
46 | true
47 |
48 |
49 | icon.ico
50 |
51 |
52 |
53 |
54 | ..\packages\ControlzEx.3.0.2.4\lib\net45\ControlzEx.dll
55 |
56 |
57 | ..\packages\MahApps.Metro.1.6.5\lib\net45\MahApps.Metro.dll
58 |
59 |
60 | ..\packages\MahApps.Metro.IconPacks.Modern.3.2.0\lib\net45\MahApps.Metro.IconPacks.Core.dll
61 |
62 |
63 | ..\packages\MahApps.Metro.IconPacks.Modern.3.2.0\lib\net45\MahApps.Metro.IconPacks.Modern.dll
64 |
65 |
66 |
67 |
68 | ..\packages\ControlzEx.3.0.2.4\lib\net45\System.Windows.Interactivity.dll
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 | 4.0
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 | MSBuild:Compile
86 | Designer
87 |
88 |
89 | MSBuild:Compile
90 | Designer
91 |
92 |
93 | App.xaml
94 | Code
95 |
96 |
97 | MainWindow.xaml
98 | Code
99 |
100 |
101 |
102 |
103 | Code
104 |
105 |
106 | True
107 | True
108 | Resources.resx
109 |
110 |
111 | True
112 | Settings.settings
113 | True
114 |
115 |
116 | ResXFileCodeGenerator
117 | Resources.Designer.cs
118 |
119 |
120 |
121 | SettingsSingleFileGenerator
122 | Settings.Designer.cs
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 | {2024f82c-3306-41b5-951c-0b08f7c65590}
131 | KeyCommon
132 |
133 |
134 | {df29a928-ab54-4752-9ea6-a05cc8fa6f3c}
135 | KeyGenerate
136 |
137 |
138 | {808a2a81-2113-4eaa-b595-9c2cd3d61595}
139 | KeyVerify
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.rsuser
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Build results
17 | [Dd]ebug/
18 | [Dd]ebugPublic/
19 | [Rr]elease/
20 | [Rr]eleases/
21 | x64/
22 | x86/
23 | [Aa][Rr][Mm]/
24 | [Aa][Rr][Mm]64/
25 | bld/
26 | [Bb]in/
27 | [Oo]bj/
28 | [Ll]og/
29 |
30 | # Visual Studio 2015/2017 cache/options directory
31 | .vs/
32 | # Uncomment if you have tasks that create the project's static files in wwwroot
33 | #wwwroot/
34 |
35 | # Visual Studio 2017 auto generated files
36 | Generated\ Files/
37 |
38 | # MSTest test Results
39 | [Tt]est[Rr]esult*/
40 | [Bb]uild[Ll]og.*
41 |
42 | # NUNIT
43 | *.VisualState.xml
44 | TestResult.xml
45 |
46 | # Build Results of an ATL Project
47 | [Dd]ebugPS/
48 | [Rr]eleasePS/
49 | dlldata.c
50 |
51 | # Benchmark Results
52 | BenchmarkDotNet.Artifacts/
53 |
54 | # .NET Core
55 | project.lock.json
56 | project.fragment.lock.json
57 | artifacts/
58 |
59 | # StyleCop
60 | StyleCopReport.xml
61 |
62 | # Files built by Visual Studio
63 | *_i.c
64 | *_p.c
65 | *_h.h
66 | *.ilk
67 | *.meta
68 | *.obj
69 | *.iobj
70 | *.pch
71 | *.pdb
72 | *.ipdb
73 | *.pgc
74 | *.pgd
75 | *.rsp
76 | *.sbr
77 | *.tlb
78 | *.tli
79 | *.tlh
80 | *.tmp
81 | *.tmp_proj
82 | *_wpftmp.csproj
83 | *.log
84 | *.vspscc
85 | *.vssscc
86 | .builds
87 | *.pidb
88 | *.svclog
89 | *.scc
90 |
91 | # Chutzpah Test files
92 | _Chutzpah*
93 |
94 | # Visual C++ cache files
95 | ipch/
96 | *.aps
97 | *.ncb
98 | *.opendb
99 | *.opensdf
100 | *.sdf
101 | *.cachefile
102 | *.VC.db
103 | *.VC.VC.opendb
104 |
105 | # Visual Studio profiler
106 | *.psess
107 | *.vsp
108 | *.vspx
109 | *.sap
110 |
111 | # Visual Studio Trace Files
112 | *.e2e
113 |
114 | # TFS 2012 Local Workspace
115 | $tf/
116 |
117 | # Guidance Automation Toolkit
118 | *.gpState
119 |
120 | # ReSharper is a .NET coding add-in
121 | _ReSharper*/
122 | *.[Rr]e[Ss]harper
123 | *.DotSettings.user
124 |
125 | # JustCode is a .NET coding add-in
126 | .JustCode
127 |
128 | # TeamCity is a build add-in
129 | _TeamCity*
130 |
131 | # DotCover is a Code Coverage Tool
132 | *.dotCover
133 |
134 | # AxoCover is a Code Coverage Tool
135 | .axoCover/*
136 | !.axoCover/settings.json
137 |
138 | # Visual Studio code coverage results
139 | *.coverage
140 | *.coveragexml
141 |
142 | # NCrunch
143 | _NCrunch_*
144 | .*crunch*.local.xml
145 | nCrunchTemp_*
146 |
147 | # MightyMoose
148 | *.mm.*
149 | AutoTest.Net/
150 |
151 | # Web workbench (sass)
152 | .sass-cache/
153 |
154 | # Installshield output folder
155 | [Ee]xpress/
156 |
157 | # DocProject is a documentation generator add-in
158 | DocProject/buildhelp/
159 | DocProject/Help/*.HxT
160 | DocProject/Help/*.HxC
161 | DocProject/Help/*.hhc
162 | DocProject/Help/*.hhk
163 | DocProject/Help/*.hhp
164 | DocProject/Help/Html2
165 | DocProject/Help/html
166 |
167 | # Click-Once directory
168 | publish/
169 |
170 | # Publish Web Output
171 | *.[Pp]ublish.xml
172 | *.azurePubxml
173 | # Note: Comment the next line if you want to checkin your web deploy settings,
174 | # but database connection strings (with potential passwords) will be unencrypted
175 | *.pubxml
176 | *.publishproj
177 |
178 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
179 | # checkin your Azure Web App publish settings, but sensitive information contained
180 | # in these scripts will be unencrypted
181 | PublishScripts/
182 |
183 | # NuGet Packages
184 | *.nupkg
185 | # The packages folder can be ignored because of Package Restore
186 | **/[Pp]ackages/*
187 | # except build/, which is used as an MSBuild target.
188 | !**/[Pp]ackages/build/
189 | # Uncomment if necessary however generally it will be regenerated when needed
190 | #!**/[Pp]ackages/repositories.config
191 | # NuGet v3's project.json files produces more ignorable files
192 | *.nuget.props
193 | *.nuget.targets
194 |
195 | # Microsoft Azure Build Output
196 | csx/
197 | *.build.csdef
198 |
199 | # Microsoft Azure Emulator
200 | ecf/
201 | rcf/
202 |
203 | # Windows Store app package directories and files
204 | AppPackages/
205 | BundleArtifacts/
206 | Package.StoreAssociation.xml
207 | _pkginfo.txt
208 | *.appx
209 |
210 | # Visual Studio cache files
211 | # files ending in .cache can be ignored
212 | *.[Cc]ache
213 | # but keep track of directories ending in .cache
214 | !?*.[Cc]ache/
215 |
216 | # Others
217 | ClientBin/
218 | ~$*
219 | *~
220 | *.dbmdl
221 | *.dbproj.schemaview
222 | *.jfm
223 | *.pfx
224 | *.publishsettings
225 | orleans.codegen.cs
226 |
227 | # Including strong name files can present a security risk
228 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
229 | #*.snk
230 |
231 | # Since there are multiple workflows, uncomment next line to ignore bower_components
232 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
233 | #bower_components/
234 |
235 | # RIA/Silverlight projects
236 | Generated_Code/
237 |
238 | # Backup & report files from converting an old project file
239 | # to a newer Visual Studio version. Backup files are not needed,
240 | # because we have git ;-)
241 | _UpgradeReport_Files/
242 | Backup*/
243 | UpgradeLog*.XML
244 | UpgradeLog*.htm
245 | ServiceFabricBackup/
246 | *.rptproj.bak
247 |
248 | # SQL Server files
249 | *.mdf
250 | *.ldf
251 | *.ndf
252 |
253 | # Business Intelligence projects
254 | *.rdl.data
255 | *.bim.layout
256 | *.bim_*.settings
257 | *.rptproj.rsuser
258 | *- Backup*.rdl
259 |
260 | # Microsoft Fakes
261 | FakesAssemblies/
262 |
263 | # GhostDoc plugin setting file
264 | *.GhostDoc.xml
265 |
266 | # Node.js Tools for Visual Studio
267 | .ntvs_analysis.dat
268 | node_modules/
269 |
270 | # Visual Studio 6 build log
271 | *.plg
272 |
273 | # Visual Studio 6 workspace options file
274 | *.opt
275 |
276 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
277 | *.vbw
278 |
279 | # Visual Studio LightSwitch build output
280 | **/*.HTMLClient/GeneratedArtifacts
281 | **/*.DesktopClient/GeneratedArtifacts
282 | **/*.DesktopClient/ModelManifest.xml
283 | **/*.Server/GeneratedArtifacts
284 | **/*.Server/ModelManifest.xml
285 | _Pvt_Extensions
286 |
287 | # Paket dependency manager
288 | .paket/paket.exe
289 | paket-files/
290 |
291 | # FAKE - F# Make
292 | .fake/
293 |
294 | # JetBrains Rider
295 | .idea/
296 | *.sln.iml
297 |
298 | # CodeRush personal settings
299 | .cr/personal
300 |
301 | # Python Tools for Visual Studio (PTVS)
302 | __pycache__/
303 | *.pyc
304 |
305 | # Cake - Uncomment if you are using it
306 | # tools/**
307 | # !tools/packages.config
308 |
309 | # Tabs Studio
310 | *.tss
311 |
312 | # Telerik's JustMock configuration file
313 | *.jmconfig
314 |
315 | # BizTalk build output
316 | *.btp.cs
317 | *.btm.cs
318 | *.odx.cs
319 | *.xsd.cs
320 |
321 | # OpenCover UI analysis results
322 | OpenCover/
323 |
324 | # Azure Stream Analytics local run output
325 | ASALocalRun/
326 |
327 | # MSBuild Binary and Structured Log
328 | *.binlog
329 |
330 | # NVidia Nsight GPU debugger configuration file
331 | *.nvuser
332 |
333 | # MFractors (Xamarin productivity tool) working folder
334 | .mfractor/
335 |
336 | # Local History for Visual Studio
337 | .localhistory/
338 |
339 | # BeatPulse healthcheck temp database
340 | healthchecksdb
--------------------------------------------------------------------------------
/LicenseEngine/KeyGenerate/KeyGenerator.cs:
--------------------------------------------------------------------------------
1 | // This Key Generator was built with the assistance of the article linked below (credit to Brandon Staggs).
2 | // Brandon Staggs article provided example in Delphi. Some functionality has been ported to C#
3 | // to create this key generator.
4 |
5 | // http://www.brandonstaggs.com/2007/07/26/implementing-a-partial-serial-number-verification-system-in-delphi/
6 |
7 | // Delphi -> .NET mapping concept notes
8 |
9 | // Delphi shift right http://www.delphibasics.co.uk/RTL.asp?Name=Shr
10 | // C# Shift right http://www.blackwasp.co.uk/CSharpShiftOperators.aspx
11 | // Delphi Word - equivalent to ushort http://www.delphibasics.co.uk/RTL.asp?Name=Word
12 | // Delphi Decrement - https://www.google.co.uk/search?q=delphi+Dec&oq=delphi+Dec&sugexp=chrome,mod=3&sourceid=chrome&ie=UTF-8
13 | // C# string to hex http://stackoverflow.com/questions/5426582/turn-byte-into-two-digit-hexadecimal-number-just-using-tostring
14 |
15 | using System;
16 | using System.Collections.Generic;
17 | using System.IO;
18 | using System.Reflection;
19 | using KeyCommon;
20 |
21 | namespace KeyGenerate
22 | {
23 | public class KeyGenerator
24 | {
25 | ///
26 | /// Retrieve the directory path for where the executing assembly resides.
27 | ///
28 | private static string AssemblyDirectory
29 | {
30 | // From http://stackoverflow.com/questions/52797/how-do-i-get-the-path-of-the-assembly-the-code-is-in
31 |
32 | get
33 | {
34 | var codeBase = Assembly.GetExecutingAssembly().CodeBase;
35 | var uri = new UriBuilder(codeBase);
36 | var path = Uri.UnescapeDataString(uri.Path);
37 |
38 | const char folderSeparator = '/';
39 |
40 | if (path.IndexOf(folderSeparator) != -1) path = path.Substring(0, path.LastIndexOf(folderSeparator));
41 |
42 | // Using Path.GetDirectoryName(path); as in the online example was causing me to loose the
43 | // debug folder from the path (when using under unit test conditions at least). Using manual
44 | // last directory position detection using folder char fixes this issue.
45 |
46 | // Convert / to \ without loosing directory levels
47 |
48 | return Path.GetFullPath(path);
49 | }
50 | }
51 |
52 | ///
53 | /// Generate a new key given a seed value. This seed should be unique so that where licences are blacklisted,
54 | /// we only blacklist one key. Store the seed when generating new licences, or put in place some other mechanism so
55 | /// that
56 | /// the key will not be repeated for the same application. This seed does not necessarily have to be randomised.
57 | ///
58 | /// Random number
59 | /// A list of key bytes that will be used to produce the key
60 | ///
61 | public string MakeKey(int seed, KeyByteSet[] keyByteSets)
62 | {
63 | if (keyByteSets.Length < 2)
64 | throw new InvalidOperationException("The KeyByteSet array must be of length 2 or greater.");
65 |
66 | // Check that array is in correct order as this will cause errors if passed in incorrectly
67 |
68 | Array.Sort(keyByteSets, new KeyByteSetComparer());
69 |
70 | var allKeyByteNosDistinct = true;
71 |
72 | var keyByteCheckedNos = new List();
73 |
74 | var maxKeyByteNo = 0;
75 |
76 | foreach (var keyByteSet in keyByteSets)
77 | if (!keyByteCheckedNos.Contains(keyByteSet.KeyByteNo))
78 | {
79 | keyByteCheckedNos.Add(keyByteSet.KeyByteNo);
80 |
81 | if (keyByteSet.KeyByteNo > maxKeyByteNo) maxKeyByteNo = keyByteSet.KeyByteNo;
82 | }
83 | else
84 | {
85 | allKeyByteNosDistinct = false;
86 | break;
87 | }
88 |
89 | if (!allKeyByteNosDistinct)
90 | throw new InvalidOperationException(
91 | "The KeyByteSet array contained at least 1 item with a duplicate KeyByteNo value.");
92 |
93 | if (maxKeyByteNo != keyByteSets.Length)
94 | throw new InvalidOperationException(
95 | "The values for KeyByteNo in each KeyByteSet item must be sequential and start with the number 1.");
96 |
97 | // Note these seed value, with random numbers need to be repeated in check function.
98 | // The more of these values the better, but they will also increase the length of the
99 | // key by 2 chars each. Changing the length of the key is not something to be done
100 | // without testing, since some operations depend on certain portions of the key
101 | // being found at specific indexes.
102 |
103 | var keyBytes = new byte[keyByteSets.Length];
104 |
105 | for (var i = 0; i < keyByteSets.Length; i++)
106 | keyBytes[i] = GetKeyByte(
107 | seed,
108 | keyByteSets[i].KeyByteA,
109 | keyByteSets[i].KeyByteB,
110 | keyByteSets[i].KeyByteC
111 | );
112 |
113 | // The key string begins with a hexidecimal string of the seed
114 |
115 | var result = seed.ToString("X8"); // 8 digit hex;
116 |
117 | for (var i = 0; i < keyBytes.Length; i++) result = result + keyBytes[i].ToString("X2");
118 |
119 | result = result + GetChecksum(result);
120 |
121 | // Insert hyphens every 6 chars for readability
122 |
123 | var startPos = 7;
124 |
125 | while (startPos < result.Length - 1)
126 | {
127 | result = result.Insert(startPos, "-");
128 |
129 | startPos = startPos + 7;
130 | }
131 |
132 | return result;
133 | }
134 |
135 | ////////////////////////////////////////////////////
136 | // Code below from here is duplicated across both
137 | // private and public projects / dlls
138 | ////////////////////////////////////////////////////
139 |
140 | ///
141 | /// Given a seed and some input bytes, generate a single byte to return. This should
142 | /// be used with randomised data, that can be represented to retrieve the same key.
143 | ///
144 | ///
145 | ///
146 | ///
147 | ///
148 | ///
149 | private byte GetKeyByte(long seed, byte a, byte b, byte c)
150 | {
151 | var aTemp = a % 25;
152 | var bTemp = b % 3;
153 |
154 | long result;
155 |
156 | if (a % 2 == 0)
157 | result = ((seed >> aTemp) & 0xFF) ^ ((seed >> bTemp) | c);
158 | else
159 | result = ((seed >> aTemp) & 0xFF) ^ ((seed >> bTemp) & c);
160 |
161 | return (byte) result;
162 | }
163 |
164 | ///
165 | /// Generate a new checksum for a key
166 | ///
167 | ///
168 | ///
169 | private string GetChecksum(string str)
170 | {
171 | ushort left = 0x56;
172 | ushort right = 0xAF;
173 |
174 | if (str.Length > 0)
175 | // 0xFF hex for 255
176 |
177 | for (var cnt = 0; cnt < str.Length; cnt++)
178 | {
179 | right = (ushort) (right + Convert.ToByte(str[cnt]));
180 |
181 | if (right > 0xFF) right -= 0xFF;
182 |
183 | left += right;
184 |
185 | if (left > 0xFF) left -= 0xFF;
186 | }
187 |
188 | var sum = (ushort) ((left << 8) + right);
189 |
190 | return sum.ToString("X4"); // 4 char hex
191 | }
192 | }
193 | }
--------------------------------------------------------------------------------
/LicenseEngine/KeyVerify/KeyCheck.cs:
--------------------------------------------------------------------------------
1 | #define Key01
2 | #undef Key02
3 | #undef Key03
4 | #undef Key04
5 | #undef Key05
6 | #undef Key06
7 | #define Key07
8 | #undef Key08
9 |
10 | using System;
11 | using System.Globalization;
12 | using KeyCommon;
13 |
14 | namespace KeyVerify
15 | {
16 | ///
17 | /// Provides methods for verifying a licence key.
18 | ///
19 | public class KeyCheck
20 | {
21 | ///
22 | /// Check a given key for validity
23 | ///
24 | /// The full key
25 | /// The KeyBytes that are to be tested in this check
26 | /// The total number of KeyBytes used to make the key
27 | /// Any seed values (hex string representation) that should be banned
28 | ///
29 | public LicenseKeyResult CheckKey(
30 | string key,
31 | KeyByteSet[] keyByteSetsToCheck,
32 | int totalKeyByteSets,
33 | string[] blackListedSeeds
34 | )
35 | {
36 | key = FormatKeyForCompare(key);
37 |
38 | var result = LicenseKeyResult.KeyInvalid;
39 |
40 | var checksumPass = CheckKeyChecksum(key, totalKeyByteSets);
41 |
42 | if (checksumPass)
43 | {
44 | if (blackListedSeeds != null && blackListedSeeds.Length > 0)
45 | // Test key against our black list
46 |
47 | // Example black listed seed: 111111 (Hex val). Producing keys with the same
48 | // seed and key bytes will produce the same key, so using a seed such as a user id
49 | // can provide a mechanism for tracking the source of any keys that are found to
50 | // be used out of licence terms.
51 |
52 | for (var i = 0; i < blackListedSeeds.Length; i++)
53 | if (key.StartsWith(blackListedSeeds[i]))
54 | result = LicenseKeyResult.KeyBlackListed;
55 |
56 | if (result != LicenseKeyResult.KeyBlackListed)
57 | {
58 | // At this point, the key is either valid or forged,
59 | // because a forged key can have a valid checksum.
60 | // We now test the "bytes" of the key to determine if it is
61 | // actually valid.
62 |
63 | // When building your release application, use conditional defines
64 | // or comment out most of the byte checks! This is the heart
65 | // of the partial key verification system. By not compiling in
66 | // each check, there is no way for someone to build a keygen that
67 | // will produce valid keys. If an invalid keygen is released, you
68 | // simply change which byte checks are compiled in, and any serial
69 | // number built with the fake keygen no longer works.
70 |
71 | // Note that the parameters used for PKV_GetKeyByte calls MUST
72 | // MATCH the values that PKV_MakeKey uses to make the key in the
73 | // first place!
74 |
75 | result = LicenseKeyResult.KeyPhoney;
76 |
77 | var seedParsed = int.TryParse(key.Substring(0, 8), NumberStyles.HexNumber, null, out var seed);
78 |
79 | if (!seedParsed) return result;
80 | // The using of conditional compilation for the key byte checks
81 | // means that we define the portions of the key that are checked
82 | // at runtime. The advantage of this is that if someone creates
83 | // a keygen using this source code, we can change the key bytes that
84 | // are checked, so subsequent releases will not work with keys generated by
85 | // the key generator.
86 |
87 | foreach (var keyByteSet in keyByteSetsToCheck)
88 | {
89 | var keySubstringStart = GetKeySubstringStart(keyByteSet.KeyByteNo);
90 |
91 | if (keySubstringStart - 1 > key.Length)
92 | throw new InvalidOperationException(
93 | "The KeyByte check position is out of range. You may have specified a check KeyByteNo that did not exist in the original key generation.");
94 |
95 | var keyBytes = key.Substring(keySubstringStart, 2);
96 | var b = GetKeyByte(seed, keyByteSet.KeyByteA, keyByteSet.KeyByteB, keyByteSet.KeyByteC);
97 |
98 | if (keyBytes != b.ToString("X2"))
99 | // If true, then it means the key is either good, or was made
100 | // with a keygen derived from "this" release.
101 |
102 | return result; // Return result in failed state
103 | }
104 |
105 | result = LicenseKeyResult.KeyGood;
106 | }
107 | }
108 |
109 | return result;
110 | }
111 |
112 | ///
113 | /// Short hand way of creating pattern 8, 10, 12, 14
114 | ///
115 | ///
116 | ///
117 | private int GetKeySubstringStart(int keyByteNo)
118 | {
119 | return keyByteNo * 2 + 6;
120 | }
121 |
122 | ///
123 | /// Indicate if the check sum portion of the key is valid
124 | ///
125 | ///
126 | ///
127 | ///
128 | public bool CheckKeyChecksum(string key, int totalKeyByteSets)
129 | {
130 | var result = false;
131 | var formattedKey = FormatKeyForCompare(key);
132 |
133 | if (formattedKey.Length != 8 + 4 + 2 * totalKeyByteSets) return result;
134 |
135 | var keyLessChecksumLength = formattedKey.Length - 4;
136 | var checkSum = formattedKey.Substring(keyLessChecksumLength, 4); // Last 4 chars are checksum
137 | var keyWithoutChecksum = formattedKey.Substring(0, keyLessChecksumLength);
138 | result = GetChecksum(keyWithoutChecksum) == checkSum;
139 | return result;
140 | }
141 |
142 | ////////////////////////////////////////////////////
143 | // Code below from here is duplicated across both
144 | // private and public projects / dlls
145 | ////////////////////////////////////////////////////
146 |
147 | ///
148 | /// Strip padding chars for comparison
149 | ///
150 | ///
151 | ///
152 | private static string FormatKeyForCompare(string key)
153 | {
154 | if (key == null) key = string.Empty;
155 |
156 | // Replace -, space etc, upper case
157 |
158 | return key.Trim().ToUpper().Replace("-", string.Empty).Replace(" ", string.Empty);
159 | }
160 |
161 | ///
162 | /// Given a seed and some input bytes, generate a single byte to return. This should
163 | /// be used with randomised data, that can be represented to retrieve the same key.
164 | ///
165 | ///
166 | ///
167 | ///
168 | ///
169 | ///
170 | private byte GetKeyByte(long seed, byte a, byte b, byte c)
171 | {
172 | var aTemp = a % 25;
173 | var bTemp = b % 3;
174 |
175 | long result;
176 |
177 | if (a % 2 == 0)
178 | result = ((seed >> aTemp) & 0xFF) ^ ((seed >> bTemp) | c);
179 | else
180 | result = ((seed >> aTemp) & 0xFF) ^ ((seed >> bTemp) & c);
181 |
182 | return (byte) result;
183 | }
184 |
185 | ///
186 | /// Generate a new checksum for a key
187 | ///
188 | ///
189 | ///
190 | private string GetChecksum(string str)
191 | {
192 | ushort left = 0x56;
193 | ushort right = 0xAF;
194 |
195 | if (str.Length > 0)
196 | // 0xFF hex for 255
197 |
198 | foreach (var t in str)
199 | {
200 | right = (ushort) (right + Convert.ToByte(t));
201 |
202 | if (right > 0xFF) right -= 0xFF;
203 |
204 | left += right;
205 |
206 | if (left > 0xFF) left -= 0xFF;
207 | }
208 |
209 | var sum = (ushort) ((left << 8) + right);
210 |
211 | return sum.ToString("X4"); // 4 char hex
212 | }
213 | }
214 | }
--------------------------------------------------------------------------------