PublicKeyUtils 10.0.0
dotnet add package PublicKeyUtils --version 10.0.0
NuGet\Install-Package PublicKeyUtils -Version 10.0.0
<PackageReference Include="PublicKeyUtils" Version="10.0.0" />
<PackageVersion Include="PublicKeyUtils" Version="10.0.0" />
<PackageReference Include="PublicKeyUtils" />
paket add PublicKeyUtils --version 10.0.0
#r "nuget: PublicKeyUtils, 10.0.0"
#:package PublicKeyUtils@10.0.0
#addin nuget:?package=PublicKeyUtils&version=10.0.0
#tool nuget:?package=PublicKeyUtils&version=10.0.0
PublicKeyUtils
PublicKeyUtils is an open-source .NET class library designed to simplify working with public keys for cryptographic operations. It provides utilities for encryption, decryption, signing, and verification using public keys. The library is built with extensibility and ease of use in mind, making it a great choice for developers working with RSA and ECC cryptography.
Table of Contents
Features
RSA Public Key Operations:
- Encrypt data using RSA public keys with support for various padding schemes (RSA-OAEP with SHA-1, SHA-256, SHA-384, SHA-512)
- Decode and parse RSA public key components (modulus and exponent) from Base64URL-encoded strings
- Key operation validation to ensure encryption is only performed when allowed
ECC Public Key Operations:
- Verify digital signatures using ECC public keys
- Support for named curves such as P-256, P-384, and P-521
- Hash algorithm support for SHA-1, SHA-256, SHA-384, and SHA-512
JSON Serialization:
- Public key properties are annotated with
System.Text.Json.Serializationattributes for seamless JSON serialization and deserialization - Compatible with JWK (JSON Web Key) format
- Public key properties are annotated with
Unit Tests:
- Comprehensive test coverage using xUnit to ensure correctness and reliability
- All cryptographic operations thoroughly tested
Installation
The library is available as a NuGet package. You can install it using one of the following methods:
.NET CLI
dotnet add package PublicKeyUtils
Package Manager Console
Install-Package PublicKeyUtils
Package Reference
<PackageReference Include="PublicKeyUtils" Version="*" />
For more details, visit the NuGet package page.
Usage Examples
RSA Encryption
Encrypt data using an RSA public key with different padding schemes:
using PublicKeyUtils.CryptoKeys;
// Create an RSA public key for encryption
var publicKey = new EncryptDecryptPublicKey
{
Algorithm = "RSA-OAEP-256", // Use RSA-OAEP with SHA-256
E = "AQAB", // Public exponent (65537)
N = "sXch9W6_K8oZn3PfJPZepKvXYTwc_nPIu9JrYfPRzM8zqVnD3edGldHTebAiN4MbcDkN5q1nbRV69BQ1_lwPt6b92_l6dcH0QJebQqovExY16Y7bQO02NGqjc8tkFPAeqC1cgI2VmojzG3FeAWqxtj5Ez5g0PYYJgxIoEXRopv9N2V-DME4mXwMxf3NVZ9d73Sm1Tb9p_U1OwQuWCh0p4kJHDsh44yBdM37KMLWSLM6pEr7jeWyzX0d1sKdfbORaVq0f1uzjZ_3iM_Oey7GMJKkPGYQlQjWbL2iyHv5PeAxJmZLykB0CZ0oUzOGlYfKJhL1x_j1D_zFckvj7o0K9GQ", // Modulus (Base64URL)
KeyOps = new[] { "encrypt" } // Allowed operations
};
// Encrypt plaintext
string plaintext = "Hello, World!";
byte[] ciphertext = publicKey.Encrypt(plaintext);
Console.WriteLine($"Encrypted data: {Convert.ToBase64String(ciphertext)}");
Supported RSA Padding Schemes:
RSA-OAEP- RSA-OAEP with SHA-1RSA-OAEP-256- RSA-OAEP with SHA-256RSA-OAEP-384- RSA-OAEP with SHA-384RSA-OAEP-512- RSA-OAEP with SHA-512
ECC Signature Verification
Verify digital signatures using an ECC public key:
using PublicKeyUtils.CryptoKeys;
// Create an ECC public key for signature verification
var publicKey = new SignVerifyPublicKey
{
Algorithm = "ES256", // ECDSA with SHA-256
Curve = "P-256", // Named curve
X = "WKn-ZIGevcwGIyyrzFoZNBdaq9_TsqzGl96oc0CWuis", // X coordinate (Base64URL)
Y = "y77t-RvAHRKTsSGdIYUfweuOvwrvDD-Q3Hv5J0fSKbE", // Y coordinate (Base64URL)
KeyOps = new[] { "verify" } // Allowed operations
};
// Data and signature to verify
string data = "Important message";
byte[] signature = Convert.FromBase64String("MEUCIQDKZokqnCjrRtw0...");
// Verify the signature
bool isValid = publicKey.Verify(data, signature);
Console.WriteLine($"Signature valid: {isValid}");
Supported ECC Algorithms:
ES256- ECDSA with SHA-256ES384- ECDSA with SHA-384ES512- ECDSA with SHA-512
Supported Algorithms
RSA Encryption Padding Schemes
| Algorithm | Description | Hash Function |
|---|---|---|
RSA-OAEP |
RSA-OAEP padding | SHA-1 |
RSA-OAEP-256 |
RSA-OAEP padding | SHA-256 |
RSA-OAEP-384 |
RSA-OAEP padding | SHA-384 |
RSA-OAEP-512 |
RSA-OAEP padding | SHA-512 |
ECC Curves
| Curve | Key Size | Description |
|---|---|---|
P-256 |
256-bit | NIST P-256 (secp256r1) |
P-384 |
384-bit | NIST P-384 (secp384r1) |
P-521 |
521-bit | NIST P-521 (secp521r1) |
Hash Algorithms
- SHA-1 (legacy support)
- SHA-256 (recommended)
- SHA-384
- SHA-512
Contributing
Contributions are welcome! If you have ideas for new features, bug fixes, or improvements, feel free to:
- Open an Issue: Report bugs or suggest features on the GitHub Issues page
- Submit a Pull Request: Fork the repository, make your changes, and submit a PR
- Improve Documentation: Help enhance the documentation and examples
Please ensure all tests pass before submitting a pull request:
dotnet test
CI/CD
This project uses GitHub Actions for continuous integration and deployment. On every push to a version tag (e.g., v2.0.0), the library is automatically:
- Built and tested
- Packaged as a NuGet package
- Published to NuGet.org
See the release workflow for more details.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Acknowledgments
Special thanks to all contributors and the open-source community for their support and feedback.
Links:
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.