A secure .env file encryption tool for C#/.NET developers with automatic MSBuild integration.
- Accidental commits: Encrypt your .env files so secrets never leak into git
- Team sharing: Safely share encrypted configuration with your team
- Environment security: Keep secrets encrypted at rest
- Build automation: Automatic encryption/decryption during builds
Perfect for teams and automatic workflows:
# Add MSBuild package to your project
dotnet add package DotEnv.SecretManager.MSBuildEnable automatic encryption in your project file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<!-- Enable automatic .env encryption during builds -->
<AutoEncryptEnv>true</AutoEncryptEnv>
</PropertyGroup>
</Project>Set your encryption password:
# Via environment variable (recommended)
export ENV_ENCRYPTION_PASSWORD=your-secure-password
# Or via MSBuild property
dotnet build -p:EnvEncryptionPassword=your-secure-passwordFor individual developers who prefer manual control:
# Install the global tool
dotnet tool install -g --add-source ./bin/Release DotEnvSecretManager
# Use anywhere
dotenv-encrypt encrypt .envFor programmatic use in your applications:
# Add to your project
dotnet add package DotEnv.SecretManager.Coregit clone https://github.com/iiSmitty/envcrypt
cd DotEnvSecretManager
dotnet build
dotnet run --project DotEnv.SecretManager.CLI encrypt .envTransform your development workflow with zero-effort encryption:
Before (Manual):
vim .env # Edit secrets
dotenv-encrypt encrypt .env # Remember to encrypt
git add .env.enc # Manual process
git commit -m "Update config"After (Automatic):
vim .env # Edit secrets
dotnet build # Encryption happens automatically
git add .env.enc # Only encrypted files exist
git commit -m "Update config" # Zero extra stepsNew team member setup:
git clone company-repo
cd awesome-app
dotnet build # Password from environment variable, auto-decrypts
dotnet run # App starts with correct configurationGitHub Actions:
- name: Build and Deploy
run: dotnet build -p:EnvEncryptionPassword=${{ secrets.ENV_PASSWORD }}
env:
ENV_ENCRYPTION_PASSWORD: ${{ secrets.ENV_PASSWORD }}
# Automatic decryption during build, deploys with correct configAzure DevOps:
- task: DotNetCoreCLI@2
inputs:
command: 'build'
arguments: '-p:EnvEncryptionPassword=$(EnvironmentPassword)'Environment-specific encryption:
<PropertyGroup Condition="'$(Configuration)' == 'Development'">
<EnvInputFile>.env.development</EnvInputFile>
<EnvOutputFile>.env.development.enc</EnvOutputFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Production'">
<EnvInputFile>.env.production</EnvInputFile>
<EnvOutputFile>.env.production.enc</EnvOutputFile>
</PropertyGroup>Build for specific environments:
dotnet build -c Development # Uses .env.development ? .env.development.enc
dotnet build -c Production # Uses .env.production ? .env.production.enc<PropertyGroup>
<!-- Custom file paths -->
<EnvInputFile>$(ProjectDir)config/.env</EnvInputFile>
<EnvOutputFile>$(ProjectDir)config/.env.encrypted</EnvOutputFile>
<!-- Conditional automation -->
<AutoEncryptEnv Condition="'$(Configuration)' == 'Release'">true</AutoEncryptEnv>
<AutoDecryptEnv Condition="'$(CI)' == 'true'">true</AutoDecryptEnv>
<!-- Password from different sources -->
<EnvEncryptionPassword Condition="'$(EnvEncryptionPassword)' == ''">$(ENV_ENCRYPTION_PASSWORD)</EnvEncryptionPassword>
</PropertyGroup># Basic encryption (prompts for password)
dotenv-encrypt encrypt .env
# Custom output file
dotenv-encrypt encrypt .env --output .env.production.enc
# Force overwrite existing files
dotenv-encrypt encrypt .env --force# Basic decryption (prompts for password)
dotenv-encrypt decrypt .env.enc
# Custom output file
dotenv-encrypt decrypt .env.enc --output .env.local
# Force overwrite
dotenv-encrypt decrypt .env.enc --force# Validate with password prompt
dotenv-encrypt validate .env.encdotenv-encrypt help # General help# Add package to your project
dotnet add package DotEnv.SecretManager.MSBuild
# Edit your .csproj to enable auto-encryption
echo '<AutoEncryptEnv>true</AutoEncryptEnv>' # Add to PropertyGroup
# Create test .env file
echo "API_KEY=sk_test_123456789" > .env
echo "DB_PASSWORD=super_secret_password" >> .env
# Set password and build
export ENV_ENCRYPTION_PASSWORD=testpassword123
dotnet build
# Check results - .env.enc should be created automatically
ls -la .env*# Install CLI tool
dotenv tool install -g DotEnvSecretManager
# Create sample .env file
echo "API_KEY=test-secret-key-123" > .env
echo "DB_PASSWORD=super-secret-password" >> .env
# Encrypt the file
dotenv-encrypt encrypt .env
# Verify it worked
dotenv-encrypt validate .env.enc
# Decrypt when needed
dotenv-encrypt decrypt .env.enc --output .env.local# Developer A: Encrypt and commit
dotenv-encrypt encrypt .env
git add .env.enc
git commit -m "Add encrypted environment config"
git push
# Developer B: Pull and decrypt
git pull
dotenv-encrypt decrypt .env.enc --output .env.local
# Uses .env.local for developmentThis project provides three complementary packages:
| Package | Use Case | Installation |
|---|---|---|
DotEnv.SecretManager.MSBuild |
Automatic build integration | dotnet add package |
DotEnvSecretManager |
Manual CLI encryption | dotnet tool install -g |
DotEnv.SecretManager.Core |
Programmatic library use | dotnet add package |
| Scenario | Recommended Package | Why |
|---|---|---|
| Team development | DotEnv.SecretManager.MSBuild |
Zero setup, automatic, consistent |
| CI/CD pipelines | DotEnv.SecretManager.MSBuild |
One build command handles everything |
| New projects | DotEnv.SecretManager.MSBuild |
Set-and-forget automation |
| Individual manual use | DotEnvSecretManager (CLI) |
Full control, no project changes |
| Library integration | DotEnv.SecretManager.Core |
Programmatic access |
- AES-256-CBC encryption with PBKDF2 key derivation
- 10,000 iterations for key strengthening
- Random salt and IV for each encryption
- Base64 encoding for safe text storage
- Password confirmation for encryption operations
- Secure password prompting with masked input
.env.enc,.env.production.enc(encrypted files)README.md, source code, tests
.env
.env.local
.env.development
.env.production
*.decrypted
decrypted.env
*.backup.*- Use strong, unique passwords for each project
- Store passwords in a secure password manager
- Consider using key derivation from master passwords
- Rotate passwords regularly in production
- Always add
.env*to.gitignore(except.encfiles) - Delete decrypted files after use in CI/CD
- Create backups of original files before encryption
- Use environment-specific encrypted files
- Share encrypted files via git safely
- Share passwords via secure channels (not Slack/email)
- Use different passwords for different environments
- Document the encryption/decryption process for your team
- Store passwords in secure CI/CD secrets
- Use environment-specific configurations
- Clean up decrypted files after deployment
- Audit who has access to encryption passwords
using DotEnv.SecretManager.Core.Services;
// Setup services
var encryptionService = new AesEncryptionService();
var fileService = new EnvFileService();
var secretManager = new SecretManager(encryptionService, fileService);
// Encrypt a file
var result = await secretManager.EncryptFileAsync(".env", "password");
if (result.Success)
{
Console.WriteLine($"Encrypted {result.ProcessedEntries} entries");
}
// Direct string encryption
string encrypted = await encryptionService.EncryptAsync("secret-value", "password");
string decrypted = await encryptionService.DecryptAsync(encrypted, "password");dotnet test
# All tests should passdotnet build
dotnet packDotEnvSecretManager/
??? DotEnv.SecretManager.Core/ # Core library
??? DotEnv.SecretManager.CLI/ # CLI tool
??? DotEnv.SecretManager.MSBuild/ # MSBuild integration
??? DotEnv.SecretManager.Tests/ # Unit tests
??? README.md
??? .gitignore
- v1.2.0 - Added MSBuild integration for automatic encryption
- v1.1.0 - Enhanced CLI with info command, better UX, improved architecture
- v1.0.0 - Initial release with core encryption/decryption functionality
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
MIT License - feel free to use this in your projects!
- Issues: Report bugs and feature requests on GitHub
- Documentation: Check the examples and help commands
- Security: Report security issues privately to maintainers