KNM.CryptoHelper 1.2.1
KNM.CryptoHelper
Description
KNM.CryptoHelper is a C# library for AES symmetric encryption with HMAC integrity, designed to be configurable and reusable in .NET projects.
Installation
Build the project as a Class Library to generate the KNM.CryptoHelper.dll and include it in your project.
Usage
Configuration (simple)
var options = new CryptoOptions
{
Password = "password_segreta",
SaltSize = 16,
IvSize = 16,
KeySize = 32,
Iterations = 100_000
};
var cryptoHelper = new CryptoHelper(options);
Configuration (with Injection)
Register the services in the DI collection In the ConfigureServices method of ASP.NET Core (or in the generic builder):
services.Configure<CryptoOptions>(configuration.GetSection("CryptoOptions")); // Se vuoi usare appsettings.json
services.AddSingleton<ICryptoHelper>(sp =>
{
var options = sp.GetRequiredService<IOptions<CryptoOptions>>().Value;
return new CryptoHelper(options);
});
Or passing a manual configuration:
var options = new CryptoOptions
{
Password = "miaPassword",
SaltSize = 16,
IvSize = 16,
KeySize = 32,
Iterations = 100_000
};
services.AddSingleton<ICryptoHelper>(new CryptoHelper(options));
Or if you want to use default values:
services.AddSingleton<ICryptoHelper>(new CryptoHelper());
Encrypting a text
string testoChiaro = "Testo da criptare";
string testoCifrato = cryptoHelper.EncryptText(testoChiaro);
Console.WriteLine($"Testo cifrato: {testoCifrato}");
Decrypting an encrypted text
string testoDecifrato = cryptoHelper.DecryptText(testoCifrato);
Console.WriteLine($"Testo decifrato: {testoDecifrato}");
Technical details
- Encryption is AES in CBC mode with PKCS7 padding.
- The AES key is derived using PBKDF2 with SHA256.
- Message integrity is guaranteed with HMAC-SHA256.
- The encrypted string is formatted as:
!HMAC:SALT:IV:CIPHERTEXT(all Base64).
Error handling
- A
CryptographicExceptionis thrown in case of tampering or wrong password. - Incorrect input formats throw
FormatException.
Notes
- For security reasons, change the default password to a strong and unique one in your environment.
- Parameters like salt size, iterations, and key size are customizable via
CryptoOptions.
Showing the top 20 packages that depend on KNM.CryptoHelper.
| Packages | Downloads |
|---|---|
|
KNM.LicenseValidator
Hybrid offline/online license validation library for .NET 9 with multilanguage support (IT/EN). Features RSA, AES, and HMAC security with database storage and API integration.
|
0 |
|
KNM.LicenseValidator
Hybrid offline/online license validation library for .NET 9 with multilanguage support (IT/EN). Features RSA, AES, and HMAC security with database storage and API integration.
|
1 |
|
KNM.LicenseValidator
Hybrid offline/online license validation library for .NET 9 with multilanguage support (IT/EN). Features RSA, AES, and HMAC security with database storage and API integration.
|
2 |
.NET 9.0
- System.IdentityModel.Tokens.Jwt (>= 8.15.0)