Uses of Salt in encrypting the Password using c# [Resolved]

Posted by Amatya under .NET Framework on 5/16/2015 | Points: 10 | Views : 1603 | Status : [Member] | Replies : 2
How to use Salt concept in converting the password in to encrypted format and again want to get that password into a string variable?
Please Share guys

Feel free to share informations.
mail Id ' adityagupta200@gmail.com
Thanks



Responses

Posted by: Rajeshatkiit on: 12/18/2015 [Member] Starter | Points: 50

Up
0
Down

Resolved
Plz find cryptography code for encryption and decryption .

I am giving example of License generation using encryption and decryption with salt.
Please let me know if you need any other help.

public class MyClass{


private static string salt = "Cool";


public static string GenerateLicense(string systemId)
{
string license = string.Empty;

systemId = salt + systemId;
license = Cryptography.Encrypt(systemId);
return license;
}

public static bool ValidateLicense(string licenseKey)
{


bool validLicense = false;
string key = string.Empty;

try
{
string license = Cryptography.Decrypt(licenseKey);
key = salt + Key();

if (key == license)
{
validLicense = true;
}
}
catch (Exception)
{
validLicense = false;
}



return validLicense;
}
}
public class Cryptography
{


static byte[] bytes = ASCIIEncoding.ASCII.GetBytes("ZeroCool");
/// <summary>
/// Encrypt a string.
/// </summary>
/// <param name="originalString">The original string.</param>
/// <returns>The encrypted string.</returns>
/// <exception cref="ArgumentNullException">This exception will be thrown when the original string is null or empty.</exception>
public static string Encrypt(string originalString)
{
if (String.IsNullOrEmpty(originalString))
{
throw new ArgumentNullException("The string which needs to be encrypted can not be null.");
}

DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);

StreamWriter writer = new StreamWriter(cryptoStream);
writer.Write(originalString);
writer.Flush();
cryptoStream.FlushFinalBlock();
writer.Flush();

return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}

/// <summary>
/// Decrypt a crypted string.
/// </summary>
/// <param name="cryptedString">The crypted string.</param>
/// <returns>The decrypted string.</returns>
/// <exception cref="ArgumentNullException">This exception will be thrown when the crypted string is null or empty.</exception>
public static string Decrypt(string cryptedString)
{
if (String.IsNullOrEmpty(cryptedString))
{
throw new ArgumentNullException("The string which needs to be decrypted can not be null.");
}

DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(cryptedString));
CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryptoStream);

return reader.ReadToEnd();
}
}


Amatya, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajeshatkiit on: 12/16/2015 [Member] Starter | Points: 25

Up
0
Down
Hi Amatya,
Please refer this link. It will help you.

http://www.codeproject.com/Articles/769741/Csharp-AES-bits-Encryption-Library-with-Salt/

Please let me know if you need any clarifications.

Amatya, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response