Snail.Toolkit.Blazor.Extensions.Cryptography 1.0.0

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

Toolkit.Blazor.Extensions.Cryptography

A Blazor extension providing JavaScript-compatible symmetric and asymmetric cryptography operations through Web Crypto API interop.

Features

  • Symmetric Encryption (AES-based)

    • Encrypt/decrypt strings with automatic Base64 conversion
    • Configurable passphrase, IV, and salt
    • JavaScript-compatible output
  • Asymmetric Encryption (RSA-OAEP)

    • Generate key pairs
    • Encrypt with public key
    • Decrypt with private key
    • Configurable modulus length and hash algorithm

Installation

Add the package to your Blazor project:

dotnet add package Snail.Toolkit.Blazor.Extensions.Cryptography

Configuration

Symmetric Encryption Setup

Choose one of these configuration methods:

  1. Programmatic configuration:
builder.Services.AddJsSymmetricCipher(o =>
{
    o.Passphrase = "secure-passphrase-here";
    o.IV = "unique-iv-value-16bytes";
    o.Salt = "unique-salt-value-16bytes";
    o.Iterations = 1000;
    o.DesiredKeyLength = 32; // 256 bits
    o.HashMethod = HashAlgo.SHA512;
});
  1. Configuration-based (appsettings.json):
builder.Services.AddJsSymmetricCipher(builder.Configuration);
  1. Default values (for development only):
builder.Services.AddJsSymmetricCipher();

Asymmetric Encryption Setup

builder.Services.AddJsAsymmetricCipher(o =>
{
    o.ModulusLengthInBits = 4096;
    o.HashMethod = HashAlgo.SHA512;
});

Sample appsettings.json

{
  "Cryptography": {
    "Passphrase": "secure-passphrase-here",
    "IV": "unique-iv-value-16bytes",
    "Salt": "unique-salt-value-16bytes",
    "Iterations": 1000,
    "DesiredKeyLength": 32,
    "HashMethod": "SHA512"
  }
}

Usage

Symmetric Encryption

public class MyService
{
    private readonly ISymmetricCipher _cipher;

    public MyService(ISymmetricCipher cipher)
    {
        _cipher = cipher;
    }

    public async Task<string> SecureOperation(string sensitiveData)
    {
        // Encrypt
        var encrypted = await _cipher.EncryptToBase64Async(sensitiveData);
        
        // Decrypt
        var decrypted = await _cipher.DecryptFromBase64Async(encrypted);
        
        return decrypted;
    }
}

Asymmetric Encryption

public class MySecureService
{
    private readonly IJsAsymmetricCipher _cipher;

    public MySecureService(IJsAsymmetricCipher cipher)
    {
        _cipher = cipher;
    }

    public async Task<(byte[] encrypted, string privateKey)> EncryptData(string data)
    {
        var result = await _cipher.EncryptAsync(data);
        return (result.EncryptedData, result.PrivateKey);
    }

    public async Task<string> DecryptData(string privateKey, byte[] encrypted)
    {
        return await _cipher.DecryptAsync(privateKey, encrypted);
    }
}

Security Considerations

Supported Hash Algorithms

  • SHA1 (legacy)
  • SHA256
  • SHA384
  • SHA512

Unsupported Algorithms (will throw exceptions)

  • MD5 (insecure)
  • SHA3 variants (not compatible with Web Crypto API)

Best Practices

  1. Always use strong passphrases (min 16 chars)
  2. Generate unique IVs for each encryption operation
  3. Store private keys securely
  4. Use at least SHA256 for hashing
  5. Prefer 4096-bit keys for asymmetric encryption

JavaScript Interop

The library uses these JavaScript modules from your wwwroot:

  • /_content/Snail.Toolkit.Blazor.Extensions.Cryptography/symmetric.js
  • /_content/Snail.Toolkit.Blazor.Extensions.Cryptography/asymmetric.js

License

Toolkit.Blazor.Extensions.Cryptography is a free and open source project, released under the permissible MIT license.

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 (1)

Showing the top 1 NuGet packages that depend on Snail.Toolkit.Blazor.Extensions.Cryptography:

Package Downloads
Snail.Toolkit.Blazor.Extensions.LocalStorage

Extension for the framework Blazor

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 86 5/31/2025