Easy password encryption

runningbull 0 Tallied Votes 336 Views Share

The password salt (the byte array) can be any random collection of numbers - so long as it matches in both the encrypt and decrypt methods.

Also - the same password key needs to be used in both encryption and decryption.

//on the top:

using System.Security.Cryptography;



//in the class
public string Encrypt(string clearText, string Password)
        {
            byte[] clearData = Encoding.Unicode.GetBytes(clearText);
            PasswordDeriveBytes bytes = new PasswordDeriveBytes(Password, new byte[] { 50, 21, 32, 119, 19, 4, 56, 76, 23, 65, 58, 23, 43 });
            return Convert.ToBase64String(this.Encrypt(clearData, bytes.GetBytes(0x20), bytes.GetBytes(0x10)));
        }

public string Decrypt(string cipherText, string Password)
        {
            byte[] cipherData = Convert.FromBase64String(cipherText);
            PasswordDeriveBytes bytes = new PasswordDeriveBytes(Password, new byte[] { 50, 21, 32, 119, 19, 4, 56, 76, 23, 65, 58, 23, 43 });
            byte[] buffer2 = this.Decrypt(cipherData, bytes.GetBytes(0x20), bytes.GetBytes(0x10));
            return Encoding.Unicode.GetString(buffer2);
        


//To implement encryption
string str2 = cs.Encrypt("YourPasswordKey", txtPassword.Text);


//To implement decryption
string text = this.txtPassword.Text;
str3 = cs.Encrypt("YourPasswordKey", text);