Blazor.SubtleCrypto 9.0.0

dotnet add package Blazor.SubtleCrypto --version 9.0.0
                    
NuGet\Install-Package Blazor.SubtleCrypto -Version 9.0.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="Blazor.SubtleCrypto" Version="9.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Blazor.SubtleCrypto" Version="9.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Blazor.SubtleCrypto" />
                    
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 Blazor.SubtleCrypto --version 9.0.0
                    
#r "nuget: Blazor.SubtleCrypto, 9.0.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 Blazor.SubtleCrypto@9.0.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=Blazor.SubtleCrypto&version=9.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Blazor.SubtleCrypto&version=9.0.0
                    
Install as a Cake Tool

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 net9.0 is compatible.  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 (4)

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

Package Downloads
JadeAuth.SDK.Blazor

Package Description

EngramaCore

Galeria de funciones y procesos para facilar el desarrollo de software. Consulta la base de datos, consulta APIs, genera documentos, trabaja con Json, todo lo que se usa dia a dia en un solo Nuget.

CommonFuncion

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

Boxty.ClientBase

Client-side Blazor components and services for building multi-tenant applications

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
9.0.0 7,701 5/8/2025
8.0.0 3,825 5/8/2025
7.0.0 295 5/8/2025
6.0.1 103,856 1/8/2022
6.0.0 711 1/7/2022 6.0.0 is deprecated.