public class Password
{
public static string CreateRandomPassword(int Length)
{
Random rnd = new Random(Convert.ToInt32(DateTime.Now.Millisecond)); //Creates the seed from the time
string Password = "";
while (Password.Length < Length)
{
char newChar = Convert.ToChar((int)((122 - 48 + 1) * rnd.NextDouble() + 48));
if ((((int)newChar) >= ((int)'A')) & (((int)newChar) <= ((int)'Z')) | (((int)newChar) >= ((int)'a')) & (((int)newChar) <= ((int)'z')) | (((int)newChar) >= ((int)'0')) & (((int)newChar) <= ((int)'9')))
Password += newChar;
}
return Password;
}
public static string GetMd5Hash(string value)
{
var md5Hasher = MD5.Create();
var data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(value));
var sBuilder = new StringBuilder();
for (var i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
const string passphrase = "Password@123"; //consant string Pass key
public string encrypt(string message)
{
byte[] results;
UTF8Encoding utf8 = new UTF8Encoding();
//to create the object for UTF8Encoding class
//TO create the object for MD5CryptoServiceProvider
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] deskey = md5.ComputeHash(utf8.GetBytes(passphrase));
//to convert to binary passkey
//TO create the object for TripleDESCryptoServiceProvider
TripleDESCryptoServiceProvider desalg = new TripleDESCryptoServiceProvider();
desalg.Key = deskey;//to pass encode key
desalg.Mode = CipherMode.ECB;
desalg.Padding = PaddingMode.PKCS7;
byte[] encrypt_data = utf8.GetBytes(message);
//to convert the string to utf encoding binary
try
{
//To transform the utf binary code to md5 encrypt
ICryptoTransform encryptor = desalg.CreateEncryptor();
results = encryptor.TransformFinalBlock(encrypt_data, 0, encrypt_data.Length);
}
finally
{
//to clear the allocated memory
desalg.Clear();
md5.Clear();
}
//to convert to 64 bit string from converted md5 algorithm binary code
return Convert.ToBase64String(results);
}
public string decrypt(string message)
{
byte[] results;
UTF8Encoding utf8 = new UTF8Encoding();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] deskey = md5.ComputeHash(utf8.GetBytes(passphrase));
TripleDESCryptoServiceProvider desalg = new TripleDESCryptoServiceProvider();
desalg.Key = deskey;
desalg.Mode = CipherMode.ECB;
desalg.Padding = PaddingMode.PKCS7;
byte[] decrypt_data = Convert.FromBase64String(message);
try
{
//To transform the utf binary code to md5 decrypt
ICryptoTransform decryptor = desalg.CreateDecryptor();
results = decryptor.TransformFinalBlock(decrypt_data, 0, decrypt_data.Length);
}
finally
{
desalg.Clear();
md5.Clear();
}
//TO convert decrypted binery code to string
return utf8.GetString(results);
}
}
{
public static string CreateRandomPassword(int Length)
{
Random rnd = new Random(Convert.ToInt32(DateTime.Now.Millisecond)); //Creates the seed from the time
string Password = "";
while (Password.Length < Length)
{
char newChar = Convert.ToChar((int)((122 - 48 + 1) * rnd.NextDouble() + 48));
if ((((int)newChar) >= ((int)'A')) & (((int)newChar) <= ((int)'Z')) | (((int)newChar) >= ((int)'a')) & (((int)newChar) <= ((int)'z')) | (((int)newChar) >= ((int)'0')) & (((int)newChar) <= ((int)'9')))
Password += newChar;
}
return Password;
}
public static string GetMd5Hash(string value)
{
var md5Hasher = MD5.Create();
var data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(value));
var sBuilder = new StringBuilder();
for (var i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
const string passphrase = "Password@123"; //consant string Pass key
public string encrypt(string message)
{
byte[] results;
UTF8Encoding utf8 = new UTF8Encoding();
//to create the object for UTF8Encoding class
//TO create the object for MD5CryptoServiceProvider
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] deskey = md5.ComputeHash(utf8.GetBytes(passphrase));
//to convert to binary passkey
//TO create the object for TripleDESCryptoServiceProvider
TripleDESCryptoServiceProvider desalg = new TripleDESCryptoServiceProvider();
desalg.Key = deskey;//to pass encode key
desalg.Mode = CipherMode.ECB;
desalg.Padding = PaddingMode.PKCS7;
byte[] encrypt_data = utf8.GetBytes(message);
//to convert the string to utf encoding binary
try
{
//To transform the utf binary code to md5 encrypt
ICryptoTransform encryptor = desalg.CreateEncryptor();
results = encryptor.TransformFinalBlock(encrypt_data, 0, encrypt_data.Length);
}
finally
{
//to clear the allocated memory
desalg.Clear();
md5.Clear();
}
//to convert to 64 bit string from converted md5 algorithm binary code
return Convert.ToBase64String(results);
}
public string decrypt(string message)
{
byte[] results;
UTF8Encoding utf8 = new UTF8Encoding();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] deskey = md5.ComputeHash(utf8.GetBytes(passphrase));
TripleDESCryptoServiceProvider desalg = new TripleDESCryptoServiceProvider();
desalg.Key = deskey;
desalg.Mode = CipherMode.ECB;
desalg.Padding = PaddingMode.PKCS7;
byte[] decrypt_data = Convert.FromBase64String(message);
try
{
//To transform the utf binary code to md5 decrypt
ICryptoTransform decryptor = desalg.CreateDecryptor();
results = decryptor.TransformFinalBlock(decrypt_data, 0, decrypt_data.Length);
}
finally
{
desalg.Clear();
md5.Clear();
}
//TO convert decrypted binery code to string
return utf8.GetString(results);
}
}