set Key in Webconfig
============================================================
<add key="PasswordLength" value="6"/>
===========================================================
string OTP = GenerateRandom.RandomOTP(Convert.ToInt32(ConfigurationManager.AppSettings["PasswordLength"]));
=================
using Encrypt
=================
string Password = EncriptDecript.Encrypt(GenerateRandom.RandomPassword(Convert.ToInt32(ConfigurationManager.AppSettings["PasswordLength"])));
=============================================================
Create a GenerateRandom Class
============================================================
public class GenerateRandom
{
public static Random rnd = new Random();
public static string RandomUserName(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[rnd.Next(s.Length)]).ToArray());
}
public static string RandomEmailid(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[rnd.Next(s.Length)]).ToArray());
}
public static string RandomPassword(int length)
{
const string chars = "0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[rnd.Next(s.Length)]).ToArray());
}
public static string RandomOTP(int length)
{
const string chars = "0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[rnd.Next(s.Length)]).ToArray());
}
}
==============================
EncriptDecript
public class EncriptDecript
{
#region Encode the string value
public static string Encrypt(string clearText)
{
string EncryptionKey = "MAKV2SPBNI99212";
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
return clearText;
}
#endregion
#region Decode the encoded value
public static string Decrypt(string cipherText)
{
string EncryptionKey = "MAKV2SPBNI99212";
cipherText = cipherText.Replace(" ", "+");
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}
#endregion
}
============================================================
<add key="PasswordLength" value="6"/>
===========================================================
string OTP = GenerateRandom.RandomOTP(Convert.ToInt32(ConfigurationManager.AppSettings["PasswordLength"]));
=================
using Encrypt
=================
string Password = EncriptDecript.Encrypt(GenerateRandom.RandomPassword(Convert.ToInt32(ConfigurationManager.AppSettings["PasswordLength"])));
=============================================================
Create a GenerateRandom Class
============================================================
public class GenerateRandom
{
public static Random rnd = new Random();
public static string RandomUserName(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[rnd.Next(s.Length)]).ToArray());
}
public static string RandomEmailid(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[rnd.Next(s.Length)]).ToArray());
}
public static string RandomPassword(int length)
{
const string chars = "0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[rnd.Next(s.Length)]).ToArray());
}
public static string RandomOTP(int length)
{
const string chars = "0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[rnd.Next(s.Length)]).ToArray());
}
}
==============================
EncriptDecript
public class EncriptDecript
{
#region Encode the string value
public static string Encrypt(string clearText)
{
string EncryptionKey = "MAKV2SPBNI99212";
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
return clearText;
}
#endregion
#region Decode the encoded value
public static string Decrypt(string cipherText)
{
string EncryptionKey = "MAKV2SPBNI99212";
cipherText = cipherText.Replace(" ", "+");
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}
#endregion
}
No comments:
Post a Comment