Exemple #1
0
        /// <summary>
        /// XML序列化并对文件进行加密
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="obj">对象实例</param>
        /// <param name="type">对象类型</param>
        /// <returns></returns>
        public bool XmlSerializeEncrypt(string path, object obj, Type type)
        {
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

            ns.Add("", "");

            try
            {
                if (!File.Exists(path))
                {
                    FileInfo fi = new FileInfo(path);
                    if (!fi.Directory.Exists)
                    {
                        Directory.CreateDirectory(fi.Directory.FullName);
                    }
                }

                using (Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    string content = "";
                    using (MemoryStream ms = new MemoryStream())
                    {
                        XmlSerializer format = new XmlSerializer(type);
                        format.Serialize(ms, obj, ns);
                        ms.Seek(0, 0);
                        content = Encoding.ASCII.GetString(ms.ToArray());
                    }

                    string encrypt = EncodeHelper.EncryptString(content);
                    byte[] bytes   = UTF8Encoding.Default.GetBytes(encrypt);
                    stream.Write(bytes, 0, bytes.Length);
                    stream.Close();
                }


                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(false);
            }
        }
Exemple #2
0
        /// <summary>
        /// 从"独立存贮空间"取程序第一次运行的时间并解密
        /// </summary>
        /// <returns></returns>
        public static string GetDataTime()
        {
            string fromDataTime;

            //按用户、域、程序集获取独立存储区
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User
                                                                        | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, null, null);

            string[] myusername = isoStore.GetDirectoryNames(UIConstants.IsolatedStorageDirectoryName);
            if (myusername.Length == 0) //没有文件夹
            {
                return(string.Empty);   //域中没有他的目录
            }

            myusername = isoStore.GetFileNames(UIConstants.IsolatedStorage);
            if (myusername.Length == 0) //没有文件
            {
                return(string.Empty);   //域中没有他的用户名
            }
            else
            {
                using (IsolatedStorageFileStream isoStream1 = new IsolatedStorageFileStream(UIConstants.IsolatedStorage, FileMode.OpenOrCreate, isoStore))
                {
                    using (StreamReader reader = new StreamReader(isoStream1))
                    {
                        fromDataTime = reader.ReadLine();
                    }
                }
                if (!string.IsNullOrEmpty(fromDataTime)) //解密
                {
                    try
                    {
                        fromDataTime = EncodeHelper.DesDecrypt(fromDataTime, UIConstants.IsolatedStorageEncryptKey);
                    }
                    catch
                    {
                    }
                }
                return(fromDataTime);
            }
        }
Exemple #3
0
        /// <summary>
        /// XML反序列化并解密
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="type">对象类型</param>
        /// <returns></returns>
        public object XmlDeserializeDecrypt(string path, Type type)
        {
            try
            {
                string encrypt = File.ReadAllText(path, Encoding.UTF8);
                string content = EncodeHelper.DecryptString(encrypt, true);

                byte[] bytes = UTF8Encoding.Default.GetBytes(content);
                using (MemoryStream stream = new MemoryStream(bytes))
                {
                    XmlSerializer formatter = new XmlSerializer(type);

                    stream.Seek(0, SeekOrigin.Begin);
                    object obj = formatter.Deserialize(stream);
                    stream.Close();
                    return(obj);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(null);
            }
        }
Exemple #4
0
        /// <summary>
        /// 加密并保存当前时间到"独立存贮空间" (以分号(;)追加保存)
        /// </summary>
        public static void SaveDataTime(DateTime fromDate)
        {
            string fromDataTime = fromDate.ToString("MM-dd-yyyy HH:mm:ss");
            string oldTime      = GetDataTime().Trim();

            if (!string.IsNullOrEmpty(oldTime))
            {
                fromDataTime = oldTime + ";" + fromDataTime;                                             //追加最后时间到左边
            }
            fromDataTime = EncodeHelper.DesEncrypt(fromDataTime, UIConstants.IsolatedStorageEncryptKey); //加密

            #region 将fromDataTime保存在"独立存贮空间"

            string username = fromDataTime;
            //按用户、域、程序集获取独立存储区
            IsolatedStorageFile isoStore =
                IsolatedStorageFile.GetStore(
                    IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, null, null);
            string[] myusername = isoStore.GetDirectoryNames(UIConstants.IsolatedStorageDirectoryName);
            IsolatedStorageFileStream isoStream1 = null;
            if (myusername.Length == 0) //没有目录
            {
                //创建目录
                isoStore.CreateDirectory(UIConstants.IsolatedStorageDirectoryName);
                //创建文件
                using (isoStream1 = new IsolatedStorageFileStream(UIConstants.IsolatedStorage, FileMode.Create, isoStore))
                {
                    //写入文件
                    using (StreamWriter writer = new StreamWriter(isoStream1))
                    {
                        writer.WriteLine(fromDataTime);
                    }
                }
            }
            else
            {
                myusername = isoStore.GetFileNames(UIConstants.IsolatedStorage);
                if (myusername.Length == 0) //没有文件
                {
                    //创建文件
                    using (isoStream1 = new IsolatedStorageFileStream(UIConstants.IsolatedStorage, FileMode.Create, isoStore))
                    {
                        //写入文件
                        using (StreamWriter writer = new StreamWriter(isoStream1))
                        {
                            writer.WriteLine(fromDataTime);
                        }
                    }
                }
                else
                {
                    using (isoStream1 = new IsolatedStorageFileStream(UIConstants.IsolatedStorage, FileMode.Open, isoStore))
                    {
                        //写入文件
                        using (StreamWriter writer = new StreamWriter(isoStream1))
                        {
                            writer.WriteLine(fromDataTime);
                        }
                    }
                }
            }

            #endregion
        }