docs/en/framework/infrastructure/string-encryption.md
//[doc-seo]
{
"Description": "Learn how to securely encrypt and decrypt strings in your ABP applications with easy installation and usage guides."
}
ABP provides string encryption feature that allows to Encrypt and Decrypt strings.
This package is already installed by default with the startup template. So, most of the time, you don't need to install it manually.
If installation is needed, it is suggested to use the ABP CLI to install this package.
Open a command line window in the folder of the project (.csproj file) and type the following command:
abp add-package Volo.Abp.Security
If you want to manually install;
Add the Volo.Abp.Security NuGet package to your project:
dotnet add package Volo.Abp.Security
Add the AbpSecurityModule to the dependency list of your module:
[DependsOn(
//...other dependencies
typeof(AbpSecurityModule) // <-- Add module dependency like that
)]
public class YourModule : AbpModule
{
}
All encryption operations are included in IStringEncryptionService. You can inject it and start to use.
public class MyService : DomainService
{
protected IStringEncryptionService StringEncryptionService { get; }
public MyService(IStringEncryptionService stringEncryptionService)
{
StringEncryptionService = stringEncryptionService;
}
public string Encrypt(string value)
{
// To encrypt a value
return StringEncryptionService.Encrypt(value);
}
public string Decrypt(string value)
{
// To decrypt a value
return StringEncryptionService.Decrypt(value);
}
}
IStringEncryptionService methods has passPharase parameter with default value and it uses default PassPhrase when you don't pass passPhrase parameter.
// Default Pass Phrase
var encryptedValue = StringEncryptionService.Encrypt(value);
// Custom Pass Phrase
var encryptedValue = StringEncryptionService.Encrypt(value, "MyCustomPassPhrase");
// Encrypt & Decrypt have same parameters.
var decryptedValue = StringEncryptionService.Decrypt(value, "MyCustomPassPhrase");
IStringEncryptionService methods has salt parameter with default value and it uses default Salt when you don't pass the parameter.
// Default Salt
var encryptedValue = StringEncryptionService.Encrypt(value);
// Custom Salt
var encryptedValue = StringEncryptionService.Encrypt(value, salt: Encoding.UTF8.GetBytes("MyCustomSalt"));
// Encrypt & Decrypt have same parameters.
var decryptedValue = StringEncryptionService.Decrypt(value, salt: Encoding.UTF8.GetBytes("MyCustomSalt"));
Default values can be configured with AbpStringEncryptionOptions type.
Configure<AbpStringEncryptionOptions>(opts =>
{
opts.DefaultPassPhrase = "MyStrongPassPhrase";
opts.DefaultSalt = Encoding.UTF8.GetBytes("MyStrongSalt");
opts.InitVectorBytes = Encoding.UTF8.GetBytes("My16ByteInitVect");
opts.Keysize = 256;
});
DefaultPassPhrase: Default password to encrypt/decrypt texts. It's recommended to set to another value for security. Default value: gsKnGZ041HLL4IM8
DefaultSalt: A value which is used as salt while encrypting/decrypting.
Default value: Encoding.ASCII.GetBytes("hgt!16kl")
InitVectorBytes: The initialization vector used by AES. It must be exactly 16 bytes, regardless of the configured key size.
Default value: Encoding.ASCII.GetBytes("jkE49230Tf093b42")
Keysize: The AES key size in bits. Use a key size supported by AES: 128, 192, or 256.
Default value: 256