Blazor.SubtleCrypto 6.0.1

dotnet add package Blazor.SubtleCrypto --version 6.0.1
NuGet\Install-Package Blazor.SubtleCrypto -Version 6.0.1
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="Blazor.SubtleCrypto" Version="6.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Blazor.SubtleCrypto --version 6.0.1
#r "nuget: Blazor.SubtleCrypto, 6.0.1"
#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.
// Install Blazor.SubtleCrypto as a Cake Addin
#addin nuget:?package=Blazor.SubtleCrypto&version=6.0.1

// Install Blazor.SubtleCrypto as a Cake Tool
#tool nuget:?package=Blazor.SubtleCrypto&version=6.0.1

Blazor.SubtleCrypto

Provides services for encrypt and decrypt data. The data is protected using SubtleCrypto encrypt/decrypt methods and AES-GCM algorithm and returned in ciphertext. Because it uses JSInterop, this library can run on Blazor WebAssembly (client-side project) as well as Blazor Server.

Install

Install-Package Blazor.SubtleCrypto

To know the latest version: https://www.nuget.org/packages/Blazor.SubtleCrypto

How to use in Blazor WebAssembly & Blazor Server

Add to Program.cs

using Blazor.SubtleCrypto;

builder.Services.AddSubtleCrypto(opt => 
    opt.Key = "ELE9xOyAyJHCsIPLMbbZHQ7pVy7WUlvZ60y5WkKDGMSw5xh5IM54kUPlycKmHF9VGtYUilglL8iePLwr" //Use another key
);

Add this in your specific page .razor. If you need to encrypt/decrypt in diferent pages or components, add it in _Imports.razor

@using Blazor.SubtleCrypto
@inject ICryptoService Crypto

Encrypt/Decrypt

Converts textplain to a ciphertext and vice versa

CryptoResult encrypted = await Crypto.EncryptAsync("The krabby patty secret formula is...");
//encrypted.Value returns: emw4a0xHNDQ3WnppU9jxTBZ4SseC1/fkpm68N/SgzjpL6dlihEz8q8opjbc9OcE=

string decrypted = await Crypto.DecryptAsync(encrypted.Value);
//decrypted returns: "The krabby patty secret formula is..."

Converts object class to a ciphertext and vice versa

CryptoResult encrypted = await Crypto.EncryptAsync(new myModel{name="Bob", age=36});
//encrypted.Value returns: MFA5QlJsWDNlZXY0cEE8EI5O74iI/4pMr59Sd+VPHmnMcgs5pivb9FI4/CVfoX...

string decrypted = await Crypto.DecryptAsync(encrypted.Value);
//decrypted returns: '{"name":"Bob","age":36}'

myModel decrypted = await Crypto.DecryptAsync<myModel>(encrypted.Value);
//decrypted returns a populated myModel object

Converts a list of objects to a ciphertext and vice versa

CryptoResult encrypted = await Crypto.EncryptAsync(myListModel);
//encrypted.Value returns: OHRCMUJ2MDhLc2JTDerrg1DxyO0/Srcu3+JuG+Lp8MLTnsXjXXklxQ9zQ7jeN...

string decrypted = await Crypto.DecryptAsync(encrypted.Value);
//decrypted returns: '[{"name":"Bob","age":36},{"name":"Pat","age":38}]'

myModel decrypted = await Crypto.DecryptAsync<List<myModel>>(encrypted.Value);
//decrypted returns a populated List<myModel> object

Converts each string of a list into an individual ciphertext and vice versa

//To encrypt a list we use EncryptListAsync()
List<string> myList = new List<string> { "Bob", "Pat", "Don" };
List<CryptoResult> encrypted = await Crypto.EncryptListAsync(myList);

//To decrypt a list we use DecryptListAsync()
List<string> decrypted = await Crypto.DecryptListAsync(encrypted.Select(x=> x.Value).ToList());

//You can use DecryptListAsync<T>() to get a list of object
List<MyModel> decrypted = await Crypto.DecryptListAsync<MyModel>(encrypted.Select(x=> x.Value).ToList());

CryptoResult info: | Property | Description | | --- | --- | | Origin | Original data converted in string| | Secret | If there is no key in Program.cs, IV and Key will be generated automatically, otherwise they will be null. | | Status | Returns true if encryption/decryption was successfull | | Value | Returns ciphertext |

Encrypt/Decrypt with dynamic key

You can skip setting the key to Program.cs and each encryption will generate a different key and IV.<br /> In Program.cs

using Blazor.SubtleCrypto;

builder.Services.AddSubtleCrypto();

Example using a single string

CryptoResult encrypted = await Crypto.EncryptAsync("The krabby patty secret formula is...");
//encrypted.Value returns: WEs5SHJCMWN6ZlZsjN7KgUEJB6cu5eLy4tzORacAVmaX6F/xKQgLs0p20sHY=
//encrypted.Secret.Key returns: RRR0dYZ6eCHX91nzkNx9oTzW3j7FoDIC3Hu04LdKT4cKnpMBUdEhLVoj...

//To decrypt we need to ue CryptoInput model and set the ciphertext(Value) and the key
string decrypted = await Crypto.DecryptAsync(new CryptoInput { Value = encrypted.Value, Key = encrypted.Secret.Key});
//decrypted returns: "The krabby patty secret formula is..."

Example using a list of object

List<CryptoResult> encryptedList = await Crypto.EncryptListAsync(list);
//returns a list of CryptoResult each one with distinct Key & IV

//To decrypt we need to ue CryptoInput model and set the ciphertext(Value) and the key
List<CryptoInput> cryptoInputs = encryptedList.Select(x => new CryptoInput { Key = x.Secret.Key, Value = x.Value }).ToList();
List<MyModel> decrypted = await Crypto.DecryptListAsync<MyModel>(cryptoInputs);
//decrypted returns a populated List<myModel> object
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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on Blazor.SubtleCrypto:

Package Downloads
JadeAuth.SDK.Blazor

Package Description

EngramaCore

Tools to connect to Database, call APIs and so many tools to made faster the software development

CommonFuncion

Tools to connect to Database, call APIs and so many tools to made faster the software development

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.0.1 42,093 1/8/2022
6.0.0 305 1/7/2022