Takes a string and either encode or decode it using CryptoStream. "Pass Phrase" and "Salt" needs to be passed in.
$ dotnet add package String.Encoder.DecoderThe EncodeDecode class provides encryption and decryption functionality using AES (Advanced Encryption Standard) with a customizable initialization vector and passphrase. It is designed to securely encode and decode data using a salted encryption approach.
In your app settings add the configuration settings:
"EncodeDecodeConfiguration": {
"PasswordIterations": 2,
"Salt": "ThisIsYourSalt",
"InitVector": "@1B2c3D4e5F6g7H8",
"Passphrase": "DoNotUseForPasswords"
}
Register the service in your program.cs file
services.Configure<EncodeDecodeConfiguration>(context.Configuration.GetSection(EncodeDecodeConfiguration.Name));
services.AddScoped<IEncodeDecode, EncodeDecode>();
Inject the service where you need it:
private readonly IEncodeDecode _encryptor;
public YourClassConstructor(IEncodeDecode encrypt)
{
_encryptor = encrypt;
}
public void test()
{
Console.WriteLine("Enter Text");
var text = Console.ReadLine();
var encrypted = _encryptor.Encrypt(text);
var decrypted =_encryptor.Decrypt(encrypted);
Console.WriteLine(encrypted);
Console.WriteLine("****************");
Console.WriteLine(decrypted);
}
You can initialize the EncodeDecode class with the following parameters:
string passPhrase = "YourPassphrase"; // At least 8 characters
string salt = "YourSaltValue"; // At least 8 characters
string initVector = "@1B2c3D4e5F6g7H8"; // Must be exactly 16 characters
EncodeDecode encryptor = new EncodeDecode(passPhrase, salt, initVector);
var encrypted = encryptor.Encrypt(text);
var decrypted = encryptor.Decrypt(encrypted);
or using the hardcoded init vector which is less secure.
var encryptor = new EncodeDecode(PassPhraseString,SaltString);
var encrypted = encryptor.Encrypt(text);
var decrypted = encryptor.Decrypt(encrypted);