Monday, July 22, 2013

Password Encryption and Decryption In C#

Code

using System.Text;


    protected void btnOk_Click(object sender, EventArgs e)
    {
        string strOriginal = txtPassword.Text;
        string strEncrypted = Encryptdata(strOriginal);
        string strDecrypted = Decryptdata(strEncrypted);
        lblOriginal.Text  = strOriginal;
        lblEncrypted.Text = strEncrypted;
        lblDecrypted.Text  = strDecrypted;

    }
    private string Encryptdata(string password)
    {
        string strmsg = string.Empty;
        byte[] encode = new byte[password.Length];
        encode = Encoding.UTF8.GetBytes(password);
        strmsg = Convert.ToBase64String(encode);
        return strmsg;
    }
    private string Decryptdata(string encryptpwd)
    {
        string decryptpwd = string.Empty;
        UTF8Encoding encodepwd = new UTF8Encoding();
        Decoder Decode = encodepwd.GetDecoder();
        byte[] todecode_byte = Convert.FromBase64String(encryptpwd);
        int charCount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
        char[] decoded_char = new char[charCount];
        Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
        decryptpwd = new String(decoded_char);
        return decryptpwd;

    }

No comments:

Post a Comment