In ASP.NET Core, you can generate a 32-byte symmetric key for encryption purposes using various cryptographic libraries.
One commonly used library is the System.Security.Cryptography namespace.
Symmetric key encryption algorithms, such as AES, commonly use 256 bits (32 bytes) keys.
symmetricKeyValue = generateAlphaNumericKey(32); // "32 bytes symmetrickey";
static String generateAlphaNumericKey(int keySize)
{
int VAR1 = 'a';
int VAR2 = 'z';
int VAR3 = 'A';
int VAR4 = 'Z';
int ZERO = '0';
int NINE = '9';
Random random = new Random();
StringBuilder key = new StringBuilder();
while ((key.Length < keySize))
{
int character = random.Next(128);
// is within range
if ((((character <= VAR2)
&& (character >= VAR1))
|| (((character <= VAR4)
&& (character >= VAR3))
|| ((character <= NINE)
&& (character >= ZERO)))))
{
key.Append(((char)(character)));
}
}
return key.ToString();
}
Output like:
bgzlNm3wFEMzjKjzpab94ZTss3gXqGo9
0 Comments