//private method which writes the eMart data onto a file after encrypting it private void ToFile(E_Mart_Store p, string password, string file) { byte[] passwordBytes = StrToByte(password); byte[] bytes = serialize.SerializeObjectToByteArray(p); byte[] encryptedBytes = aes.AES_Encrypt(bytes, passwordBytes); ByteToFile(encryptedBytes, file); }
//private method which reads the encrypted data from file, decrypts it and enters it into eMart variable private E_Mart_Store FromFile(string file, string password) { byte[] passwordBytes = StrToByte(password); byte[] fileInBytes = FileToByte(file); byte[] decryptedFileInBytes = aes.AES_Decrypt(fileInBytes, passwordBytes); E_Mart_Store p = serialize.DeserializeByteArrayToObject(decryptedFileInBytes); return(p); }
/* * input: E_Mart_Store object * output: byte array * action: serialization */ public byte[] SerializeObjectToByteArray(E_Mart_Store p) { if (p == null) { throw new ArgumentNullException(); } XmlSerializer serializer = new XmlSerializer(typeof(E_Mart_Store)); using (MemoryStream memoryStream = new MemoryStream()) { using (XmlWriter xmlWriter = XmlWriter.Create(memoryStream)) { serializer.Serialize(xmlWriter, p); //writing the serialized data onto memoryStream return(memoryStream.ToArray()); //returns an memory string converted to a byteArray } } }
public static void isEncyptionOK() { IDAL adal = new LINQ_DAL(); IBL abl = new E_Mart_BL(adal); Department d1 = new Department("Department 1"); d1.DepartmentID = 1; User user = new User("Naharda", "Kawabanga"); Employee emp = new Employee(305, "Yair", "LAnd", 1, 50000.5, "Male", -1); Product prod = new Product("Banana", "Food", 1, 1, InStock.True, 300, 2.5, 100); ClubMember clu = new ClubMember("2516", "Googi", "Sheldi", "Male", "08/10/1989"); clu.MemberID = 1; Receipt rec = new Receipt(); ProductSale ps = new ProductSale(prod, 3); rec.addProductSale(ps); Transaction tranc = new Transaction(rec, "Cash"); tranc.TransactionID = 1; E_Mart_Store e = new E_Mart_Store(); e.addClubMember(clu); e.addDepartment(d1); e.addEmployee(emp); e.addProduct(prod); e.addTransaction(tranc); e.addUser(user); Serialize b = new Serialize(); AES aes = new AES(); String s1 = "password"; byte[] a = b.SerializeObjectToByteArray(e); byte[] password = new byte[s1.Length * sizeof(char)]; System.Buffer.BlockCopy(s1.ToCharArray(), 0, password, 0, password.Length); byte[] ans = aes.AES_Encrypt(a, password); Assert.IsFalse(ByteArrayCompare(a, ans)); byte[] ans2 = aes.AES_Decrypt(ans, password); Assert.IsTrue(ByteArrayCompare(ans2, a)); }
//loads data from file to this.eMart variable and then enitializes the rest public void loadDataFromFile() { this.eMart = FromFile(filename, password); enitializeLinqDal(); //toConsole(this.eMart); }