├── Demo
├── KeyGen.csproj.user
├── app.config
├── System.Ini
│ ├── IInitializer.cs
│ ├── Win32ErrorMessageAttribute.cs
│ ├── AssemblyMessageAttribute.cs
│ ├── ResourceAttribute.cs
│ └── ModuleStringAttribute.cs
├── System.Windows.Forms
│ ├── IUserControl.cs
│ ├── DialogBox.cs
│ ├── TextControl.cs
│ ├── GenLicForm.resx
│ └── PasswordForm.cs
├── System.ComponentModel
│ ├── Win32FormatMessageFlags.cs
│ ├── ModuleMessageFormatterConverter.cs
│ ├── Win32MessageFormatterConverter.cs
│ ├── AssemblyMessageFormatter.cs
│ ├── ModuleMessageFormatter.cs
│ └── Win32MessageFormatter.cs
├── Properties
│ ├── TestAssemblyInfo.cs
│ └── KeyGenAssemblyInfo.cs
├── GenLic
│ └── Program.cs
├── Test.csproj
├── KeyGen.csproj
├── Test
│ └── Program.cs
├── System
│ └── Extensions.cs
├── System.Management.WMI
│ ├── ProcessorInfo.cs
│ └── NetworkAdapterConfigurationInfo.cs
└── System.Resourses
│ └── ResourceFile.cs
├── Source
├── Text
│ ├── PrintableEncoding.cs
│ ├── IPrintableEncoding.cs
│ ├── Base16Encoding.cs
│ ├── InternalBaseEncoding.cs
│ ├── BaseEncoding.cs
│ ├── Base10Encoding.cs
│ ├── Base32Encoding.cs
│ ├── Base64Encoding.cs
│ └── CustomEncoding.cs
├── Security
│ ├── Activation
│ │ ├── ActivationKeyConverter.cs
│ │ ├── ActivationKeyEncryptor.cs
│ │ ├── ActivationKeyDecryptor.cs
│ │ └── ActivationKeyBinaryParser.cs
│ └── Cryptography
│ │ ├── SipHashAlgorithm.cs
│ │ └── ARC4CryptoTransform.cs
└── IO
│ └── IniFile.cs
├── LICENSE
├── ActivationKey.sln
├── Properties
└── AssemblyInfo.cs
├── ActivationKey.csproj
└── .gitignore
/Demo/KeyGen.csproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/Demo/app.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Demo/System.Ini/IInitializer.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 |
3 | namespace System.Ini
4 | {
5 | public interface IInitializer
6 | {
7 | void ReadSettings(Assembly assembly = null);
8 |
9 | void WriteSettings(Assembly assembly = null);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Demo/System.Windows.Forms/IUserControl.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.ObjectModel;
2 |
3 | namespace System.Windows.Forms
4 | {
5 | internal interface IUserControl
6 | {
7 | string DisplayName {get; set;}
8 | bool Required {get; set;}
9 | bool Complete { get; }
10 |
11 | string Text {get; set;}
12 | event EventHandler TextChanged;
13 |
14 | bool Allert(ref Collection controlNames);
15 |
16 | void Light();
17 |
18 | void Reset();
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/Demo/System.ComponentModel/Win32FormatMessageFlags.cs:
--------------------------------------------------------------------------------
1 | namespace System.ComponentModel
2 | {
3 | [Serializable]
4 | [Flags]
5 | internal enum Win32FormatMessageFlags
6 | {
7 | FORMAT_MESSAGE_NONE = 0x0,
8 | FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x100,
9 | FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x2000,
10 | FORMAT_MESSAGE_FROM_HMODULE = 0x800,
11 | FORMAT_MESSAGE_FROM_STRING = 0x400,
12 | FORMAT_MESSAGE_FROM_SYSTEM = 0x1000,
13 | FORMAT_MESSAGE_IGNORE_INSERTS = 0x200,
14 | FORMAT_MESSAGE_MAX_WIDTH_MASK = 0xFF
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/Demo/Properties/TestAssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.InteropServices;
3 | [assembly: AssemblyTitle("Test")]
4 | [assembly: AssemblyDescription("")]
5 | [assembly: AssemblyConfiguration("")]
6 | [assembly: AssemblyCompany("")]
7 | [assembly: AssemblyProduct("Test")]
8 | [assembly: AssemblyCopyright("Copyright © 2024")]
9 | [assembly: AssemblyTrademark("")]
10 | [assembly: AssemblyCulture("")]
11 | [assembly: ComVisible(false)]
12 | [assembly: Guid("414850d2-031c-49eb-9ecc-098599e57f2f")]
13 | [assembly: AssemblyVersion("1.0.0.0")]
14 | [assembly: AssemblyFileVersion("1.0.0.0")]
15 |
--------------------------------------------------------------------------------
/Demo/Properties/KeyGenAssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.InteropServices;
3 |
4 | [assembly: AssemblyTitle("Activation Key Generator 5.0 Application")]
5 | [assembly: AssemblyDescription("Activation Key Generator 5.0 Application")]
6 | [assembly: AssemblyConfiguration("")]
7 | [assembly: AssemblyCompany("NG256")]
8 | [assembly: AssemblyProduct("NG256 Activation Key Generator")]
9 | [assembly: AssemblyCopyright("© NG256 2019-2024")]
10 | [assembly: AssemblyFileVersion("5.0.0.1")]
11 | [assembly: ComVisible(false)]
12 | [assembly: Guid("77bccba3-838a-4b6e-be95-9e4ec16c46d9")]
13 | [assembly: AssemblyVersion("5.0.0.1")]
14 |
--------------------------------------------------------------------------------
/Demo/System.Ini/Win32ErrorMessageAttribute.cs:
--------------------------------------------------------------------------------
1 | namespace System.Ini
2 | {
3 | [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
4 | public sealed class Win32ErrorMessageAttribute : Attribute
5 | {
6 | private static readonly Win32ErrorMessageAttribute _defaultAttribute = new Win32ErrorMessageAttribute(0u);
7 |
8 | public uint MessageId {get; set;}
9 | public string[] Arguments {get; set;}
10 |
11 | public override bool Match(object obj)
12 | {
13 | Win32ErrorMessageAttribute iniEntryAttribute;
14 | if ((iniEntryAttribute = obj as Win32ErrorMessageAttribute) != null)
15 | {
16 | return iniEntryAttribute.MessageId == MessageId;
17 | }
18 | return false;
19 | }
20 |
21 | public override bool IsDefaultAttribute()
22 | {
23 | return Match(_defaultAttribute);
24 | }
25 |
26 | public Win32ErrorMessageAttribute(uint msgCode, params string[] args)
27 | {
28 | MessageId = msgCode;
29 | Arguments = args;
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Demo/System.Ini/AssemblyMessageAttribute.cs:
--------------------------------------------------------------------------------
1 | namespace System.Ini
2 | {
3 | [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
4 | public sealed class AssemblyMessageAttribute : Attribute
5 | {
6 | private static readonly AssemblyMessageAttribute _defaultAttribute = new AssemblyMessageAttribute("UnknownError");
7 |
8 | public string MessageId {get; set;}
9 | public string[] Arguments {get; set;}
10 |
11 | public override bool Match(object obj)
12 | {
13 | AssemblyMessageAttribute iniEntryAttribute;
14 | if ((iniEntryAttribute = obj as AssemblyMessageAttribute) != null)
15 | {
16 | return iniEntryAttribute.MessageId == MessageId;
17 | }
18 | return false;
19 | }
20 |
21 | public override bool IsDefaultAttribute()
22 | {
23 | return Match(_defaultAttribute);
24 | }
25 |
26 | public AssemblyMessageAttribute(string msgCode, params string[] args)
27 | {
28 | MessageId = msgCode;
29 | Arguments = args;
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Demo/System.Ini/ResourceAttribute.cs:
--------------------------------------------------------------------------------
1 | namespace System.Ini
2 | {
3 | [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
4 | public sealed class ResourceAttribute : Attribute
5 | {
6 | private static readonly ResourceAttribute _defaultAttribute = new ResourceAttribute();
7 |
8 | public string Name
9 | {
10 | get;
11 | set;
12 | }
13 |
14 | public object DefaultValue
15 | {
16 | get;
17 | set;
18 | }
19 |
20 | public override bool Match(object obj)
21 | {
22 | ResourceAttribute iniEntryAttribute;
23 | if ((iniEntryAttribute = obj as ResourceAttribute) != null)
24 | {
25 | return iniEntryAttribute.Name == Name;
26 | }
27 | return false;
28 | }
29 |
30 | public override bool IsDefaultAttribute()
31 | {
32 | return Match(_defaultAttribute);
33 | }
34 |
35 | public ResourceAttribute(string name = null, object defaultValue = null)
36 | {
37 | Name = name;
38 | DefaultValue = defaultValue;
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/Demo/System.Ini/ModuleStringAttribute.cs:
--------------------------------------------------------------------------------
1 | namespace System.Ini
2 | {
3 | [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
4 | public sealed class ModuleStringAttribute : Attribute
5 | {
6 | private static readonly ModuleStringAttribute _defaultAttribute = new ModuleStringAttribute("user32.dll", 0u);
7 |
8 | public string Name {get; set;}
9 | public uint MessageId {get; set;}
10 | public string[] Arguments {get; set;}
11 |
12 | public override bool Match(object obj)
13 | {
14 | ModuleStringAttribute iniEntryAttribute;
15 | if ((iniEntryAttribute = obj as ModuleStringAttribute) != null && iniEntryAttribute.MessageId == MessageId)
16 | {
17 | return iniEntryAttribute.Name == Name;
18 | }
19 | return false;
20 | }
21 |
22 | public override bool IsDefaultAttribute()
23 | {
24 | return Match(_defaultAttribute);
25 | }
26 |
27 | public ModuleStringAttribute(string name, uint msgCode, params string[] args)
28 | {
29 | Name = name;
30 | MessageId = msgCode;
31 | Arguments = args;
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/Source/Text/PrintableEncoding.cs:
--------------------------------------------------------------------------------
1 | /***************************************************************
2 |
3 | • File: PrintableEncoding.cs
4 |
5 | • Description.
6 |
7 | Represents an enum of encoding types. Each enumeration
8 | element represents a specific encoding type.
9 |
10 | ***************************************************************/
11 |
12 | namespace System.Text
13 | {
14 | ///
15 | /// Defines printable encoding types.
16 | ///
17 | public enum PrintableEncoding
18 | {
19 | ///
20 | /// Represents a 32 character encoding.
21 | ///
22 | Base32,
23 |
24 | ///
25 | /// Represents a 64 character encoding.
26 | ///
27 | Base64,
28 |
29 | ///
30 | /// Represents the decimal number system.
31 | ///
32 | Decimal,
33 |
34 | ///
35 | /// Represents the hexadecimal encoding.
36 | ///
37 | Hexadecimal
38 | }
39 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021-2024 ng256
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 |
--------------------------------------------------------------------------------
/Demo/GenLic/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.ComponentModel;
3 | using System.IO;
4 | using System.Resourses;
5 | using System.Windows.Forms;
6 |
7 | namespace GenLic5
8 | {
9 | internal static class Program
10 | {
11 | [STAThread]
12 | private static void Main()
13 | {
14 | Application.EnableVisualStyles();
15 | Application.SetCompatibleTextRenderingDefault(false);
16 | string dbPath = Path.GetFileNameWithoutExtension(PathsInfo.AppFileName) + ".db";
17 | using (ResourceFile resourceFile = new ResourceFile(dbPath))
18 | {
19 | resourceFile.ReadSettings();
20 | }
21 | using (ModuleMessageFormatter user32 = new ModuleMessageFormatter("user32.dll"))
22 | {
23 | user32.ReadSettings();
24 | }
25 | Form mainForm;
26 | using (PasswordForm pass = new PasswordForm())
27 | {
28 | if (pass.ShowDialog() != DialogResult.OK)
29 | {
30 | return;
31 | }
32 | mainForm = new GenLicForm(pass.SecurePassword);
33 | }
34 | Application.Run(mainForm);
35 | using (ResourceFile dbFile = new ResourceFile(dbPath))
36 | {
37 | dbFile.WriteSettings();
38 | }
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/Source/Text/IPrintableEncoding.cs:
--------------------------------------------------------------------------------
1 | /***************************************************************
2 |
3 | • File: IPrintableEncoding.cs
4 |
5 | • Description.
6 |
7 | IBaseEncoding provides methods for converting binary data to
8 | text representation.
9 |
10 | The GetString and GetBytes methods allow you to encode and
11 | decode data, which can be useful when working with binary
12 | data that needs to be represented in a human-readable format
13 | or transmitted over pipes that only support text data.
14 |
15 | ***************************************************************/
16 |
17 | namespace System.Text
18 | {
19 | ///
20 | /// Represents a set of methods that allow you to convert binary data into a text representation.
21 | /// This can be useful for displaying binary data in a human-readable format,
22 | /// or for transferring binary data over pipes that only support text data.
23 | ///
24 | public interface IPrintableEncoding : ICloneable
25 | {
26 | /// Decodes all the bytes in the specified byte array into a string.
27 | /// The byte array containing the sequence of bytes to decode.
28 | /// A string that contains the results of decoding the specified sequence of bytes.
29 | string GetString(byte[] bytes);
30 |
31 | /// Encodes all the characters in the specified string into a sequence of bytes.
32 | /// The string containing the characters to encode.
33 | /// A byte array containing the results of encoding the specified set of characters.
34 | byte[] GetBytes(string s);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/Demo/System.ComponentModel/ModuleMessageFormatterConverter.cs:
--------------------------------------------------------------------------------
1 | using System.Globalization;
2 |
3 | namespace System.ComponentModel
4 | {
5 | internal class ModuleMessageFormatterConverter : TypeConverter
6 | {
7 | private static TypeDescriptionProvider _attributeProvider;
8 |
9 | static ModuleMessageFormatterConverter()
10 | {
11 | _attributeProvider = null;
12 | }
13 |
14 | public static void Register()
15 | {
16 | if (_attributeProvider == null)
17 | {
18 | _attributeProvider = TypeDescriptor.AddAttributes(typeof(ModuleMessageFormatter), new TypeConverterAttribute(typeof(ModuleMessageFormatterConverter)));
19 | }
20 | }
21 |
22 | public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
23 | {
24 | if (!(sourceType == typeof(string)) && !(sourceType == typeof(int)))
25 | {
26 | return base.CanConvertFrom(context, sourceType);
27 | }
28 | return true;
29 | }
30 |
31 | public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
32 | {
33 | if (!(destinationType == typeof(string)) && !(destinationType == typeof(int)))
34 | {
35 | return base.CanConvertTo(context, destinationType);
36 | }
37 | return true;
38 | }
39 |
40 | public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
41 | {
42 | string fileNmae;
43 | if ((fileNmae = value as string) != null)
44 | {
45 | return new ModuleMessageFormatter(fileNmae);
46 | }
47 | return base.ConvertFrom(context, culture, value);
48 | }
49 |
50 | public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
51 | {
52 | ModuleMessageFormatter formatter;
53 | if ((formatter = value as ModuleMessageFormatter) != null && destinationType == typeof(string))
54 | {
55 | return formatter.FileName;
56 | }
57 | return base.ConvertTo(context, culture, value, destinationType);
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/Demo/Test.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {414850D2-031C-49EB-9ECC-098599E57F2F}
8 | Exe
9 | Test
10 | Test
11 | v4.0
12 | 512
13 | true
14 |
15 |
16 | AnyCPU
17 | true
18 | full
19 | false
20 | bin\Debug\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 |
25 |
26 | AnyCPU
27 | none
28 | true
29 | bin\Release\
30 | TRACE
31 | prompt
32 | 4
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 | {a25af6b1-f1a6-4a0e-989e-ca61c126d865}
46 | ActivationKey
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 | copy "$(TargetPath)" "$(SolutionDir)Bin\"
57 |
58 |
--------------------------------------------------------------------------------
/Demo/System.Windows.Forms/DialogBox.cs:
--------------------------------------------------------------------------------
1 | using System.ComponentModel;
2 | using System.Ini;
3 |
4 | namespace System.Windows.Forms
5 | {
6 | public class DialogBox : Form
7 | {
8 | private IContainer components;
9 |
10 | protected Button btnCancel;
11 |
12 | protected Button btnOk;
13 |
14 | [ModuleString("user32.dll", 800u)]
15 | private static string OKText { get; set; } = "&OK";
16 |
17 |
18 | [ModuleString("user32.dll", 801u)]
19 | private static string CancelText { get; set; } = "&Cancel";
20 |
21 |
22 | protected override void Dispose(bool disposing)
23 | {
24 | if (disposing && components != null)
25 | {
26 | components.Dispose();
27 | }
28 | base.Dispose(disposing);
29 | }
30 |
31 | private void InitializeComponent()
32 | {
33 | btnCancel = new System.Windows.Forms.Button();
34 | btnOk = new System.Windows.Forms.Button();
35 | SuspendLayout();
36 | btnCancel.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
37 | btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
38 | btnCancel.Location = new System.Drawing.Point(376, 142);
39 | btnCancel.Name = "btnCancel";
40 | btnCancel.Size = new System.Drawing.Size(75, 23);
41 | btnCancel.TabIndex = 102;
42 | btnCancel.Text = "&Cancel";
43 | btnCancel.UseVisualStyleBackColor = true;
44 | btnOk.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
45 | btnOk.DialogResult = System.Windows.Forms.DialogResult.OK;
46 | btnOk.Location = new System.Drawing.Point(295, 142);
47 | btnOk.Name = "btnOk";
48 | btnOk.Size = new System.Drawing.Size(75, 23);
49 | btnOk.TabIndex = 101;
50 | btnOk.Text = "&OK";
51 | btnOk.UseVisualStyleBackColor = true;
52 | base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
53 | base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
54 | base.ClientSize = new System.Drawing.Size(463, 177);
55 | base.Controls.Add(btnCancel);
56 | base.Controls.Add(btnOk);
57 | base.Name = "DialogBox";
58 | Text = "";
59 | ResumeLayout(false);
60 | }
61 |
62 | public DialogBox()
63 | {
64 | InitializeComponent();
65 | btnOk.Text = OKText;
66 | btnOk.Click += Ok_Click;
67 | btnCancel.Text = CancelText;
68 | btnCancel.Click += Cancel_Click;
69 | }
70 |
71 | private void Ok_Click(object sender, EventArgs e)
72 | {
73 | base.DialogResult = DialogResult.OK;
74 | Close();
75 | }
76 |
77 | private void Cancel_Click(object sender, EventArgs e)
78 | {
79 | base.DialogResult = DialogResult.Cancel;
80 | Close();
81 | }
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/Demo/System.ComponentModel/Win32MessageFormatterConverter.cs:
--------------------------------------------------------------------------------
1 | using System.Globalization;
2 |
3 | namespace System.ComponentModel
4 | {
5 | internal class Win32MessageFormatterConverter : TypeConverter
6 | {
7 | private static TypeDescriptionProvider _attributeProvider;
8 |
9 | static Win32MessageFormatterConverter()
10 | {
11 | _attributeProvider = null;
12 | }
13 |
14 | public static void Register()
15 | {
16 | if (_attributeProvider == null)
17 | {
18 | _attributeProvider = TypeDescriptor.AddAttributes(typeof(Win32MessageFormatter), new TypeConverterAttribute(typeof(Win32MessageFormatterConverter)));
19 | }
20 | }
21 |
22 | public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
23 | {
24 | if (!(sourceType == typeof(string)) && !(sourceType == typeof(int)))
25 | {
26 | return base.CanConvertFrom(context, sourceType);
27 | }
28 | return true;
29 | }
30 |
31 | public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
32 | {
33 | if (!(destinationType == typeof(string)) && !(destinationType == typeof(int)))
34 | {
35 | return base.CanConvertTo(context, destinationType);
36 | }
37 | return true;
38 | }
39 |
40 | public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
41 | {
42 | object obj;
43 | if ((obj = value) is int)
44 | {
45 | int lcid = (int)obj;
46 | return new Win32MessageFormatter(lcid);
47 | }
48 | CultureInfo c;
49 | if ((c = value as CultureInfo) != null)
50 | {
51 | return new Win32MessageFormatter(c);
52 | }
53 | string name;
54 | if ((name = value as string) != null)
55 | {
56 | if (int.TryParse(name, NumberStyles.Integer, culture, out var lcid))
57 | {
58 | return new Win32MessageFormatter(lcid);
59 | }
60 | return new Win32MessageFormatter(name);
61 | }
62 | return base.ConvertFrom(context, culture, value);
63 | }
64 |
65 | public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
66 | {
67 | Win32MessageFormatter formatter;
68 | if ((formatter = value as Win32MessageFormatter) != null)
69 | {
70 | if (destinationType == typeof(int))
71 | {
72 | return formatter.LCID;
73 | }
74 | if (destinationType == typeof(string))
75 | {
76 | return new CultureInfo(formatter.LCID).Name;
77 | }
78 | if (destinationType == typeof(CultureInfo))
79 | {
80 | return new CultureInfo(formatter.LCID);
81 | }
82 | }
83 | return base.ConvertTo(context, culture, value, destinationType);
84 | }
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/Demo/System.Windows.Forms/TextControl.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.ObjectModel;
2 | using System.Drawing;
3 |
4 | namespace System.Windows.Forms
5 | {
6 | internal class TextControl : TextBox, IUserControl
7 | {
8 | private static readonly ToolTip tool = new ToolTip();
9 |
10 | public sealed override string Text
11 | {
12 | get
13 | {
14 | return base.Text;
15 | }
16 | set
17 | {
18 | base.Text = value;
19 | }
20 | }
21 |
22 | public string DisplayName {get; set;}
23 | public string HelpText {get; set;}
24 | public bool Required {get; set;}
25 | public int MinLength {get; set;}
26 | public bool Complete
27 | {
28 | get
29 | {
30 | if (!Required)
31 | {
32 | return true;
33 | }
34 | if (MinLength <= 0)
35 | {
36 | return Text.Length > 0;
37 | }
38 | return Text.Length >= MinLength;
39 | }
40 | }
41 |
42 | public TextControl()
43 | {
44 | base.TextChanged += _text_TextChanged;
45 | base.LostFocus += TextControl_LostFocus;
46 | }
47 |
48 | public TextControl(bool required)
49 | : this()
50 | {
51 | Required = required;
52 | }
53 |
54 | public TextControl(string text)
55 | : this(false)
56 | {
57 | Text = text;
58 | }
59 |
60 | public TextControl(string text, bool required)
61 | : this(required)
62 | {
63 | Text = text;
64 | }
65 |
66 | public static implicit operator string(TextControl value)
67 | {
68 | return value.Text;
69 | }
70 |
71 | internal void _text_TextChanged(object sender, EventArgs e)
72 | {
73 | BackColor = SystemColors.Window;
74 | }
75 |
76 | private void TextControl_LostFocus(object sender, EventArgs e)
77 | {
78 | Light();
79 | }
80 |
81 | public void Parse(string value)
82 | {
83 | Text = value;
84 | }
85 |
86 | public bool Allert(ref Collection controlNames)
87 | {
88 | if (controlNames == null)
89 | {
90 | controlNames = new Collection();
91 | }
92 | if (Complete)
93 | {
94 | return true;
95 | }
96 | controlNames.Add(this);
97 | return false;
98 | }
99 |
100 | public void Light()
101 | {
102 | BackColor = (Complete ? SystemColors.Window : Color.LightCoral);
103 | if (!Complete && !base.DesignMode)
104 | {
105 | tool.Show(HelpText, this, 3000);
106 | }
107 | }
108 |
109 | public void Reset()
110 | {
111 | Clear();
112 | }
113 |
114 | /*void IUserControl.TextChanged(EventHandler value)
115 | {
116 | base.TextChanged += value;
117 | }
118 |
119 | void IUserControl.TextChanged(EventHandler value)
120 | {
121 | base.TextChanged -= value;
122 | }*/
123 | }
124 | }
125 |
--------------------------------------------------------------------------------
/Source/Security/Activation/ActivationKeyConverter.cs:
--------------------------------------------------------------------------------
1 | /***************************************************************
2 |
3 | • File: ActivationKeyConverter.cs
4 |
5 | • Description.
6 |
7 | ActivationKeyConverter is a converter for the ActivationKey
8 | data type. It allows you to convert objects of type
9 | ActivationKey to and from other datatypes.
10 |
11 | ***************************************************************/
12 |
13 | using System.ComponentModel;
14 | using System.Globalization;
15 |
16 | namespace System.Security.Activation
17 | {
18 | ///
19 | /// Converts between other types.
20 | ///
21 | public sealed class ActivationKeyConverter : TypeConverter
22 | {
23 | ///
24 | public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
25 | {
26 | if (sourceType == typeof(string) || sourceType == typeof(byte[]))
27 | return true;
28 |
29 | return base.CanConvertFrom(context, sourceType);
30 | }
31 |
32 | ///
33 | public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
34 | {
35 | if (destinationType == typeof(string) || destinationType == typeof(byte[]))
36 | return true;
37 |
38 | return base.CanConvertTo(context, destinationType);
39 | }
40 |
41 | ///
42 | public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
43 | {
44 | switch (value)
45 | {
46 | case string str:
47 | return new ActivationKey(str);
48 | case byte[] bytes:
49 | return new ActivationKey(bytes);
50 | default:
51 | return base.ConvertFrom(context, culture, value);
52 | }
53 | }
54 |
55 | ///
56 | public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
57 | {
58 | switch (value)
59 | {
60 | case ActivationKey activationKey when destinationType == typeof(string):
61 | return activationKey.ToString();
62 | case ActivationKey activationKey when destinationType == typeof(byte[]):
63 | return activationKey.ToBinary();
64 | default:
65 | return base.ConvertTo(context, culture, value, destinationType);
66 | }
67 | }
68 | }
69 | }
--------------------------------------------------------------------------------
/ActivationKey.sln:
--------------------------------------------------------------------------------
1 | Microsoft Visual Studio Solution File, Format Version 12.00
2 | # Visual Studio Version 17
3 | VisualStudioVersion = 17.6.33712.159
4 | MinimumVisualStudioVersion = 10.0.40219.1
5 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ActivationKey", "ActivationKey.csproj", "{A25AF6B1-F1A6-4A0E-989E-CA61C126D865}"
6 | EndProject
7 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Demo\Test.csproj", "{414850D2-031C-49EB-9ECC-098599E57F2F}"
8 | EndProject
9 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KeyGen", "Demo\KeyGen.csproj", "{D9034D82-3C96-4F9E-95ED-0691D6FF3ABA}"
10 | EndProject
11 | Global
12 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
13 | Debug|Any CPU = Debug|Any CPU
14 | Debug|x86 = Debug|x86
15 | Release|Any CPU = Release|Any CPU
16 | Release|x86 = Release|x86
17 | EndGlobalSection
18 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
19 | {A25AF6B1-F1A6-4A0E-989E-CA61C126D865}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20 | {A25AF6B1-F1A6-4A0E-989E-CA61C126D865}.Debug|Any CPU.Build.0 = Debug|Any CPU
21 | {A25AF6B1-F1A6-4A0E-989E-CA61C126D865}.Debug|x86.ActiveCfg = Debug|Any CPU
22 | {A25AF6B1-F1A6-4A0E-989E-CA61C126D865}.Debug|x86.Build.0 = Debug|Any CPU
23 | {A25AF6B1-F1A6-4A0E-989E-CA61C126D865}.Release|Any CPU.ActiveCfg = Release|Any CPU
24 | {A25AF6B1-F1A6-4A0E-989E-CA61C126D865}.Release|Any CPU.Build.0 = Release|Any CPU
25 | {A25AF6B1-F1A6-4A0E-989E-CA61C126D865}.Release|x86.ActiveCfg = Release|Any CPU
26 | {A25AF6B1-F1A6-4A0E-989E-CA61C126D865}.Release|x86.Build.0 = Release|Any CPU
27 | {414850D2-031C-49EB-9ECC-098599E57F2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28 | {414850D2-031C-49EB-9ECC-098599E57F2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
29 | {414850D2-031C-49EB-9ECC-098599E57F2F}.Debug|x86.ActiveCfg = Debug|Any CPU
30 | {414850D2-031C-49EB-9ECC-098599E57F2F}.Debug|x86.Build.0 = Debug|Any CPU
31 | {414850D2-031C-49EB-9ECC-098599E57F2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
32 | {414850D2-031C-49EB-9ECC-098599E57F2F}.Release|Any CPU.Build.0 = Release|Any CPU
33 | {414850D2-031C-49EB-9ECC-098599E57F2F}.Release|x86.ActiveCfg = Release|Any CPU
34 | {414850D2-031C-49EB-9ECC-098599E57F2F}.Release|x86.Build.0 = Release|Any CPU
35 | {D9034D82-3C96-4F9E-95ED-0691D6FF3ABA}.Debug|Any CPU.ActiveCfg = Debug|x86
36 | {D9034D82-3C96-4F9E-95ED-0691D6FF3ABA}.Debug|Any CPU.Build.0 = Debug|x86
37 | {D9034D82-3C96-4F9E-95ED-0691D6FF3ABA}.Debug|x86.ActiveCfg = Debug|x86
38 | {D9034D82-3C96-4F9E-95ED-0691D6FF3ABA}.Debug|x86.Build.0 = Debug|x86
39 | {D9034D82-3C96-4F9E-95ED-0691D6FF3ABA}.Release|Any CPU.ActiveCfg = Release|x86
40 | {D9034D82-3C96-4F9E-95ED-0691D6FF3ABA}.Release|Any CPU.Build.0 = Release|x86
41 | {D9034D82-3C96-4F9E-95ED-0691D6FF3ABA}.Release|x86.ActiveCfg = Release|x86
42 | {D9034D82-3C96-4F9E-95ED-0691D6FF3ABA}.Release|x86.Build.0 = Release|x86
43 | EndGlobalSection
44 | GlobalSection(SolutionProperties) = preSolution
45 | HideSolutionNode = FALSE
46 | EndGlobalSection
47 | GlobalSection(ExtensibilityGlobals) = postSolution
48 | SolutionGuid = {5E058340-8179-4089-9FF6-C632081A19AE}
49 | EndGlobalSection
50 | EndGlobal
51 |
--------------------------------------------------------------------------------
/Source/Text/Base16Encoding.cs:
--------------------------------------------------------------------------------
1 | /***************************************************************
2 |
3 | • File: Base16Encoding.cs
4 |
5 | • Description.
6 |
7 | Base16Encoding is designed to work with hexadecimal data and
8 | implements methods for encoding and decoding data.
9 |
10 | ***************************************************************/
11 |
12 | using static System.InternalTools;
13 |
14 | namespace System.Text
15 | {
16 | internal sealed class Base16Encoding : InternalBaseEncoding
17 | {
18 | public override string EncodingName => "base-16";
19 |
20 | public Base16Encoding()
21 | : base(0)
22 | {
23 | }
24 |
25 | public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
26 | {
27 | Validate(chars, charIndex, charCount, bytes, byteIndex);
28 | charCount = GetCharCount(chars, charIndex, charCount, bytes, byteIndex);
29 | int startByteIndex = byteIndex;
30 | int endCharIndex = charIndex + charCount;
31 | while (charIndex < endCharIndex)
32 | {
33 | byte value = (byte)((GetValue(chars[charIndex++]) << 4) + GetValue(chars[charIndex++]));
34 | bytes[byteIndex++] = value;
35 | }
36 |
37 | return byteIndex - startByteIndex;
38 | }
39 |
40 | public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
41 | {
42 | Validate(bytes, byteIndex, byteCount, chars, charIndex);
43 | byteCount = GetByteCount(bytes, byteIndex, byteCount, chars, charIndex);
44 | int startCharIndex = charIndex;
45 | int endByteIndex = byteIndex + byteCount;
46 | while (byteIndex < endByteIndex)
47 | {
48 | byte value = bytes[byteIndex++];
49 | chars[charIndex++] = GetDigit(value >> 4);
50 | chars[charIndex++] = GetDigit(value & 0xF);
51 | }
52 | return charIndex - startCharIndex;
53 | }
54 |
55 | public override int GetMaxByteCount(int charCount)
56 | {
57 | return charCount >> 1;
58 | }
59 |
60 | public override int GetMaxCharCount(int byteCount)
61 | {
62 | return byteCount << 1;
63 | }
64 |
65 | private static int GetValue(char digit)
66 | {
67 | if (digit > 0x2F && digit < 0x3A)
68 | return digit - 48;
69 | if (digit > 0x40 && digit < 0x47)
70 | return digit - 55;
71 | if (digit > 0x60 && digit < 0x67)
72 | return digit - 87;
73 | throw new ArgumentOutOfRangeException(nameof(digit), digit, GetResourceString("Format_BadBase"));
74 | }
75 |
76 | private static char GetDigit(int value)
77 | {
78 | return value >= 0xA ? (char)(value + 0x37) : (char)(value + 0x30);
79 | }
80 |
81 | public override object Clone()
82 | {
83 | return new Base16Encoding();
84 | }
85 |
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | /***********************************************************
2 | Activation Key v. 2.1
3 |
4 | The MIT License (MIT)
5 | Copyright: © NG256 2021-2024.
6 |
7 | Permission is hereby granted, free of charge, to any person
8 | obtaining a copy of this software and associated
9 | documentation files (the "Software"), to deal in the
10 | Software without restriction, including without limitation
11 | the rights to use, copy, modify, merge, publish, distribute,
12 | sublicense, and/or sell copies of the Software, and to
13 | permit persons to whom the Software is furnished to do so,
14 | subject to the following conditions:
15 |
16 | The above copyright notice and this permission notice shall
17 | be included in all copies or substantial portions of the
18 | Software.
19 |
20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
21 | KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
22 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
23 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
24 | OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
26 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 | ************************************************************/
29 |
30 | #if COMVISIBLE
31 | using System.EnterpriseServices;
32 | #endif
33 | #if DEBUG
34 | using System.Runtime.CompilerServices;
35 | #endif
36 | using System.Reflection;
37 | using System.Runtime.InteropServices;
38 |
39 | // General information about this assembly is provided by the following set
40 | // attributes. Change the values of these attributes to change the information,
41 | // related to the assembly.
42 | [assembly: AssemblyTitle("Activation Key Library 2.1")] // Assembly name.
43 | [assembly: AssemblyDescription("Activation Key 2.1 Library")] // Assembly description.
44 | [assembly: AssemblyCompany("NG256")] // Developer.
45 | [assembly: AssemblyProduct("NG256 Activation Key")] // Product name.
46 | [assembly: AssemblyCopyright("© NG256 2021-2024")] // Copyright.
47 | [assembly: AssemblyTrademark("NG256® Activation Key®")] // Trademark.
48 | [assembly: AssemblyCulture("")]
49 | [assembly: AssemblyVersion("2.1.2412.0003")]
50 | [assembly: AssemblyFileVersion("2.1.2412.0003")]
51 | #if DEBUG
52 | [assembly: InternalsVisibleTo("Test")]
53 | [assembly: AssemblyConfiguration("Debug")]
54 | #else
55 | [assembly: AssemblyConfiguration("Release")]
56 | #endif
57 |
58 | // Setting ComVisible to False makes the types in this assembly invisible
59 | // for COM components. If you need to refer to the type in this assembly via COM,
60 | // set the ComVisible attribute to TRUE for this type.
61 | #if COMVISIBLE
62 | [assembly: ComVisible(true)]
63 | [assembly: ApplicationName("Activation Key")] // COM application name.
64 | [assembly: ApplicationID("8174d929-d3d4-4aa6-8197-56c589912c27")]
65 | #else
66 | [assembly: ComVisible(false)]
67 | #endif
68 | // The following GUID serves to identify the type library if this project will be visible to COM
69 | [assembly: Guid("138e9ccd-369e-4d83-ac84-3fccb2f7027a")]
70 |
71 |
--------------------------------------------------------------------------------
/ActivationKey.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | AnyCPU
6 | {A25AF6B1-F1A6-4A0E-989E-CA61C126D865}
7 | Library
8 | ActivationKey
9 | v4.0
10 |
11 | 2.0.2405.8
12 | 512
13 | System
14 |
15 |
16 | AnyCPU
17 | true
18 | full
19 | false
20 | bin\Debug\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 | true
25 |
26 |
27 | AnyCPU
28 | pdbonly
29 | true
30 | bin\Release\
31 | TRACE
32 | prompt
33 | 4
34 | true
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/Source/Text/InternalBaseEncoding.cs:
--------------------------------------------------------------------------------
1 | /***************************************************************
2 |
3 | • File: InternalBaseEncoding.cs
4 |
5 | • Description.
6 |
7 | It is an abstract class InternalBaseEncoding, which is a
8 | descendant of the Encoding class and implements the
9 | IBaseEncoding interface for creating various encodings.
10 |
11 | ***************************************************************/
12 |
13 | namespace System.Text
14 | {
15 | internal abstract class InternalBaseEncoding : Encoding, IPrintableEncoding
16 | {
17 | protected InternalBaseEncoding(int codePage)
18 | : base(codePage)
19 | {
20 | }
21 |
22 | protected virtual void Validate(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
23 | {
24 | if (chars == null)
25 | throw new ArgumentNullException(nameof (chars));
26 | if (charIndex < 0)
27 | throw new ArgumentOutOfRangeException(nameof (charIndex), charIndex, InternalTools.GetResourceString("ArgumentOutOfRange_StartIndex"));
28 | if (charCount < 0)
29 | throw new ArgumentOutOfRangeException(nameof (charCount), charCount, InternalTools.GetResourceString("ArgumentOutOfRange_NegativeCount"));
30 | if (bytes == null)
31 | throw new ArgumentNullException(nameof (bytes));
32 | if (byteIndex < 0)
33 | throw new ArgumentOutOfRangeException(nameof (byteIndex), byteIndex, InternalTools.GetResourceString("ArgumentOutOfRange_StartIndex"));
34 | }
35 |
36 | protected virtual void Validate(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
37 | {
38 | if (bytes == null)
39 | throw new ArgumentNullException(nameof (bytes));
40 | if (byteCount < 0)
41 | throw new ArgumentOutOfRangeException(nameof (byteCount), byteCount, InternalTools.GetResourceString("ArgumentOutOfRange_NegativeCount"));
42 | if (byteIndex < 0)
43 | throw new ArgumentOutOfRangeException(nameof (byteIndex), byteIndex, InternalTools.GetResourceString("ArgumentOutOfRange_StartIndex"));
44 | if (chars == null)
45 | throw new ArgumentNullException(nameof (chars));
46 | if (charIndex < 0)
47 | throw new ArgumentOutOfRangeException(nameof (charIndex), charIndex, InternalTools.GetResourceString("ArgumentOutOfRange_StartIndex"));
48 | }
49 |
50 | public override int GetByteCount(char[] chars, int index, int count)
51 | {
52 | return GetMaxByteCount(chars.GetMaxCount(index, count));
53 | }
54 |
55 | public override int GetCharCount(byte[] bytes, int index, int count)
56 | {
57 | return GetMaxCharCount(bytes.GetMaxCount(index, count));
58 | }
59 |
60 | protected virtual int GetCharCount(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
61 | {
62 | int maxCharCount = GetMaxCharCount(bytes.GetMaxCount(byteIndex));
63 | return Math.Min(chars.GetMaxCount(charIndex, charCount), maxCharCount);
64 | }
65 |
66 | protected virtual int GetByteCount(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
67 | {
68 | int maxByteCount = GetMaxByteCount(chars.GetMaxCount(charIndex));
69 | byteCount = Math.Min(bytes.GetMaxCount(byteIndex, byteCount), maxByteCount);
70 | return byteCount;
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/Source/Text/BaseEncoding.cs:
--------------------------------------------------------------------------------
1 | /***************************************************************
2 |
3 | • File: IniFile.cs
4 |
5 | • Description.
6 |
7 | BaseEncoding provides a convenient way to create and use
8 | various encodings that implement the IBaseEncoding
9 | interface, which can be useful in a variety of scenarios.
10 |
11 | The BaseEncoding class implements the following encodings:
12 |
13 | - Base16Encoding - Returns the hexadecimal encoding.
14 | - Base10Encoding - Returns the decimal encoding.
15 | - Base32Encoding - Returns the 32-digit encoding.
16 | - Base64Encoding - Returns the 64-character encoding.
17 |
18 | Also in the BaseEncoding class there is a GetEncoding
19 | method, which takes an alphabet string as a parameter and
20 | creates an instance of a class that implements the
21 | IBaseEncoding interface. This class is based on a passed
22 | alphabet string.
23 |
24 | ***************************************************************/
25 |
26 | namespace System.Text
27 | {
28 | ///
29 | /// The BaseEncoding class provides static methods for creating and obtaining instances of classes that implement the IBaseEncoding interface.
30 | ///
31 | public static class BaseEncoding
32 | {
33 | ///
34 | /// Returns a hexadecimal based encoding that implements the interface.
35 | ///
36 | public static IBaseEncoding HexadecimalEncoding => (IBaseEncoding) new Base16Encoding();
37 |
38 | ///
39 | /// Returns a decimal encoding that implements the interface.
40 | ///
41 | public static IBaseEncoding DecimalEncoding => GetEncoding("0123456789");
42 |
43 | ///
44 | /// Returns a 32-digit based encoding that implements the interface.
45 | ///
46 | public static IBaseEncoding Base32Encoding => (IBaseEncoding) new Base32Encoding();
47 |
48 | ///
49 | /// Returns a 64-digit based encoding that implements the interface.
50 | ///
51 | public static IBaseEncoding Base64Encoding => (IBaseEncoding) new Base64Encoding();
52 |
53 | ///
54 | /// Creates an instance of the custom encoding class, which implements the interface,
55 | /// based on the passed alphabet string.
56 | ///
57 | /// The alphabet string on the basis of which encoding will be created.
58 | /// An instance of a class that implements the interface.
59 | ///
60 | /// The parameter is null.
61 | ///
62 | ///
63 | /// The parameter was either an empty string, contained duplicates, or contained only spaces.
64 | ///
65 | public static IBaseEncoding GetEncoding(string alphabet) => (IBaseEncoding) new CustomEncoding(alphabet);
66 |
67 | ///
68 | /// Creates an instance of encoding based on the passed .
69 | ///
70 | /// The on which the encoding will be created.
71 | /// An instance of a class that implements the interface.
72 | ///
73 | /// Thrown when the parameter does not match any of the listed values.
74 | ///
75 | public static IBaseEncoding GetEncoding(BaseEncodingType type)
76 | {
77 | switch (type)
78 | {
79 | case BaseEncodingType.Base10:
80 | return Base10Encoding;
81 | case BaseEncodingType.Base16:
82 | return Base16Encoding;
83 | case BaseEncodingType.Base32:
84 | return Base32Encoding;
85 | case BaseEncodingType.Base64:
86 | return Base64Encoding;
87 | default:
88 | throw new ArgumentOutOfRangeException(nameof (type), (object) type, InternalTools.GetResourceString("Arg_EnumIllegalVal", (object) type));
89 | }
90 | }
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/Source/Text/Base10Encoding.cs:
--------------------------------------------------------------------------------
1 | /***************************************************************
2 |
3 | • File: Base10Encoding.cs
4 |
5 | • Description.
6 |
7 | Base10Encoding is designed to work with decimal (base-10) data and
8 | implements methods for encoding and decoding data.
9 |
10 | ***************************************************************/
11 |
12 | using static System.InternalTools;
13 |
14 | namespace System.Text
15 | {
16 | internal sealed class Base10Encoding : InternalBaseEncoding
17 | {
18 | public override string EncodingName => "base-10";
19 |
20 | public Base10Encoding()
21 | : base(0)
22 | {
23 | }
24 |
25 | public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
26 | {
27 | Validate(chars, charIndex, charCount, bytes, byteIndex);
28 | charCount = GetCharCount(chars, charIndex, charCount, bytes, byteIndex);
29 | int startByteIndex = byteIndex;
30 | int endCharIndex = charIndex + charCount;
31 | while (charIndex < endCharIndex)
32 | {
33 | byte value = (byte)((GetValue(chars[charIndex++]) << 4) + GetValue(chars[charIndex++]));
34 | bytes[byteIndex++] = value;
35 | }
36 |
37 | return byteIndex - startByteIndex;
38 | }
39 |
40 | static string BytesToString(byte[] bytes)
41 | {
42 | // Minimum length 1.
43 | if (bytes.Length == 0) return "0";
44 |
45 | // length <= digits.Length.
46 | var chars = new byte[(bytes.Length * 0x00026882 + 0xFFFF) >> 16];
47 | int length = 1;
48 |
49 | // For each byte:
50 | for (int j = 0; j != bytes.Length; ++j)
51 | {
52 | // digits = digits * 256 + data[j].
53 | int i, carry = bytes[j];
54 | for (i = 0; i < length || carry != 0; ++i)
55 | {
56 | int value = chars[i] * 256 + carry;
57 | carry = value / 10;
58 | value %= 10;
59 | chars[i] = (byte)value;
60 | }
61 | // digits got longer.
62 | if (i > length) length = i;
63 | }
64 |
65 | // Return string.
66 | var result = new StringBuilder(length);
67 | while (0 != length) result.Append((char)('0' + chars[--length]));
68 | return result.ToString();
69 | }
70 |
71 | public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
72 | {
73 | Validate(bytes, byteIndex, byteCount, chars, charIndex);
74 | byteCount = GetByteCount(bytes, byteIndex, byteCount, chars, charIndex);
75 | int startCharIndex = charIndex;
76 | int endByteIndex = byteIndex + byteCount;
77 | while (byteIndex < endByteIndex)
78 | {
79 | int i;
80 | int carry = bytes[byteIndex++];
81 | int length = 1;
82 | for (i = 0; i < length || carry != 0; ++i)
83 | {
84 | int value = chars[i] * 256 + carry;
85 | carry = value / 10;
86 | value %= 10;
87 | chars[i + charIndex] = (char)(value + '0');
88 | }
89 |
90 | }
91 | return charIndex - startCharIndex;
92 | }
93 |
94 | public override int GetMaxByteCount(int charCount)
95 | {
96 | return (int)Math.Ceiling(charCount * Math.Log(10, 256) / 8);
97 | }
98 |
99 | public override int GetMaxCharCount(int byteCount)
100 | {
101 | return (int)Math.Ceiling(byteCount * 8 / Math.Log(10, 256));
102 | }
103 |
104 | private static int GetValue(char digit)
105 | {
106 | if (digit >= '0' && digit <= '9')
107 | return digit - '0';
108 | throw new ArgumentOutOfRangeException(nameof(digit), digit, GetResourceString("Format_BadBase"));
109 | }
110 |
111 | private static char GetDigit(int value) // Corrected method name
112 | {
113 | return (char)(value + '0');
114 | }
115 |
116 | public override object Clone()
117 | {
118 | return new Base10Encoding();
119 | }
120 | }
121 | }
122 |
--------------------------------------------------------------------------------
/Source/Text/Base32Encoding.cs:
--------------------------------------------------------------------------------
1 | /***************************************************************
2 |
3 | • File: Base16Encoding.cs
4 |
5 | • Description.
6 |
7 | Base32Encoding implements methods for encoding and decoding
8 | data using Base32 encoding.
9 |
10 | ***************************************************************/
11 |
12 | using static System.InternalTools;
13 |
14 | namespace System.Text
15 | {
16 | internal sealed class Base32Encoding : InternalBaseEncoding
17 | {
18 | public override string EncodingName => "base-32";
19 |
20 | public Base32Encoding()
21 | : base(0)
22 | {
23 | }
24 |
25 | public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
26 | {
27 | Validate(bytes, byteIndex, byteCount, chars, charIndex);
28 |
29 | int maxCharCount = GetMaxCharCount(byteCount);
30 | byte value = 0;
31 | byte bitsCount = 5;
32 | int startCharIndex = charIndex;
33 | int endByteIndex = byteIndex + byteCount;
34 | while (byteIndex < endByteIndex)
35 | {
36 | byte currentByte = bytes[byteIndex++];
37 | chars[charIndex++] = GetDigit((byte)(value | (uint)currentByte >> 8 - bitsCount));
38 | if (bitsCount < 4)
39 | {
40 | chars[charIndex++] = GetDigit((byte)(currentByte >> 3 - bitsCount & 0x1F));
41 | bitsCount += 5;
42 | }
43 |
44 | bitsCount -= 3;
45 | value = (byte)(currentByte << bitsCount & 0x1F);
46 | }
47 |
48 | if (charIndex != maxCharCount)
49 | chars[charIndex++] = GetDigit(value);
50 |
51 | return charIndex - startCharIndex;
52 | }
53 |
54 | public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
55 | {
56 | Validate(chars, charIndex, charCount, bytes, byteIndex);
57 | charCount = GetCharCount(chars, charIndex, charCount, bytes, byteIndex);
58 | int byteCount = bytes.GetMaxCount(byteIndex);
59 | int startByteIndex = byteIndex;
60 | int endCharIndex = charIndex + charCount;
61 | uint block = 0;
62 | byte bitsCount = 8;
63 | while (charIndex < endCharIndex)
64 | {
65 | int value = GetValue(chars[charIndex++]);
66 | if (bitsCount > 5)
67 | {
68 | block |= (byte)(value << bitsCount - 5);
69 | bitsCount -= 5;
70 | }
71 | else
72 | {
73 | bytes[byteIndex++] = (byte)(block | (uint)(value >> 5 - bitsCount));
74 | block = (byte)(value << 3 + bitsCount);
75 | bitsCount += 3;
76 | }
77 | }
78 |
79 | if (byteIndex != byteCount)
80 | bytes[byteIndex] = (byte) block;
81 |
82 | return byteIndex - startByteIndex;
83 | }
84 |
85 | private static char GetDigit(byte value)
86 | {
87 | return value < 0x1A ? (char)(value + 0x41) : (char)(value + 0x18);
88 | }
89 |
90 | private static int GetValue(char digit)
91 | {
92 | if (digit < 0x5B && digit > 0x40) return digit - 0x41;
93 | if (digit < 0x7B && digit > 0x60) return digit - 0x61;
94 | if (digit < 0x38 && digit > 0x31) return digit - 0x18;
95 | throw new ArgumentOutOfRangeException(nameof(digit), digit, GetResourceString("Format_BadBase"));
96 | }
97 |
98 | public override int GetByteCount(char[] chars, int index, int count)
99 | {
100 | return GetMaxByteCount(chars.Length - index);
101 | }
102 |
103 | public override int GetCharCount(byte[] bytes, int index, int count)
104 | {
105 | return GetMaxCharCount(bytes.Length - index);
106 | }
107 |
108 | public override int GetMaxByteCount(int charCount)
109 | {
110 | return (charCount << 2) + charCount + 0xB >> 3;
111 | }
112 |
113 | public override int GetMaxCharCount(int byteCount)
114 | {
115 | return (++byteCount << 3) / 5;
116 | }
117 |
118 | public override object Clone()
119 | {
120 | return new Base32Encoding();
121 | }
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/Demo/KeyGen.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | Debug
4 | x86
5 | {D9034D82-3C96-4F9E-95ED-0691D6FF3ABA}
6 | WinExe
7 | false
8 | KeyGen
9 | v4.0
10 |
11 |
12 | 512
13 |
14 |
15 | true
16 | full
17 | false
18 | bin\Debug\
19 | DEBUG;TRACE
20 | prompt
21 | 4
22 | x86
23 | true
24 |
25 |
26 | none
27 | true
28 | bin\Release\
29 | TRACE
30 | prompt
31 | 4
32 | x86
33 | true
34 |
35 |
36 | KeyGen
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 | Form
66 |
67 |
68 | Form
69 |
70 |
71 |
72 | Form
73 |
74 |
75 | Component
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 | {a25af6b1-f1a6-4a0e-989e-ca61c126d865}
86 | ActivationKey
87 |
88 |
89 |
90 |
91 | GenLicForm.cs
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 | copy "$(TargetPath)" "$(SolutionDir)Bin\"
100 |
101 |
--------------------------------------------------------------------------------
/Demo/System.ComponentModel/AssemblyMessageFormatter.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Globalization;
3 | using System.Linq;
4 | using System.Reflection;
5 | using System.Resources;
6 | using System.Runtime.InteropServices;
7 |
8 | namespace System.ComponentModel
9 | {
10 | [Serializable]
11 | [StructLayout(LayoutKind.Sequential, Pack = 4)]
12 | internal class AssemblyMessageFormatter
13 | {
14 | [NonSerialized]
15 | private static readonly int[] _installedLangs;
16 |
17 | [NonSerialized]
18 | private static readonly AssemblyMessageFormatter _defaultFormatter;
19 |
20 | [NonSerialized]
21 | private static readonly Assembly _mscorlib;
22 |
23 | [MarshalAs(UnmanagedType.I4)]
24 | private readonly int _lcid = CultureInfo.CurrentUICulture.LCID;
25 |
26 | [NonSerialized]
27 | private readonly Assembly _assembly = _mscorlib;
28 |
29 | [NonSerialized]
30 | private readonly CultureInfo _culture = CultureInfo.CurrentUICulture;
31 |
32 | public int LCID => _lcid;
33 |
34 | public static AssemblyMessageFormatter DefaultFormatter => _defaultFormatter;
35 |
36 | static AssemblyMessageFormatter()
37 | {
38 | _defaultFormatter = new AssemblyMessageFormatter();
39 | _mscorlib = Assembly.GetAssembly(typeof(object));
40 | CultureInfo[] installedCultures = CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures);
41 | CultureInfo[] specificCultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
42 | List installedLangs = new List(installedCultures.Length);
43 | CultureInfo[] array = specificCultures;
44 | foreach (CultureInfo culture in array)
45 | {
46 | if (installedCultures.Contains(culture))
47 | {
48 | installedLangs.Add(culture.LCID);
49 | }
50 | }
51 | _installedLangs = installedLangs.ToArray();
52 | }
53 |
54 | public AssemblyMessageFormatter(Assembly assembly = null)
55 | {
56 | if (assembly != null)
57 | {
58 | _assembly = assembly;
59 | }
60 | }
61 |
62 | public AssemblyMessageFormatter(int lcid, Assembly assembly = null)
63 | : this(assembly)
64 | {
65 | if (_installedLangs.Contains(lcid))
66 | {
67 | _culture = new CultureInfo(lcid);
68 | _lcid = lcid;
69 | }
70 | }
71 |
72 | public AssemblyMessageFormatter(CultureInfo culture, Assembly assembly = null)
73 | : this(assembly)
74 | {
75 | if (culture != null && _installedLangs.Contains(culture.LCID))
76 | {
77 | _culture = culture;
78 | _lcid = culture.LCID;
79 | }
80 | }
81 |
82 | public AssemblyMessageFormatter(TextInfo info, Assembly assembly = null)
83 | : this(assembly)
84 | {
85 | if (info != null && _installedLangs.Contains(info.LCID))
86 | {
87 | _culture = new CultureInfo(info.LCID);
88 | _lcid = info.LCID;
89 | }
90 | }
91 |
92 | public AssemblyMessageFormatter(string name, Assembly assembly = null)
93 | : this(assembly)
94 | {
95 | try
96 | {
97 | CultureInfo culture = CultureInfo.GetCultureInfo(name);
98 | if (_installedLangs.Contains(culture.LCID))
99 | {
100 | _lcid = culture.LCID;
101 | }
102 | }
103 | catch
104 | {
105 | _lcid = CultureInfo.CurrentUICulture.LCID;
106 | }
107 | }
108 |
109 | private static string GetDescription(Enum enumElement)
110 | {
111 | MemberInfo[] memInfo = enumElement.GetType().GetMember(enumElement.ToString());
112 | if (memInfo != null && memInfo.Length != 0)
113 | {
114 | object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
115 | if (attrs != null && attrs.Length != 0)
116 | {
117 | return ((DescriptionAttribute)attrs[0]).Description;
118 | }
119 | }
120 | return enumElement.ToString();
121 | }
122 |
123 | public static string GetMessage(string messageId, CultureInfo targetUICulture = null, Assembly assembly = null)
124 | {
125 | try
126 | {
127 | if (targetUICulture == null)
128 | {
129 | targetUICulture = CultureInfo.CurrentUICulture;
130 | }
131 | if (assembly == null)
132 | {
133 | assembly = _mscorlib;
134 | }
135 | return new ResourceManager(assembly.GetName().Name, assembly).GetResourceSet(targetUICulture, true, true)?.GetString(messageId);
136 | }
137 | catch
138 | {
139 | return null;
140 | }
141 | }
142 |
143 | public string GetMessage(string messageId)
144 | {
145 | return GetMessage(messageId, new CultureInfo(_lcid), _assembly);
146 | }
147 |
148 | public string FormatMessage(string messageId, params object[] arguments)
149 | {
150 | return string.Format(_culture, GetMessage(messageId), arguments);
151 | }
152 |
153 | public Exception CreateException(Exception innerException, string messageId, params object[] arguments)
154 | {
155 | return new Exception(FormatMessage(messageId, arguments), innerException);
156 | }
157 |
158 | public Exception CreateException(string messageId, params object[] arguments)
159 | {
160 | return new Exception(FormatMessage(messageId, arguments));
161 | }
162 |
163 | public override string ToString()
164 | {
165 | string name = CultureInfo.GetCultureInfo(_lcid).DisplayName;
166 | if (!string.IsNullOrEmpty(name))
167 | {
168 | return name;
169 | }
170 | return _lcid.ToString();
171 | }
172 | }
173 | }
174 |
--------------------------------------------------------------------------------
/Demo/Test/Program.cs:
--------------------------------------------------------------------------------
1 | using System.IO;
2 | using System.Security.Activation;
3 | using System.Security.Cryptography;
4 | using System;
5 | using System.Linq;
6 | using System.Net.NetworkInformation;
7 | using System.Text;
8 |
9 | internal static class Program
10 | {
11 |
12 | // Custom encoding example with numbers, latine and cyrilic characters
13 | private static string Base128 = "0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnmЙЦУКЕЁНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮйцукеёнгшщзхъфывапролджэячсмитьбю";
14 |
15 | // Input data
16 | private static byte[] HelloWorld = { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21 };
17 |
18 | private static void Main(string[] args)
19 | {
20 | byte[] macAddr =
21 | (
22 | from netInterface in NetworkInterface.GetAllNetworkInterfaces()
23 | where netInterface.OperationalStatus == OperationalStatus.Up
24 | select netInterface.GetPhysicalAddress().GetAddressBytes()
25 | ).FirstOrDefault();
26 |
27 | // Pass one: using the default encryptor without data.
28 | Console.WriteLine();
29 | Console.WriteLine("Default cryptography without data:");
30 | using (ActivationKey key = ActivationKey.CreateEncryptor(macAddr).Generate())
31 | {
32 | using (ActivationKeyDecryptor decryptor = key.CreateDecryptor(macAddr))
33 | {
34 | if (decryptor.Success && decryptor.Data.Length != 0)
35 | {
36 | using (TextReader reader = decryptor.GetTextReader(null))
37 | {
38 | Console.WriteLine("The key content is: " + reader.ReadToEnd());
39 | }
40 | }
41 | Console.WriteLine("Base10: \t" + key.ToString(PrintableEncoding.Decimal));
42 | Console.WriteLine("Base16: \t" + key.ToString(PrintableEncoding.Hexadecimal));
43 | Console.WriteLine("Base32: \t" + key);
44 | Console.WriteLine("Base64: \t" + key.ToString(PrintableEncoding.Base64));
45 | Console.WriteLine("Base128:\t" + key.ToString(ActivationKeyTextParser.CreateEncoding(Base128)));
46 | ActivationKey.DefaultManager.SaveToFile(key, "key1.bin", true);
47 | ActivationKey.DefaultManager.SaveToFile(key, "key1.txt");
48 | }
49 | }
50 |
51 | // Pass two: using the default encryptor with data.
52 | Console.WriteLine();
53 | Console.WriteLine("Default cryptography with data:");
54 | object[] objArray1 = { HelloWorld };
55 | using (ActivationKey key = ActivationKey.CreateEncryptor(new object[0]).Generate(objArray1))
56 | {
57 | using (ActivationKeyDecryptor decryptor = key.CreateDecryptor(new object[0]))
58 | {
59 | if (decryptor.Success && (decryptor.Data.Length != 0))
60 | {
61 | using (TextReader reader = decryptor.GetTextReader(null))
62 | {
63 | Console.WriteLine("The key content is: " + reader.ReadToEnd());
64 | }
65 | }
66 | Console.WriteLine("Base10: \t" + key.ToString(PrintableEncoding.Decimal));
67 | Console.WriteLine("Base16: \t" + key.ToString(PrintableEncoding.Hexadecimal));
68 | Console.WriteLine("Base32: \t" + key);
69 | Console.WriteLine("Base64: \t" + key.ToString(PrintableEncoding.Base64));
70 | Console.WriteLine("Base128:\t" + key.ToString(ActivationKeyTextParser.CreateEncoding(Base128)));
71 | ActivationKey.DefaultManager.SaveToFile(key, "key2.bin", true);
72 | ActivationKey.DefaultManager.SaveToFile(key, "key2.txt");
73 | }
74 | }
75 |
76 | // Pass three: using the AES encryptor and MD5 hash algorithm with data.
77 | Console.WriteLine();
78 | Console.WriteLine("Custom cryptography (AES+MD5) with data:");
79 | object[] objArray2 = { HelloWorld };
80 | using (ActivationKey key = ActivationKey.CreateEncryptor(new object[0]).Generate(objArray2))
81 | {
82 | using (ActivationKeyDecryptor decryptor = key.CreateDecryptor(new object[0]))
83 | {
84 | if (decryptor.Success && (decryptor.Data.Length != 0))
85 | {
86 | using (TextReader reader = decryptor.GetTextReader(null))
87 | {
88 | Console.WriteLine("The key content is: " + reader.ReadToEnd());
89 | }
90 | }
91 | Console.WriteLine("Base10: \t" + key.ToString(PrintableEncoding.Decimal));
92 | Console.WriteLine("Base16: \t" + key.ToString(PrintableEncoding.Hexadecimal));
93 | Console.WriteLine("Base32: \t" + key);
94 | Console.WriteLine("Base64: \t" + key.ToString(PrintableEncoding.Base64));
95 | Console.WriteLine("Base128:\t" + key.ToString(ActivationKeyTextParser.CreateEncoding(Base128)));
96 | ActivationKey.DefaultManager.SaveToFile(key, "key3.bin", true);
97 | ActivationKey.DefaultManager.SaveToFile(key, "key3.txt", false);
98 | }
99 | }
100 | Console.ReadKey();
101 | }
102 | }
103 |
104 |
105 |
--------------------------------------------------------------------------------
/Demo/System/Extensions.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.ComponentModel;
3 | using System.Linq;
4 | using System.Reflection;
5 |
6 | namespace System
7 | {
8 | public static class Extensions
9 | {
10 | public static string GetMessages(this Exception e)
11 | {
12 | string result = $"{e.Message} in {e.TargetSite}\n";
13 | if (e.InnerException != null)
14 | {
15 | result += e.InnerException.GetMessages();
16 | }
17 | return result;
18 | }
19 |
20 | public static IEnumerable> GetProperties(this object obj)
21 | {
22 | return from p in obj.GetType().GetProperties()
23 | let key = p.Name
24 | let result = p.GetValue(obj, null)
25 | select new KeyValuePair(key, result);
26 | }
27 |
28 | public static IEnumerable> GetProperties(this object obj, Func selector = null) where TAttr : Attribute
29 | {
30 | Type type = obj.GetType();
31 | if (!typeof(TAttr).IsSubclassOf(typeof(Attribute)))
32 | {
33 | throw new InvalidOperationException();
34 | }
35 | return from p in type.GetProperties()
36 | from a in p.GetCustomAttributes(false)
37 | where a is TAttr
38 | let key = selector?.Invoke((TAttr)a) ?? p.Name
39 | let result = p.GetValue(obj, null)
40 | select new KeyValuePair(key, result);
41 | }
42 |
43 | public static IEnumerable> GetProperties(this object obj, Func selector = null) where TAttr : Attribute
44 | {
45 | return from p in obj.GetType().GetProperties()
46 | from a in p.GetCustomAttributes(false)
47 | let result = p.GetValue(obj, null)
48 | where result is TResult && a is TAttr
49 | let key = selector?.Invoke((TAttr)a) ?? p.Name
50 | select new KeyValuePair(key, (TResult)result);
51 | }
52 |
53 | public static IEnumerable> GetProperties(this object obj, Func selector = null) where TAttr : Attribute
54 | {
55 | return from p in obj.GetType().GetProperties()
56 | from a in p.GetCustomAttributes(false)
57 | let result = p.GetValue(obj, null)
58 | where result is TResult && a is TAttr
59 | let key = selector((TAttr)a)
60 | select new KeyValuePair(key, (TResult)result);
61 | }
62 |
63 | public static IEnumerable> GetProperties(this object obj) where TAttr : Attribute
64 | {
65 | return from p in obj.GetType().GetProperties()
66 | from a in p.GetCustomAttributes(false)
67 | where a is TAttr
68 | let result = p.GetValue(obj, null)
69 | select new KeyValuePair((TAttr)a, result);
70 | }
71 |
72 | public static IEnumerable> GetProperties(this object obj) where TAttr : Attribute
73 | {
74 | return from p in obj.GetType().GetProperties()
75 | from a in p.GetCustomAttributes(false)
76 | let result = p.GetValue(obj, null)
77 | where result is TResult && a is TAttr
78 | select new KeyValuePair((TAttr)a, (TResult)result);
79 | }
80 |
81 | public static IEnumerable> GetProperties(this object obj, params Type[] types)
82 | {
83 | return from p in obj.GetType().GetProperties()
84 | from a in p.GetCustomAttributes(false)
85 | let result = p.GetValue(obj, null)
86 | where types.Any((Type t) => t.IsInstanceOfType(a))
87 | select new KeyValuePair((Attribute)a, result);
88 | }
89 |
90 | internal static void SetPropertyValue(this PropertyInfo property, T value)
91 | {
92 | if (value != null)
93 | {
94 | property?.SetValue(null, value, null);
95 | }
96 | }
97 |
98 | internal static void SetPropertyValue(this PropertyInfo property, object value)
99 | {
100 | if (value != null)
101 | {
102 | property?.SetValue(null, value, null);
103 | }
104 | }
105 |
106 | internal static T GetPropertyValue(this PropertyInfo property)
107 | {
108 | if (!(property == null))
109 | {
110 | return (T)property.GetValue(null, null);
111 | }
112 | return default(T);
113 | }
114 |
115 | internal static object GetPropertyValue(this PropertyInfo property)
116 | {
117 | if (!(property == null))
118 | {
119 | return property.GetValue(null, null);
120 | }
121 | return null;
122 | }
123 |
124 | internal static TypeConverter GetPropertyConverter(this PropertyInfo property, bool createDefault = false)
125 | {
126 | Type propertyType = property.GetType();
127 | TypeConverterAttribute propertyConverter;
128 | if ((propertyConverter = property.GetCustomAttributes(typeof(TypeConverterAttribute), false).FirstOrDefault() as TypeConverterAttribute) != null)
129 | {
130 | Type converterType = Type.GetType(propertyConverter.ConverterTypeName);
131 | TypeConverter converter;
132 | if (converterType != null && (converter = Activator.CreateInstance(converterType) as TypeConverter) != null)
133 | {
134 | return converter;
135 | }
136 | }
137 | TypeConverter typeConverter = TypeDescriptor.GetConverter(propertyType);
138 | if (!createDefault)
139 | {
140 | return null;
141 | }
142 | return typeConverter;
143 | }
144 |
145 | public static bool ImplementsInterface(this Type type, Type interfaceType)
146 | {
147 | if (type == null || (!type.IsClass && !type.IsValueType))
148 | {
149 | throw new ArgumentException(null, "type");
150 | }
151 | if (interfaceType == null || !interfaceType.IsInterface)
152 | {
153 | throw new ArgumentException(null, "interfaceType");
154 | }
155 | return type.GetInterfaces().Any((Type i) => i == interfaceType);
156 | }
157 |
158 | public static bool ImplementsInterface(this Type type) where T : class
159 | {
160 | return type.ImplementsInterface(typeof(T));
161 | }
162 | }
163 | }
164 |
--------------------------------------------------------------------------------
/Source/Text/Base64Encoding.cs:
--------------------------------------------------------------------------------
1 | /***************************************************************
2 |
3 | • File: Base64Encoding.cs
4 |
5 | • Description.
6 |
7 | Base64Encoding implements methods for encoding and decoding
8 | data using Base64 encoding. To make the text easier to read,
9 | the '+' and '/' symbols were replaced with '#' and '$', and
10 | the trailing symbol '=' were removed.
11 |
12 | ***************************************************************/
13 |
14 | using static System.InternalTools;
15 |
16 | namespace System.Text
17 | {
18 | internal sealed class Base64Encoding : InternalBaseEncoding
19 | {
20 | public override string EncodingName => "base-64";
21 |
22 | public Base64Encoding() : base(0)
23 | {
24 | }
25 |
26 | public override byte[] GetBytes(string s)
27 | {
28 | return base.GetBytes(s.Trim('='));
29 | }
30 |
31 | public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
32 | {
33 | Validate(bytes, byteIndex, byteCount, chars, charIndex);
34 |
35 | int mod = byteCount % 3;
36 | int startCharIndex = charIndex;
37 | int endByteIndex = byteIndex + (byteCount - mod);
38 |
39 | while (byteIndex < endByteIndex)
40 | {
41 | chars[charIndex] = GetDigit((bytes[byteIndex] & 0xFC) >> 2);
42 | chars[charIndex + 1] = GetDigit((bytes[byteIndex] & 0x03) << 4 | (bytes[byteIndex + 1] & 0xF0) >> 4);
43 | chars[charIndex + 2] = GetDigit((bytes[byteIndex + 0x1] & 0xF) << 2 | (bytes[byteIndex + 2] & 0xC0) >> 6);
44 | chars[charIndex + 3] = GetDigit(bytes[byteIndex + 0x2] & 0x3F);
45 | byteIndex += 3;
46 | charIndex += 4;
47 | }
48 |
49 | switch (mod)
50 | {
51 | case 1:
52 | chars[charIndex] = GetDigit((bytes[endByteIndex] & 0xFC) >> 2);
53 | chars[charIndex + 1] = GetDigit((bytes[endByteIndex] & 0x3) << 4);
54 | charIndex += 2;
55 | break;
56 | case 2:
57 | chars[charIndex] = GetDigit((bytes[endByteIndex] & 0xFC) >> 2);
58 | chars[charIndex + 1] = GetDigit((bytes[endByteIndex] & 0x3) << 4 | (bytes[endByteIndex + 0x1] & 0xF0) >> 4);
59 | chars[charIndex + 2] = GetDigit((bytes[endByteIndex + 1] & 0xF) << 2);
60 | charIndex += 3;
61 | break;
62 | }
63 |
64 | return charIndex - startCharIndex;
65 | }
66 |
67 | public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
68 | {
69 | Validate(chars, charIndex, charCount, bytes, byteIndex);
70 |
71 | int mod = GetMaxByteCount(charCount) % 3;
72 | int startByteIndex = byteIndex;
73 | int endCharIndex = charIndex + charCount;
74 | uint block = byte.MaxValue;
75 |
76 | while (charIndex < endCharIndex)
77 | {
78 | uint value = (uint) GetValue(chars[charIndex++]);
79 | block = block << 6 | value;
80 |
81 | if ((block & 0x80000000U) != 0)
82 | {
83 | bytes[byteIndex] = (byte)(block >> 16);
84 | bytes[byteIndex + 1] = (byte)(block >> 8);
85 | bytes[byteIndex + 2] = (byte)block;
86 |
87 | byteIndex += 3;
88 | block = 0xFF;
89 | }
90 | }
91 |
92 | switch (mod)
93 | {
94 | case 1:
95 | bytes[byteIndex] = (byte)(block >> 4);
96 | byteIndex += 1;
97 | break;
98 | case 2:
99 | bytes[byteIndex] = (byte)(block >> 10);
100 | bytes[byteIndex + 1] = (byte)(block >> 2);
101 | byteIndex += 2;
102 | break;
103 | }
104 |
105 | return byteIndex - startByteIndex;
106 | }
107 |
108 | public char GetDigit(int value)
109 | {
110 | if (value < 0x1A) return (char)(value + 0x41);
111 | if (value < 0x34) return (char)(value + 0x47);
112 | if (value < 0x3E) return (char)(value - 0x04);
113 | if (value == 0x3E) return (char)0x23;
114 | if (value == 0x3F) return (char)0x24;
115 | return (char)0x3D;
116 | }
117 |
118 | private int GetValue(char digit)
119 | {
120 | if (digit < 0x5B && digit > 0x40) return digit - 0x41;
121 | if (digit < 0x7B && digit > 0x60) return digit - 0x47;
122 | if (digit < 0x3A && digit > 0x2F) return digit + 0x04;
123 | if (digit == 0x23) return 0x3E;
124 | if (digit == 0x24) return 0x3F;
125 | throw new ArgumentOutOfRangeException(nameof(digit), digit, GetResourceString("Format_BadBase"));
126 | }
127 |
128 | public override int GetByteCount(char[] chars, int index, int count)
129 | {
130 | return GetMaxByteCount(chars.Length - index);
131 | }
132 |
133 | public override int GetCharCount(byte[] bytes, int index, int count)
134 | {
135 | return GetMaxCharCount(bytes.Length - index);
136 | }
137 |
138 |
139 | public override int GetMaxByteCount(int charCount)
140 | {
141 | return (charCount * 3) >> 2;
142 | }
143 |
144 | public override int GetMaxCharCount(int byteCount)
145 | {
146 | return ((byteCount << 2) | 2) / 3;
147 | }
148 |
149 | public override object Clone()
150 | {
151 | return new Base64Encoding();
152 | }
153 | }
154 | }
--------------------------------------------------------------------------------
/Demo/System.Windows.Forms/GenLicForm.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 |
107 |
108 |
109 | text/microsoft-resx
110 |
111 |
112 | 2.0
113 |
114 |
115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
116 |
117 |
118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
119 |
120 |
--------------------------------------------------------------------------------
/Demo/System.Management.WMI/ProcessorInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace System.Management.WMI
4 | {
5 | public class ProcessorInfo
6 | {
7 | public ushort AddressWidth {get; set;}
8 | public ushort Architecture {get; set;}
9 | public ushort Availability {get; set;}
10 | public string Caption {get; set;}
11 | public object ConfigManagerErrorCode {get; set;}
12 | public object ConfigManagerUserConfig {get; set;}
13 | public ushort CpuStatus {get; set;}
14 | public string CreationClassName {get; set;}
15 | public uint CurrentClockSpeed {get; set;}
16 | public ushort CurrentVoltage {get; set;}
17 | public ushort DataWidth {get; set;}
18 | public string Description {get; set;}
19 | public string DeviceID {get; set;}
20 | public object ErrorCleared {get; set;}
21 | public object ErrorDescription {get; set;}
22 | public uint ExtClock {get; set;}
23 | public ushort Family {get; set;}
24 | public object InstallDate {get; set;}
25 | public uint L2CacheSize {get; set;}
26 | public object L2CacheSpeed {get; set;}
27 | public uint L3CacheSize {get; set;}
28 | public uint L3CacheSpeed {get; set;}
29 | public object LastErrorCode {get; set;}
30 | public ushort Level {get; set;}
31 | public ushort LoadPercentage {get; set;}
32 | public string Manufacturer {get; set;}
33 | public uint MaxClockSpeed {get; set;}
34 | public string Name {get; set;}
35 | public uint NumberOfCores {get; set;}
36 | public uint NumberOfLogicalProcessors {get; set;}
37 | public object OtherFamilyDescription {get; set;}
38 | public object PNPDeviceID {get; set;}
39 | public object PowerManagementCapabilities {get; set;}
40 | public bool PowerManagementSupported {get; set;}
41 | public string ProcessorId {get; set;}
42 | public ushort ProcessorType {get; set;}
43 | public ushort Revision {get; set;}
44 | public string Role {get; set;}
45 | public string SocketDesignation {get; set;}
46 | public string Status {get; set;}
47 | public ushort StatusInfo {get; set;}
48 | public object Stepping {get; set;}
49 | public string SystemCreationClassName {get; set;}
50 | public string SystemName {get; set;}
51 | public object UniqueId {get; set;}
52 | public ushort UpgradeMethod {get; set;}
53 | public string Version {get; set;}
54 | public object VoltageCaps {get; set;}
55 | private ProcessorInfo()
56 | {
57 | }
58 |
59 | private static T GetPropertyValue(ManagementObject wmi, string propertyName)
60 | {
61 | try
62 | {
63 | return (T)(wmi.GetPropertyValue(propertyName) ?? ((object)default(T)));
64 | }
65 | catch
66 | {
67 | return default(T);
68 | }
69 | }
70 |
71 | public static ProcessorInfo[] GetProcessorInfo()
72 | {
73 | ManagementClass managementClass = new ManagementClass(new ManagementPath("Win32_Processor"));
74 | List list = new List();
75 | foreach (ManagementObject item in managementClass.GetInstances())
76 | {
77 | list.Add(new ProcessorInfo
78 | {
79 | AddressWidth = GetPropertyValue(item, "AddressWidth"),
80 | Architecture = GetPropertyValue(item, "Architecture"),
81 | Availability = GetPropertyValue(item, "Availability"),
82 | Caption = GetPropertyValue(item, "Caption"),
83 | ConfigManagerErrorCode = GetPropertyValue