├── .gitignore ├── LICENSE ├── LicenseGenerator.pas ├── LicenseManager.pas ├── LicenseValidator.pas ├── Main.pas └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Uncomment these types if you want even more clean repository. But be careful. 2 | # It can make harm to an existing project source. Read explanations below. 3 | # 4 | # Resource files are binaries containing manifest, project icon and version info. 5 | # They can not be viewed as text or compared by diff-tools. Consider replacing them with .rc files. 6 | #*.res 7 | # 8 | # Type library file (binary). In old Delphi versions it should be stored. 9 | # Since Delphi 2009 it is produced from .ridl file and can safely be ignored. 10 | #*.tlb 11 | # 12 | # Diagram Portfolio file. Used by the diagram editor up to Delphi 7. 13 | # Uncomment this if you are not using diagrams or use newer Delphi version. 14 | #*.ddp 15 | # 16 | # Visual LiveBindings file. Added in Delphi XE2. 17 | # Uncomment this if you are not using LiveBindings Designer. 18 | #*.vlb 19 | # 20 | # Deployment Manager configuration file for your project. Added in Delphi XE2. 21 | # Uncomment this if it is not mobile development and you do not use remote debug feature. 22 | #*.deployproj 23 | # 24 | # C++ object files produced when C/C++ Output file generation is configured. 25 | # Uncomment this if you are not using external objects (zlib library for example). 26 | #*.obj 27 | # 28 | 29 | # Delphi compiler-generated binaries (safe to delete) 30 | *.exe 31 | *.dll 32 | *.bpl 33 | *.bpi 34 | *.dcp 35 | *.so 36 | *.apk 37 | *.drc 38 | *.map 39 | *.dres 40 | *.rsm 41 | *.tds 42 | *.dcu 43 | *.lib 44 | *.a 45 | *.o 46 | *.ocx 47 | 48 | # Delphi autogenerated files (duplicated info) 49 | *.cfg 50 | *.hpp 51 | *Resource.rc 52 | 53 | # Delphi local files (user-specific info) 54 | *.local 55 | *.identcache 56 | *.projdata 57 | *.tvsconfig 58 | *.dsk 59 | 60 | # Delphi history and backups 61 | __history/ 62 | __recovery/ 63 | *.~* 64 | 65 | # Castalia statistics file (since XE7 Castalia is distributed with Delphi) 66 | *.stat 67 | 68 | # Boss dependency manager vendor folder https://github.com/HashLoad/boss 69 | modules/ 70 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 DelphiFan 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 | -------------------------------------------------------------------------------- /LicenseGenerator.pas: -------------------------------------------------------------------------------- 1 | unit LicenseGenerator; 2 | 3 | interface 4 | 5 | uses 6 | SysUtils, DateUtils, EncdDecd; 7 | 8 | function GenerateLicenseKey(const productName, productKey: string; expirationDate: TDateTime; encryptionKey: string): string; 9 | 10 | implementation 11 | 12 | function GenerateLicenseKey(const productName, productKey: string; expirationDate: TDateTime; encryptionKey: string): string; 13 | var 14 | rawKey: string; 15 | encodedKey: string; 16 | begin 17 | // Create a raw key containing product name, product key, and expiration date 18 | rawKey := productName + '|' + productKey + '|' + FormatDateTime('yyyy-mm-dd', expirationDate); 19 | 20 | // Encrypt the raw key 21 | encodedKey := EncodeString(encryptionKey, rawKey); 22 | 23 | Result := encodedKey; 24 | end; 25 | 26 | end. 27 | -------------------------------------------------------------------------------- /LicenseManager.pas: -------------------------------------------------------------------------------- 1 | unit LicenseManager; 2 | 3 | interface 4 | 5 | uses 6 | SysUtils, IniFiles; 7 | 8 | function SaveLicenseKey(const licenseKey: string): Boolean; 9 | function LoadLicenseKey: string; 10 | 11 | implementation 12 | 13 | const 14 | LicenseIniFileName = 'MyAppLicense.ini'; 15 | LicenseSection = 'License'; 16 | 17 | function SaveLicenseKey(const licenseKey: string): Boolean; 18 | var 19 | iniFile: TIniFile; 20 | begin 21 | try 22 | iniFile := TIniFile.Create(LicenseIniFileName); 23 | try 24 | iniFile.WriteString(LicenseSection, 'Key', licenseKey); 25 | finally 26 | iniFile.Free; 27 | end; 28 | Result := True; 29 | except 30 | Result := False; 31 | end; 32 | end; 33 | 34 | function LoadLicenseKey: string; 35 | var 36 | iniFile: TIniFile; 37 | begin 38 | try 39 | iniFile := TIniFile.Create(LicenseIniFileName); 40 | try 41 | Result := iniFile.ReadString(LicenseSection, 'Key', ''); 42 | finally 43 | iniFile.Free; 44 | end; 45 | except 46 | Result := ''; 47 | end; 48 | end; 49 | 50 | end. 51 | -------------------------------------------------------------------------------- /LicenseValidator.pas: -------------------------------------------------------------------------------- 1 | unit LicenseValidator; 2 | 3 | interface 4 | 5 | uses 6 | SysUtils, DateUtils, EncdDecd; 7 | 8 | function ValidateLicenseKey(const licenseKey, productName, productKey, encryptionKey: string): Boolean; 9 | 10 | implementation 11 | 12 | function ValidateLicenseKey(const licenseKey, productName, productKey, encryptionKey: string): Boolean; 13 | var 14 | rawKey: string; 15 | expirationDate: TDateTime; 16 | begin 17 | // Decrypt the license key 18 | rawKey := DecodeString(encryptionKey, licenseKey); 19 | 20 | if ContainsStr(rawKey, productName) and ContainsStr(rawKey, productKey) then 21 | begin 22 | // Extract and check expiration date 23 | Delete(rawKey, 1, Length(productName) + 1 + Length(productKey) + 1); 24 | expirationDate := StrToDateDef(rawKey, 0); 25 | Result := (expirationDate > Now); 26 | end 27 | else 28 | Result := False; 29 | end; 30 | 31 | end. 32 | -------------------------------------------------------------------------------- /Main.pas: -------------------------------------------------------------------------------- 1 | uses 2 | LicenseManager; 3 | 4 | // When generating a license key, save it to the configuration file 5 | function GenerateAndSaveLicenseKey(const productName, productKey: string; expirationDate: TDateTime; encryptionKey: string): string; 6 | var 7 | licenseKey: string; 8 | begin 9 | licenseKey := GenerateLicenseKey(productName, productKey, expirationDate, encryptionKey); 10 | SaveLicenseKey(licenseKey); 11 | Result := licenseKey; 12 | end; 13 | 14 | // When your program starts, load the license key from the configuration file 15 | function CheckLicense: Boolean; 16 | var 17 | licenseKey: string; 18 | productName: string; 19 | productKey: string; 20 | encryptionKey: string; 21 | begin 22 | productName := 'YourProduct'; 23 | productKey := 'YourProductKey'; 24 | encryptionKey := 'YourEncryptionKey'; 25 | 26 | licenseKey := LoadLicenseKey; 27 | 28 | if not ValidateLicenseKey(licenseKey, productName, productKey, encryptionKey) then 29 | begin 30 | // Invalid license, take appropriate action (e.g., show an error message and exit) 31 | Result := False; 32 | end 33 | else 34 | begin 35 | // Valid license, continue running the application 36 | Result := True; 37 | end; 38 | end; 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Software Licensing System with Delphi 2 | 3 | This Delphi project demonstrates a simple software licensing system that generates and validates license keys. It allows you to control access to your software by issuing license keys with expiration dates and other information. 4 | 5 | ## Overview 6 | 7 | - Generate license keys with a specified product name, product key, and expiration date. 8 | - Store and validate license keys to control software access. 9 | 10 | ## Features 11 | 12 | - Generate and validate software license keys. 13 | - Store license keys in a configuration file for persistence. 14 | 15 | ## Prerequisites 16 | 17 | - [Embarcadero Delphi](https://www.embarcadero.com/products/delphi) or a compatible development environment. 18 | - Basic understanding of Delphi programming. 19 | 20 | ## Getting Started 21 | 22 | 1. Clone or download this repository to your local machine. 23 | 24 | 2. Open the Delphi project in your development environment. 25 | 26 | 3. Configure the project with your specific product name, product key, and encryption key in the `LicenseGenerator.pas` and `LicenseValidator.pas` units. 27 | 28 | 4. Build and run the project to generate a license key using the `GenerateAndSaveLicenseKey` function in your application. 29 | 30 | 5. The generated license key is automatically saved in the configuration file. 31 | 32 | 6. When your software starts, load the license key from the configuration file using the `LoadLicenseKey` function. 33 | 34 | 7. Validate the loaded license key with the `CheckLicense` function. 35 | 36 | 8. Take action based on the license validation result, such as allowing or restricting access to your software. 37 | 38 | ## Usage Example 39 | 40 | Here is an example of how to generate and save a license key in your Delphi application: 41 | 42 | ```delphi 43 | var 44 | productName: string; 45 | productKey: string; 46 | expirationDate: TDateTime; 47 | encryptionKey: string; 48 | licenseKey: string; 49 | begin 50 | // Set your product information and encryption key 51 | productName := 'YourProduct'; 52 | productKey := 'YourProductKey'; 53 | expirationDate := EncodeDate(2023, 12, 31); 54 | encryptionKey := 'YourEncryptionKey'; 55 | 56 | // Generate and save the license key 57 | licenseKey := GenerateAndSaveLicenseKey(productName, productKey, expirationDate, encryptionKey); 58 | 59 | // Use the generated license key as needed 60 | end; 61 | ``` 62 | To check the license when your application starts: 63 | ```delphi 64 | if not CheckLicense then 65 | begin 66 | // Invalid license, take appropriate action (e.g., show an error message and exit) 67 | Application.Terminate; 68 | end 69 | else 70 | begin 71 | // Valid license, continue running the application 72 | // Add your application's main code here 73 | end; 74 | ``` 75 | 76 | ## License 77 | This library is open source and released under the MIT License. You are free to use, modify, and distribute it as needed. Please check the LICENSE file for more details. 78 | 79 | ## Contributions 80 | Contributions to this library are welcome. Feel free to open issues, submit pull requests, or suggest improvements. Your feedback and contributions will help make this library even better. 81 | 82 | ## Issues 83 | If you encounter any issues or have questions related to this library, please open a GitHub issue, and we will do our best to assist you. 84 | 85 | We welcome contributions and feedback from the community to improve and enhance this project. 86 | 87 | Enjoy using the licensing system in your Delphi applications! 88 | 89 | 90 | Please replace `YourProduct`, `YourProductKey`, and `YourEncryptionKey` with your actual product name, product key, and encryption key in the examples. Update the links to your GitHub repository and external references as needed. This README provides examples of how to generate, save, and check the license in a Delphi application. 91 | 92 | 93 | --------------------------------------------------------------------------------