Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate random salt #602

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/Orc.DataAccess/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

public static class StringExtensions
{
private static byte[]? _salt;
private const int Keysize = 256; // This constant is used to determine the keysize of the encryption algorithm.
public const string InitVector = "tu89geji340t89u2";

Expand All @@ -18,7 +19,13 @@ public static string Encrypt(this string plainText) /////to encrypt password
var initVectorBytes = Encoding.UTF8.GetBytes(InitVector);
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);

using var password = new Rfc2898DeriveBytes(passPhrase, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 });
_salt ??= new byte[8];
using (var randomGenerator = RandomNumberGenerator.Create())
{
randomGenerator.GetBytes(_salt);
}

using var password = new Rfc2898DeriveBytes(passPhrase, _salt);
var keyBytes = password.GetBytes(Keysize / 8);
using var symmetricKey = Aes.Create();
var encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes); ////To encrypt
Expand All @@ -44,7 +51,13 @@ public static string Encrypt(this string plainText) /////to encrypt password
var initVectorBytes = Encoding.ASCII.GetBytes(InitVector);
var cipherTextBytes = Convert.FromBase64String(cipherText);

using var password = new Rfc2898DeriveBytes(passPhrase, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 });
_salt ??= new byte[8];
using (var randomGenerator = RandomNumberGenerator.Create())
{
randomGenerator.GetBytes(_salt);
}

using var password = new Rfc2898DeriveBytes(passPhrase, _salt);
var keyBytes = password.GetBytes(Keysize / 8);
using var symmetricKey = Aes.Create();
var decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
Expand Down