Monday, 19 October 2015

Decrypt Text

   private string Decrypt(string cipherText)  //For Decrypt
    {
        string EncryptionKey = "MAKV2SPBNI99212";
        byte[] cipherBytes = Convert.FromBase64String(cipherText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(cipherBytes, 0, cipherBytes.Length);
                    cs.Close();
                }
                cipherText = Encoding.Unicode.GetString(ms.ToArray());
            }
        }
        return cipherText;
    }


    private void passcheck()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from ustable where uid='" + Encrypt(TextBox1.Text) + "'", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable(); da.Fill(dt);
        con.Close();
        if (TextBox1.Text == Decrypt(dt.Rows[0]["uid"].ToString()))
        {
            Response.Write("password checked");
        }
        else
        {
            Response.Write("Not Valid");
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        passcheck();
    }

No comments:

Post a Comment