https://github.com/fkucukkara/dataprotector101
This project demonstrates how to use ASP.NET Core Data Protection to securely encrypt and decrypt data
https://github.com/fkucukkara/dataprotector101
data-protect minimal-api netcore-webapi
Last synced: 11 months ago
JSON representation
This project demonstrates how to use ASP.NET Core Data Protection to securely encrypt and decrypt data
- Host: GitHub
- URL: https://github.com/fkucukkara/dataprotector101
- Owner: fkucukkara
- License: other
- Created: 2025-03-23T14:21:24.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-03-23T14:25:13.000Z (11 months ago)
- Last Synced: 2025-03-23T15:30:36.858Z (11 months ago)
- Topics: data-protect, minimal-api, netcore-webapi
- Language: C#
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ASP.NET Core Data Protection API
This project demonstrates how to use **ASP.NET Core Data Protection** to securely encrypt and decrypt data. It provides simple `/protect` and `/unprotect` endpoints to manage sensitive information.
## ๐ ๏ธ Features
- Securely **protect** (encrypt) and **unprotect** (decrypt) sensitive data.
- Supports **HTTPS** for secure data transfer.
- Customizable **data protection purpose** for scoped encryption.
- Cloud-ready with support for **persistent key storage** (e.g., Azure Blob, AWS S3).
## ๐ Requirements
- .NET 8 or later
- ASP.NET Core
## ๐ Getting Started
### 1. Clone the repository:
```bash
git clone https://github.com/fkucukkara/dataProtector101.git
cd aspnetcore-data-protection
```
### 2. Run the application:
```bash
dotnet run
```
The application will be available at `https://localhost:5001` (or the default port).
## ๐ API Endpoints
### โค **Protect Data**
Encrypts the provided data securely.
```http
POST /protect
```
**Query Parameters:**
- `data` (string, required) โ The data to be encrypted.
**Example Request:**
```bash
curl -X POST "https://localhost:5001/protect?data=HelloWorld"
```
**Response:**
```json
{
"ProtectedData": "CfDJ8M8...=="
}
```
### โค **Unprotect Data**
Decrypts previously protected data.
```http
POST /unprotect
```
**Query Parameters:**
- `data` (string, required) โ The encrypted data to be decrypted.
**Example Request:**
```bash
curl -X POST "https://localhost:5001/unprotect?data=CfDJ8M8...=="
```
**Response:**
```json
{
"UnprotectedData": "HelloWorld"
}
```
## ๐ง Customization
### โค Set Application Name
Ensure consistent key sharing across different applications.
```csharp
builder.Services.AddDataProtection()
.SetApplicationName("MySecureApp");
```
### โค Persist Keys to File System
Store encryption keys securely on disk.
```csharp
using System.IO;
builder.Services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo("/var/keys"))
.SetDefaultKeyLifetime(TimeSpan.FromDays(90));
```
### โค Custom Protectors
Use different **purposes** for better data isolation.
```csharp
var protector = provider.CreateProtector("CustomPurpose");
```
## โ๏ธ Cloud Integration
### โค Azure Blob Storage
```csharp
using Azure.Storage.Blobs;
builder.Services.AddDataProtection()
.PersistKeysToAzureBlobStorage(new Uri(""));
```
### โค AWS S3
```csharp
using Amazon.S3;
var s3Client = new AmazonS3Client();
builder.Services.AddDataProtection()
.PersistKeysToAWS(s3Client, "bucket-name", "keys-folder");
```
## ๐งน Cleaning Up
To **revoke** old keys, delete the key files from the storage location.
## ๐ Notes
1. Always use **HTTPS** in production.
2. Use different **purposes** for better data isolation.
3. Regularly rotate encryption keys using `SetDefaultKeyLifetime()`.
## ๐ Resources
- [Microsoft Docs: Data Protection](https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/)
## ๐ฌ Contribution
Feel free to open issues or contribute via pull requests.
## License
[](LICENSE)
This project is licensed under the MIT License. See the [`LICENSE`](LICENSE) file for details.