Exemple #1
0
        public void DeSerialCryptoDate()
        {
            string            fileName      = @"\Date.dat";
            string            directoryName = Directory.GetCurrentDirectory();
            string            path          = directoryName + @"\Date" + fileName;
            DateTimeSpeichern dateTime      = null;

            if (File.Exists(path))
            {
                FileStream fsOpen = File.OpenRead(path);

                byte[] bytearrayinput = new byte[fsOpen.Length];
                fsOpen.Read(bytearrayinput, 0, bytearrayinput.Length);
                fsOpen.Close();
                //FileStream fsWrite = File.OpenWrite(path);

                using (Aes aesAlg = Aes.Create())
                {
                    aesAlg.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                    aesAlg.IV  = ASCIIEncoding.ASCII.GetBytes(sIV);

                    // Create a decrytor to perform the stream transform.
                    ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                    using (MemoryStream msDecrypt = new MemoryStream(bytearrayinput))
                    {
                        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            try
                            {
                                BinaryFormatter formatter = new BinaryFormatter();

                                dateTime = (DateTimeSpeichern)formatter.Deserialize(csDecrypt);
                            }
                            catch (SerializationException)
                            {
                                MessageBox.Show("Eine Fehler ist aufgetretten.");
                                throw;
                            }
                            //finally
                            //{
                            //    fs.Close();
                            //}
                        }
                    }
                }
                if (dateTime.LetzteAnderung == DateTime.Today.Date)
                {
                    dataPickerAdd.SelectedDate = dateTime.TimeSpeichern;
                }
                else
                {
                    dataPickerAdd.SelectedDate = DateTime.Today;
                }
            }
            else
            {
                dataPickerAdd.SelectedDate = DateTime.Today;
            }
        }
Exemple #2
0
        public void SerialCryptoDate()
        {
            string        fileName      = @"\Date.dat";
            string        directoryName = Directory.GetCurrentDirectory();
            DirectoryInfo neueDirectory = Directory.CreateDirectory(directoryName + @"\Date");
            string        path          = directoryName + @"\Date" + fileName;

            #region FileAccess for date

            //File.SetAttributes(path, FileAttributes.Hidden);
            //необходимо дописать доступ к файлу, для того что бы он не изменялся
            //в файле, а был изменен только в программе

            //FileSecurity fs = new FileSecurity();
            //fs.AddAccessRule(new FileSystemAccessRule(@"DOMAINNAME\AccountName",
            //                                                FileSystemRights.ReadData,
            //                                                AccessControlType.Allow));
            //File.SetAttributes(path, FileAttributes.Encrypted);

            #endregion

            //Serialization
            DateTimeSpeichern dateTime = new DateTimeSpeichern();
            FileStream        stream   = new FileStream(path, FileMode.Create);
            dateTime.TimeSpeichern  = dataPickerAdd.SelectedDate.Value.Date;
            dateTime.LetzteAnderung = DateTime.Now.Date;
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, dateTime);
            stream.Close();

            //Cryptografy
            FileStream fsOpen = File.OpenRead(path);
            byte[]     input  = new byte[fsOpen.Length];
            fsOpen.Read(input, 0, input.Length);
            fsOpen.Close();
            FileStream fsWrite = File.OpenWrite(path);

            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                aesAlg.IV  = ASCIIEncoding.ASCII.GetBytes(sIV);

                // Create a decrytor to perform the stream transform.
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                using (CryptoStream csEncrypt = new CryptoStream(fsWrite, encryptor, CryptoStreamMode.Write))
                {
                    csEncrypt.Write(input, 0, input.Length);
                }
            }
            fsWrite.Close();
        }