Advertisement
Guest User

iplab8

a guest
Apr 15th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.33 KB | None | 0 0
  1. //Proxy
  2. /**************************************************************************
  3. * *
  4. * File: DocumentManager.cs *
  5. * Copyright: (c) 2008-2010, Florin Leon *
  6. * E-mail: fleon@cs.tuiasi.ro *
  7. * Website: http://florinleon.byethost24.com/lab_ip.htm *
  8. * Description: Secret documents application with Protection Proxy. *
  9. * The Protection Proxy (Software Engineering lab 9) *
  10. * *
  11. * This code and information is provided "as is" without warranty of *
  12. * any kind, either expressed or implied, including but not limited *
  13. * to the implied warranties of merchantability or fitness for a *
  14. * particular purpose. You are free to use this source code in your *
  15. * applications as long as the original copyright notice is included. *
  16. * *
  17. **************************************************************************/
  18.  
  19. using System;
  20. using System.Collections.Generic;
  21. using System.IO;
  22. using System.Windows.Forms;
  23.  
  24. namespace ProtectionProxy
  25. {
  26. public class ProxyDocumentManager : IDocumentManager
  27. {
  28. private RealDocumentManager _realDocumentManager;
  29. private User _currentUser;
  30. private List<User> _users;
  31. private List<string> _levels;
  32. private const string Path = "Secure\\";
  33.  
  34. public string CurrentAccessLevel
  35. {
  36. get { return _levels[_currentUser.AccessLevel]; }
  37. }
  38.  
  39. public struct User
  40. {
  41. public readonly string Name;
  42. public readonly string PassHash;
  43. public readonly int AccessLevel;
  44.  
  45. public User(string name, string passHash, int accessLevel)
  46. {
  47. Name = name;
  48. PassHash = passHash;
  49. AccessLevel = accessLevel;
  50. }
  51. }
  52.  
  53. public ProxyDocumentManager()
  54. {
  55. _levels = new List<string>();
  56. StreamReader sr = new StreamReader(Path + "niveluri.txt");
  57. string[] lvls = sr.ReadToEnd().Split(" \t\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  58. sr.Close();
  59.  
  60. for (int i = 0; i < lvls.Length; i++)
  61. _levels.Add(lvls[i]);
  62.  
  63. _users = new List<User>();
  64. sr = new StreamReader(Path + "utilizatori.txt");
  65. while (sr.Peek() != -1)
  66. {
  67. string[] toks = sr.ReadLine().Split('\t');
  68. User user = new User(toks[0], toks[1], Convert.ToInt32(toks[2]));
  69. _users.Add(user);
  70. }
  71. sr.Close();
  72. }
  73.  
  74. #region IDocumentManager Members
  75.  
  76. public bool Login(string username, string pass)
  77. {
  78. if (_users.Exists(user => user.Name.Equals(username) && user.PassHash.Equals(Cryptography.HashString(pass))))
  79. {
  80. _currentUser = _users.Find(user => user.Name.Equals(username) && user.PassHash.Equals(Cryptography.HashString(pass)));
  81. _realDocumentManager = new RealDocumentManager(_currentUser.AccessLevel);
  82. return true;
  83. }
  84. else
  85. return false;
  86. }
  87. public List<string> GetDocumentList()
  88. {
  89. return _realDocumentManager.GetDocumentList();
  90. }
  91.  
  92. public string GetDocument(string documentName, string encryptionPassword)
  93. {
  94. return Cryptography.Decrypt(_realDocumentManager.GetDocument(documentName, encryptionPassword), encryptionPassword);
  95. }
  96.  
  97. #endregion IDocumentManager Members
  98. }
  99. }
  100.  
  101. //Real
  102.  
  103. /**************************************************************************
  104. * *
  105. * File: DocumentManager.cs *
  106. * Copyright: (c) 2008-2010, Florin Leon *
  107. * E-mail: fleon@cs.tuiasi.ro *
  108. * Website: http://florinleon.byethost24.com/lab_ip.htm *
  109. * Description: Secret documents application with Protection Proxy. *
  110. * The real subject (Software Engineering lab 9) *
  111. * *
  112. * This code and information is provided "as is" without warranty of *
  113. * any kind, either expressed or implied, including but not limited *
  114. * to the implied warranties of merchantability or fitness for a *
  115. * particular purpose. You are free to use this source code in your *
  116. * applications as long as the original copyright notice is included. *
  117. * *
  118. **************************************************************************/
  119.  
  120. using System;
  121. using System.Collections.Generic;
  122. using System.IO;
  123.  
  124. namespace ProtectionProxy
  125. {
  126. internal class RealDocumentManager : IDocumentManager
  127. {
  128. private static List<List<string>> _documents;
  129. private const string Path = "Secure\\", DocPath = "Secure\\Documente\\";
  130. private int _accessLevel;
  131. static RealDocumentManager()
  132. {
  133. int numberOfLevels = 0;
  134. StreamReader sr = new StreamReader(Path + "drepturi.txt");
  135. string[] lines = sr.ReadToEnd().Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  136. sr.Close();
  137. numberOfLevels = lines.Length;
  138.  
  139. _documents = new List<List<string>>(numberOfLevels);
  140. for (int i = 0; i < numberOfLevels; i++)
  141. _documents.Add(new List<string>());
  142.  
  143. sr = new StreamReader(Path + "drepturi.txt");
  144. for (int i = 0; i < numberOfLevels; i++)
  145. {
  146. string[] files = sr.ReadLine().Split();
  147. for (int j = i; j < numberOfLevels; j++)
  148. {
  149. for (int k = 0; k < files.Length; k++)
  150. _documents[j].Add(files[k]);
  151. }
  152. }
  153. sr.Close();
  154. }
  155.  
  156. public RealDocumentManager(int accessLevel)
  157. {
  158. _accessLevel = accessLevel;
  159. }
  160. #region IDocumentManager Members
  161.  
  162. public List<string> GetDocumentList()
  163. {
  164. return _documents[_accessLevel];
  165. }
  166.  
  167. public string GetDocument(string documentName, string encryptionPassword)
  168. {
  169. string document = File.ReadAllText(DocPath + documentName);
  170. return Cryptography.Encrypt(document, encryptionPassword);
  171. }
  172.  
  173. #endregion IDocumentManager Members
  174. }
  175. }
  176.  
  177. //MAin
  178. /**************************************************************************
  179. * *
  180. * File: Form1.cs *
  181. * Copyright: (c) 2008-2010, Florin Leon *
  182. * E-mail: fleon@cs.tuiasi.ro *
  183. * Website: http://florinleon.byethost24.com/lab_ip.htm *
  184. * Description: Secret documents application with Protection Proxy. *
  185. * Main form (Software Engineering lab 9) *
  186. * *
  187. * This code and information is provided "as is" without warranty of *
  188. * any kind, either expressed or implied, including but not limited *
  189. * to the implied warranties of merchantability or fitness for a *
  190. * particular purpose. You are free to use this source code in your *
  191. * applications as long as the original copyright notice is included. *
  192. * *
  193. **************************************************************************/
  194.  
  195. using System.Windows.Forms;
  196.  
  197. namespace ProtectionProxy
  198. {
  199. public partial class MainForm : Form
  200. {
  201. private ProxyDocumentManager _proxyDocumentManager;
  202.  
  203. public MainForm()
  204. {
  205. InitializeComponent();
  206. groupBoxAdmin.Enabled = false;
  207. _proxyDocumentManager = new ProxyDocumentManager();
  208. }
  209.  
  210. private void buttonLogin_Click(object sender, System.EventArgs e)
  211. {
  212. string username = textBoxUserName.Text;
  213. string password = textBoxPassword.Text;
  214.  
  215. if(_proxyDocumentManager.Login(username, password))
  216. {
  217. textBoxAccessLevel.Text = _proxyDocumentManager.CurrentAccessLevel;
  218. this.listBoxDocList.Items.Clear();
  219.  
  220. foreach (string s in _proxyDocumentManager.GetDocumentList())
  221. this.listBoxDocList.Items.Add(s);
  222. }
  223. }
  224.  
  225. private void listBoxDocList_SelectedIndexChanged(object sender, System.EventArgs e)
  226. {
  227. richTextBoxDocument.Rtf = _proxyDocumentManager.GetDocument((string)listBoxDocList.SelectedItem, "cevainghilimele");
  228. }
  229. }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement