Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Proxy
- /**************************************************************************
- * *
- * File: DocumentManager.cs *
- * Copyright: (c) 2008-2010, Florin Leon *
- * E-mail: fleon@cs.tuiasi.ro *
- * Website: http://florinleon.byethost24.com/lab_ip.htm *
- * Description: Secret documents application with Protection Proxy. *
- * The Protection Proxy (Software Engineering lab 9) *
- * *
- * This code and information is provided "as is" without warranty of *
- * any kind, either expressed or implied, including but not limited *
- * to the implied warranties of merchantability or fitness for a *
- * particular purpose. You are free to use this source code in your *
- * applications as long as the original copyright notice is included. *
- * *
- **************************************************************************/
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Windows.Forms;
- namespace ProtectionProxy
- {
- public class ProxyDocumentManager : IDocumentManager
- {
- private RealDocumentManager _realDocumentManager;
- private User _currentUser;
- private List<User> _users;
- private List<string> _levels;
- private const string Path = "Secure\\";
- public string CurrentAccessLevel
- {
- get { return _levels[_currentUser.AccessLevel]; }
- }
- public struct User
- {
- public readonly string Name;
- public readonly string PassHash;
- public readonly int AccessLevel;
- public User(string name, string passHash, int accessLevel)
- {
- Name = name;
- PassHash = passHash;
- AccessLevel = accessLevel;
- }
- }
- public ProxyDocumentManager()
- {
- _levels = new List<string>();
- StreamReader sr = new StreamReader(Path + "niveluri.txt");
- string[] lvls = sr.ReadToEnd().Split(" \t\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
- sr.Close();
- for (int i = 0; i < lvls.Length; i++)
- _levels.Add(lvls[i]);
- _users = new List<User>();
- sr = new StreamReader(Path + "utilizatori.txt");
- while (sr.Peek() != -1)
- {
- string[] toks = sr.ReadLine().Split('\t');
- User user = new User(toks[0], toks[1], Convert.ToInt32(toks[2]));
- _users.Add(user);
- }
- sr.Close();
- }
- #region IDocumentManager Members
- public bool Login(string username, string pass)
- {
- if (_users.Exists(user => user.Name.Equals(username) && user.PassHash.Equals(Cryptography.HashString(pass))))
- {
- _currentUser = _users.Find(user => user.Name.Equals(username) && user.PassHash.Equals(Cryptography.HashString(pass)));
- _realDocumentManager = new RealDocumentManager(_currentUser.AccessLevel);
- return true;
- }
- else
- return false;
- }
- public List<string> GetDocumentList()
- {
- return _realDocumentManager.GetDocumentList();
- }
- public string GetDocument(string documentName, string encryptionPassword)
- {
- return Cryptography.Decrypt(_realDocumentManager.GetDocument(documentName, encryptionPassword), encryptionPassword);
- }
- #endregion IDocumentManager Members
- }
- }
- //Real
- /**************************************************************************
- * *
- * File: DocumentManager.cs *
- * Copyright: (c) 2008-2010, Florin Leon *
- * E-mail: fleon@cs.tuiasi.ro *
- * Website: http://florinleon.byethost24.com/lab_ip.htm *
- * Description: Secret documents application with Protection Proxy. *
- * The real subject (Software Engineering lab 9) *
- * *
- * This code and information is provided "as is" without warranty of *
- * any kind, either expressed or implied, including but not limited *
- * to the implied warranties of merchantability or fitness for a *
- * particular purpose. You are free to use this source code in your *
- * applications as long as the original copyright notice is included. *
- * *
- **************************************************************************/
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace ProtectionProxy
- {
- internal class RealDocumentManager : IDocumentManager
- {
- private static List<List<string>> _documents;
- private const string Path = "Secure\\", DocPath = "Secure\\Documente\\";
- private int _accessLevel;
- static RealDocumentManager()
- {
- int numberOfLevels = 0;
- StreamReader sr = new StreamReader(Path + "drepturi.txt");
- string[] lines = sr.ReadToEnd().Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
- sr.Close();
- numberOfLevels = lines.Length;
- _documents = new List<List<string>>(numberOfLevels);
- for (int i = 0; i < numberOfLevels; i++)
- _documents.Add(new List<string>());
- sr = new StreamReader(Path + "drepturi.txt");
- for (int i = 0; i < numberOfLevels; i++)
- {
- string[] files = sr.ReadLine().Split();
- for (int j = i; j < numberOfLevels; j++)
- {
- for (int k = 0; k < files.Length; k++)
- _documents[j].Add(files[k]);
- }
- }
- sr.Close();
- }
- public RealDocumentManager(int accessLevel)
- {
- _accessLevel = accessLevel;
- }
- #region IDocumentManager Members
- public List<string> GetDocumentList()
- {
- return _documents[_accessLevel];
- }
- public string GetDocument(string documentName, string encryptionPassword)
- {
- string document = File.ReadAllText(DocPath + documentName);
- return Cryptography.Encrypt(document, encryptionPassword);
- }
- #endregion IDocumentManager Members
- }
- }
- //MAin
- /**************************************************************************
- * *
- * File: Form1.cs *
- * Copyright: (c) 2008-2010, Florin Leon *
- * E-mail: fleon@cs.tuiasi.ro *
- * Website: http://florinleon.byethost24.com/lab_ip.htm *
- * Description: Secret documents application with Protection Proxy. *
- * Main form (Software Engineering lab 9) *
- * *
- * This code and information is provided "as is" without warranty of *
- * any kind, either expressed or implied, including but not limited *
- * to the implied warranties of merchantability or fitness for a *
- * particular purpose. You are free to use this source code in your *
- * applications as long as the original copyright notice is included. *
- * *
- **************************************************************************/
- using System.Windows.Forms;
- namespace ProtectionProxy
- {
- public partial class MainForm : Form
- {
- private ProxyDocumentManager _proxyDocumentManager;
- public MainForm()
- {
- InitializeComponent();
- groupBoxAdmin.Enabled = false;
- _proxyDocumentManager = new ProxyDocumentManager();
- }
- private void buttonLogin_Click(object sender, System.EventArgs e)
- {
- string username = textBoxUserName.Text;
- string password = textBoxPassword.Text;
- if(_proxyDocumentManager.Login(username, password))
- {
- textBoxAccessLevel.Text = _proxyDocumentManager.CurrentAccessLevel;
- this.listBoxDocList.Items.Clear();
- foreach (string s in _proxyDocumentManager.GetDocumentList())
- this.listBoxDocList.Items.Add(s);
- }
- }
- private void listBoxDocList_SelectedIndexChanged(object sender, System.EventArgs e)
- {
- richTextBoxDocument.Rtf = _proxyDocumentManager.GetDocument((string)listBoxDocList.SelectedItem, "cevainghilimele");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement