Esempio n. 1
0
        public string Load(FileData fileData)
        {
            Filestream = new FileStream(fileData.FileName, FileMode.Open);
            EncryptorFactory = new EncryptorFactory(fileData.EncryptionType, fileData.Password, Filestream);
            StreamReader streamReader = new StreamReader(EncryptorFactory.Decrypt());

            string message = streamReader.ReadToEnd();

            // Pull Hash out of message
            string delimiter = "(?<=[\n])";
            string[] plaintext = Regex.Split(message, @delimiter);

            message = "";
            for (int i = 0; i < plaintext.Length - 1; i++)
            {
                message += plaintext[i];
            }

            // Hash message
            CreateHash(fileData.HashType);
            ASCIIEncoding encoder = new ASCIIEncoding();
            byte[] input = encoder.GetBytes(message);
            byte[] output = Hash.ComputeHash(input);

            // Turn hash into string

            string[] msghash = plaintext[plaintext.Length - 1].Split(new char[] { '\0' });

            string originalhash = Convert.ToBase64String(output);

            if (msghash[0] != originalhash)
            {
                throw new Exception("Access Denied!");
            }

            // Display or return results
            return message;
        }
Esempio n. 2
0
        public void Save(FileData fileData)
        {
            using (Filestream = new FileStream(fileData.FileName, FileMode.Create))
            {
                EncryptorFactory = new EncryptorFactory(fileData.EncryptionType, fileData.Password, Filestream);

                // Hash message
                CreateHash(fileData.HashType);
                ASCIIEncoding encoder = new ASCIIEncoding();
                byte[] input = encoder.GetBytes(fileData.Message);
                byte[] output = Hash.ComputeHash(input);

                // Turn hash into string
                string s = Convert.ToBase64String(output);

                // Append hash to message
                fileData.Message += s;

                using (StreamWriter streamWriter = new StreamWriter(EncryptorFactory.Encrypt(Filestream)))
                {
                    // Write message to file and close the stream
                    streamWriter.Write(fileData.Message);
                }
            }
        }