Maintaining passwords with integrity

How to save passwords in a local application. I have a C# application in VS that requires a password when logging in, after logging in the password can be changed, but the question is how to save this password. I know about hashing and encryption, but how can I protect a file with a password from, for example, deleting or changing the data in it?

Author: Max Mikheyenko, 2016-02-29

3 answers

In the local application - nothing. If you really need protection, make a client-server application.

 2
Author: Pavel Mayorov, 2016-02-29 19:38:02

The Cryptography API and Windows Data Protection are suitable for this purpose. Examples of using this API are mainly in C/C++, but I think it should not be a problem to translate this to C# or find a wrapper.

There is a article on using this API in Russian,

 1
Author: ixSci, 2016-03-01 05:22:24
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;


namespace Forms
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            check_similar ();//проверяет соответствие сохраненного пароля и введенного(авторизация)
            //список имеющихся функций и их смысл:
            //check_enter();проверяет есть ли резервный файл пароля и проверяет пароль из этого файла.

            //Safe(); создает хэш для введенных данных
            /*controller ();проверяет был ли удален файл с паролем из проводника если да,
                            то читает данные из скрытого файла и проверяет 
                            пароль(если удалить скрытый файл,то ничего не будет работать, это сделано специально)*/

            //look (); если отсутствуют все нужные для пароля файлы, то они создаются и предлагают придумать пароль.
        }

        static void look()
        {
            string create = "conf", security = "control", text;
            Console.Write ("Придумайте пароль   -->   ");
            text = Console.ReadLine();
            if (text.Length < 9) {
                Console.Write ("Слишком короткий. ");
                look ();
            }else{
                using (MD5 crypt = MD5.Create ()) {
                    string data = Safe (crypt, text);
                    File.WriteAllText (create, data);
                    File.SetAttributes (create, FileAttributes.ReadOnly);

                    File.WriteAllText (security, data);
                    File.SetAttributes (security, FileAttributes.Hidden);
                }
            }
        }

        static void controller(){
            string create = "conf", security = "control";
            if (!File.Exists (create)){
                string read = File.ReadAllText (security);
                File.WriteAllText (create, read);
                File.SetAttributes (create, FileAttributes.ReadOnly);
            } else {
                Console.Write ("File is exists! ");
            }
        }


        static string Safe(MD5 crypt, string input){
            byte[] code = crypt.ComputeHash(Encoding.UTF8.GetBytes (input));    
            StringBuilder strings = new StringBuilder ();
            for (int i = 0; i < code.Length; i++) {
                strings.Append (code[i].ToString("x2"));
            }
            return strings.ToString (); 
        }

        static void check_similar(){
            string security = "control", data_check, data_enter;
            if (File.Exists (security)) {
                Console.Write ("Для входа ведите пароль! --->   ");
                data_check = Console.ReadLine ();
                using (MD5 crypt = MD5.Create ()) {
                    data_enter = Safe (crypt, data_check);
                    if (data_enter == File.ReadAllText (security)) {
                        controller ();
                    } else {
                        Console.Write ("Неверно! Попробуйте еще! \n");
                        check_similar ();
                    }
                }
            } else {
                look ();
            }
        }
    }
}
 0
Author: umd, 2018-02-25 11:20:16