示例#1
0
        public static User LoadUser(string path)
        {
            if (!File.Exists(path))
            {
                if (File.Exists(Path.ChangeExtension(path, "check")))
                {
                    File.Delete(Path.ChangeExtension(path, "check"));
                }
                return(null);
            }
            if (!File.Exists(Path.ChangeExtension(path, "check")))
            {
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                return(null);
            }
            string       pwd = GetPassword("");
            MemoryStream ms;

            while (!Check(pwd, out ms))
            {
                pwd = InputBoxPassWord();
                if (pwd == "")
                {
                    break;
                }
                pwd = GetPassword(pwd);
            }

            bool Check(string password, out MemoryStream stream)
            {
                Secret s = new Secret(GetSecretKey(password));

                try
                {
                    stream = s.DecryptFileToStream(path);
                }
                catch
                {
                    stream = null;
                    return(false);
                }
                stream.Seek(0, SeekOrigin.Begin);
                byte[] data = new byte[stream.Length];
                stream.Read(data, 0, data.Length);
                stream.Seek(0, SeekOrigin.Begin);
                string md5    = data.GetMD5();
                string soumd5 = File.ReadAllText(Path.ChangeExtension(path, "check")).Trim();

                if (md5 == soumd5)
                {
                    return(true);
                }
                else
                {
                    //stream = null;
                    return(false);
                }
            }

            try
            {
                using (BinaryReader br = new BinaryReader(ms, Encoding.Unicode))
                {
                    User user = new User
                    {
                        Password  = br.ReadString(),
                        AppID     = br.ReadString(),
                        SecretKey = br.ReadString(),
                        IsCheck   = br.ReadBoolean(),
                        Name      = br.ReadString(),
                        Length    = br.ReadInt64(),
                        Stop      = br.ReadBoolean()
                    };
                    return(user);
                }
            }
            catch
            {
                return(null);
            }
            finally
            {
                //GData.Secret.EncryptionFile(path);
            }
        }