Json.Protector 1.2.0

dotnet add package Json.Protector --version 1.2.0
                    
NuGet\Install-Package Json.Protector -Version 1.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Json.Protector" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Json.Protector" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Json.Protector" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Json.Protector --version 1.2.0
                    
#r "nuget: Json.Protector, 1.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Json.Protector@1.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Json.Protector&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Json.Protector&version=1.2.0
                    
Install as a Cake Tool

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 134 1/14/2025
1.1.0 162 12/10/2024
1.0.0 160 11/16/2024