Encryption Functions provides a 1:1 mapping between an arbitrary length input and output and they are always reversible. The most important thing to note is that it's a reversible using method. And it's every time 1:1 for a given key. Now, they are multiple input: key pairs that might display the same output (in fact there usually are, depending on the encryption function). Good encrypted data is imperceptible from random noise.
.NET Core. encryption is fast and can encrypt or decrypt large amounts of Data, string, text, or files but requires a shared key. Asymmetric encryption can be used without shared a key, but can only encrypt or decrypt small texts depending on the key size.
Encrypting this method takes the three parameters and creates encrypted data that can only be decrypted using the same key and IV base64 strings. If encrypting large amounts of data, then a Crypto Stream should be used. See the example of Encrypt data in below code.
public string Encrypt(string txt, string IV, string key)
{
Aes cipher = CreateCipher(key);
cipher.IV = Convert.FromBase64String(IV);
ICryptoTransform cryptTransform = cipher.CreateEncryptor();
byte[] ptxt= Encoding.UTF8.GetBytes(txt);
byte[] ciptxt= cryptTransform.TransformFinalBlock(ptxt, 0, ptxt.Length);
return Convert.ToBase64String(ciptxt);
}
It is the common precondition to generate random strings. Not For only do they make outstanding primary keys (in many NoSQL data stores at least) but in addition, they are generally used for email validation and password reset process. Web and Mobile Developers often use a (modified) GUID for this:
Guid.NewGuid(). ToString("K")
That in returns a string similar like that: 84bc1c2db56140b39e35b040e6856457
This is often allowable but for a more random, more readable, and possibility shorter string we can come up with a better alternative:
public class RandomGen
{
private const string AllowG= "abcdefghijklmnopqrstuvwxyz0123456789";
public static string GenerateString(int LNG)
{
var bts= new byte[LNG];
using (var rndm= RandomNumberGenerator.Create())
{
random.GetBytes(bts);
}
return new string(bytes.Select(x => AllowableCharacters[x % AllowG.Length]). ToArray());
}
}
The key to this method is the use of the System.Security.Cryptography. random number generator. Create which returns a cryptographically strong random number generator by the random method. Random that class was returning the same pseudo-random numbers in the same order like random numbers given the same seed. The 'known' nature of the System. Random can be very useful in some difficult situations and security-related, the use of random number generator. Create is preferred.
The above example uses a base36 word but we can easily change that to your requirements. For example, if your end-users are expected to type in the string then you might want to remove characters that can be easily confused such as 0/o and 1/i.
If you want to create a random password that allows some specific characters also. The below example has a string of valid characters. The code uses this string to take one character at a time for the password and stops at the given length. In this example, we set the default length of the password as 12.
private static string CreateRandomPassword(int len= 12)
{
string validc= "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*?_-";
Random random = new Random();
char[] chars = new char[len];
for (int j = 0; j < len; j++)
{
chars[j] = validc[random.Next(0, validc.len)];
}
return new string(chars);
}