Click here to Skip to main content
16,022,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to encrypt password and user name in c sharp.
Posted
Comments
George Jonsson 9-Sep-14 0:42am    
So what have you tried so far?

Hello friend,

Try this.. it will work for password...



byte[] newByte = Encrypt(txtPwd.Text);

public static byte[] Encrypt(string plainText)
{
string passPhrase = "Bl@@dB@nk"; // can be any string
string saltValue = "EmerGENcy"; // can be any string
string hashAlgorithm = "SHA1"; // can be "MD5"
int passwordIterations = 2; // can be any number
string initVector = "@1B2c3D4e5F6g7H8"; // must be 16 bytes
int keySize = 256; // can be 192 or 128
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
//string cipherText = Convert.ToBase64String(cipherTextBytes);
return cipherTextBytes;
}
 
Share this answer
 
 
Share this answer
 
Follow this link[^]

There is also Bcrypt available in the Nuget Packagehttps://www.nuget.org/packages/Bcrypt-Official/[^], you can use them. This is easy and has useful functions for encrypt and decrypt as well.

Thanks
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900