Json.Protector
1.2.0
dotnet add package Json.Protector --version 1.2.0
NuGet\Install-Package Json.Protector -Version 1.2.0
<PackageReference Include="Json.Protector" Version="1.2.0" />
<PackageVersion Include="Json.Protector" Version="1.2.0" />
<PackageReference Include="Json.Protector" />
paket add Json.Protector --version 1.2.0
#r "nuget: Json.Protector, 1.2.0"
#:package Json.Protector@1.2.0
#addin nuget:?package=Json.Protector&version=1.2.0
#tool nuget:?package=Json.Protector&version=1.2.0
Json.Protector
Json.Protector is a .NET library designed to protect sensitive data during JSON serialization and deserialization. This package supports both Newtonsoft.Json and System.Text.Json.
Features
Protects sensitive data by encrypting it during serialization.
Automatically decrypts data during deserialization.
Provides seamless integration with Newtonsoft.Json and System.Text.Json.
Installation
You can install this package via NuGet:
dotnet add package Json.Protector
Configuration
Using Use Default Key
builder.Services.AddJsonProtector();
Using Set Encryption Key
builder.Services.AddJsonProtector(options =>
{
options.UseDefaultKey = false;
options.Key = "Your Key-wffJGGHG#wrwfsCsddDDFgD$#@";
options.IV = "Your Iv-eF3RFfdgdsE";
});
Using Set Encryption Key With ValidityPeriod
builder.Services.AddJsonProtector(options =>
{
options.UseDefaultKey = false;
options.Key = "Your Key-wffJGGHG#wrwfsCsddDDFgD$#@";
options.IV = "Your Iv-eF3RFfdgdsE";
options.ValidityPeriod = TimeSpan.FromSeconds(20);
options.ThrowExceptionIfTimeExpired = true;
});
Using Newtonsoft.Json
To configure Json.Protector with Newtonsoft.Json, add the following to your Program.cs file:
builder.Services.AddSingleton<NewtonsoftJsonProtectorTypeConverter>();
builder.Services.AddSingleton<NewtonsoftDataProtector>();
builder.Services.AddControllers().AddNewtonsoftJson(options =>
{
using var serviceProvider = builder.Services.BuildServiceProvider();
// Register the converter with dependency injection
options.SerializerSettings.Converters.Add(serviceProvider.GetRequiredService<NewtonsoftJsonProtectorTypeConverter>());
serviceProvider.GetRequiredService<NewtonsoftDataProtector>();
});
Using System.Text.Json
To configure Json.Protector with System.Text.Json, use the following code:
builder.Services.AddSingleton<JsonConverter<JsonProtectorType>>(sp => new SystemTextJsonJsonProtectorTypeConverter(sp.GetRequiredService<IEncryptionProvider>()));
builder.Services.AddControllers().AddJsonOptions(options =>
{
using var serviceProvider = builder.Services.BuildServiceProvider();
// Register the converter with dependency injection
options.JsonSerializerOptions.Converters.Add(serviceProvider.GetRequiredService<JsonConverter<JsonProtectorType>>());
});
Usage
To encrypt and protect your sensitive data, use the JsonProtectorType.This type is equivalent to a string and supports text data.
Here’s an example:
public class UserProfile
{
public string Name { get; set; }
public JsonProtectorType SensitiveInfo { get; set; }
}
// Example Data
var profile = new UserProfile
{
Name = "saied rahimi",
SensitiveInfo = "This is encrypted data"
};
or
public class UserProfile
{
public string Name { get; set; }
[JsonConverter(typeof(NewtonsoftDataProtector))]
public string SensitiveInfo { get; set; }
}
// Example Data
var profile = new UserProfile
{
Name = "saied rahimi",
SensitiveInfo = "This is encrypted data"
};
Result
//encrypted result
{
"name": "saied rahimi",
"sensitiveInfo": "itNydZU4Y33CCPe/Bp45wOZwuI8CEofwCnO6m5VNhFs="
}
//decrypted result
{
"name": "saied rahimi",
"message": "This is encrypted data"
}
When serialized, SensitiveInfo will be automatically encrypted. Upon deserialization, it will be decrypted back to its original value.
Contributing
Contributions are welcome! Feel free to submit issues or pull requests to improve this library.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.