private void Confirm(object sender, RoutedEventArgs e)
        {
            //add this---> if(OldPin==OldPin set on Main Page)
            //try{
            if ((NewPin.Text.Length < 4) || (NewPin.Text.Contains(".")))     //checks if pincode consists of 4 digits
            {
                MessageBox.Show("Please enter 4 characters.(Digits only!)");
                return;
            }
            else
            {
                if (NewPin.Text.Trim() != CNewPin.Text.Trim())     //checks if newpin=confirm newpin
                {
                    MessageBox.Show("New Pin and Confirm Pin do not match!");
                    return;
                }
                else
                {
                    DataProtection readdata         = new DataProtection();
                    byte[]         ProtectedPinByte = readdata.ReadPinFromFile();

                    // Decrypt the PIN by using the Unprotect method.
                    // if I enter null or some byte, Unprotect method generates exception.
                    // return message is "The parameter is incorrect"
                    byte[] PinByte = ProtectedData.Unprotect(ProtectedPinByte, null);
                    // Convert the PIN from byte to string
                    string EncPincode = Encoding.UTF8.GetString(PinByte, 0, PinByte.Length);


                    if (OldPin.Text != EncPincode)
                    {
                        MessageBox.Show("Old pincode does not match!");
                        return;
                    }
                    else if (OldPin.Text == NewPin.Text)
                    {
                        MessageBox.Show("Old pincode eqauls New pincode!");
                        return;
                    }
                    else
                    {
                        DataProtection writedata           = new DataProtection();
                        byte[]         NewPinByte          = Encoding.UTF8.GetBytes(NewPin.Text);
                        byte[]         ProtectedNewPinByte = ProtectedData.Protect(NewPinByte, null);
                        writedata.WritePinToFile(ProtectedNewPinByte);
                        //this.WritePinToFile(ProtectedNewPinByte);
                        MessageBox.Show("You successfully changed your pincode!");
                        OldPin.Text  = "";
                        NewPin.Text  = "";
                        CNewPin.Text = "";
                    }
                }
            }
        }
示例#2
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // using(IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
            try
            {
                DataProtection readdata             = new DataProtection();
                byte[]         ReadProtectedPinByte = readdata.ReadPinFromFile();
                string         ProtectedPincode     = Encoding.UTF8.GetString(ReadProtectedPinByte, 0, ReadProtectedPinByte.Length);

                // byte[] ReadPinByte = ProtectedData.Unprotect(ReadProtectedPinByte, null);
                //string EncPincode = Encoding.UTF8.GetString(ReadPinByte, 0, ReadPinByte.Length);

                if (ProtectedPincode == "")
                {
                    DataProtection writedata        = new DataProtection();
                    byte[]         PinByte          = Encoding.UTF8.GetBytes(PinBox.Text);
                    byte[]         ProtectedPinByte = ProtectedData.Protect(PinByte, null);
                    writedata.WritePinToFile(ProtectedPinByte);
                    MessageBox.Show("Your pincode is set!");
                    NavigationService.Navigate(new Uri("/Login.xaml", UriKind.Relative));
                    return;
                }
                else
                {
                    byte[] ReadPinByte = ProtectedData.Unprotect(ReadProtectedPinByte, null);
                    string EncPincode  = Encoding.UTF8.GetString(ReadPinByte, 0, ReadPinByte.Length);
                    if (EncPincode.CompareTo(PinBox.Text) == 0)
                    {
                    }
                    else
                    {
                        MessageBox.Show("pincode is unmatch");
                        return;
                    }
                }

                if (PinBox.Text.Length != 4 || PinBox.Text.Any(char.IsDigit) != true)
                {
                    MessageBox.Show("Error! Please enter a 4 digit code");
                    return;
                }

                NavigationService.Navigate(new Uri("/Login.xaml", UriKind.Relative)); //Work Normally

                /*using (IsolatedStorageFileStream stream = isoStorage.OpenFile("pincode.txt", FileMode.OpenOrCreate))
                 * {
                 *  using (StreamWriter writer = new StreamWriter(stream))
                 *  {
                 *      writer.WriteLine(PswdBox.Password);
                 *      MessageBox.Show("Success! Your pincode is set");
                 *      NavigationService.Navigate(new Uri("/Login.xaml", UriKind.Relative));
                 *      writer.Close();
                 *  }
                 * }*/
            }
            catch (IsolatedStorageException)
            {
                MessageBox.Show("IsolatedStorageException");
                NavigationService.Navigate(new Uri("/Login.xaml", UriKind.Relative));
            }
            catch (ArgumentNullException)
            {
                MessageBox.Show("ArgumentnullException");
            }
        }