private static PadlockFile ParsePazwordFile(string file) { var xdoc = XDocument.Parse(file); PadlockFile pf = new PadlockFile(); try { var info = xdoc.Root.Element("info"); var guid = info.Attribute("guid").Value; var parse = Guid.Parse(guid); pf.Id = Guid.Parse(xdoc.Root.Element("info").Attribute("guid").Value); foreach (var node in xdoc.Root.Element("nodes").Elements("node")) { var card = new DataFile.Card(); card.Id = Guid.Parse(node.Attribute("guid").Value); card.Title = node.Attribute("title").Value; card.IsFavotire = node.Attribute("favorite") != null?node.Attribute("favorite").Value.Equals("1") : false; card.UsedCounter = node.Attribute("usedcounter") != null?int.Parse((node.Attribute("usedcounter").Value)) : 0; card.Notes = ""; if (node.Element("moreinfo") != null) { foreach (var row in node.Element("moreinfo").Elements("line")) { card.Notes += row.Value + "\r\n"; } } foreach (var attr in node.Elements("attribute")) { DataFile.Attribute attribute = new DataFile.Attribute(); if (attr.Attribute("value").Value == "") { attribute.Type = AttributeType.TYPE_HEADER; } else { if (attr.Attribute("type") != null) { if (attr.Attribute("type").Value == "password") { attribute.Type = AttributeType.TYPE_PASSWORD; } if (attr.Attribute("type").Value == "generic") { attribute.Type = AttributeType.TYPE_STRING; } if (attr.Attribute("type").Value == "URL") { attribute.Type = AttributeType.TYPE_URL; } } } attribute.Name = attr.Attribute("name").Value; attribute.Value = attr.Attribute("value").Value; card.Rows.Add(attribute); } pf.Cards.Add(card); } } catch { } return(pf); }
public static DataFile.PadlockFile OpenFile(byte[] source, string unlockPassword) { EncrDecr ed; ed = new EncrDecr(); byte[] crypt_key; // public key for encrypted doc byte[] crypt_iv; // init vectory for encrypted doc crypt_key = new byte[EncrDecr.C_KEYSIZE]; crypt_iv = new byte[EncrDecr.C_IVSIZE]; using (MemoryStream memory = new MemoryStream(source)) { // Use the memory stream in a binary reader. using (BinaryReader br = new BinaryReader(memory, System.Text.Encoding.UTF8)) { string tmp; // read 'PASSWORD-' tmp = br.ReadString(); // set password int i; for (i = 0; i < unlockPassword.Length && i < EncrDecr.C_KEYSIZE; i++) { crypt_key[i] = (byte)(unlockPassword[i] & 0xFF); } byte[] data = new Byte[memory.Length - (tmp.Length + 1) - (EncrDecr.C_KEYSIZE - i) - EncrDecr.C_IVSIZE]; // read crypt-key for (; i < EncrDecr.C_KEYSIZE; i++) { crypt_key[i] = br.ReadByte(); } // read crypt-iv br.Read(crypt_iv, 0, EncrDecr.C_IVSIZE); // read VerifyText byte[] data_test = new Byte[C_VERIFYTEXT.Length * 2 + 16]; br.Read(data_test, 0, C_VERIFYTEXT.Length * 2 + 16); string decrypted = ed.Decrypt(data_test, crypt_key, crypt_iv); string verifyText = C_VERIFYTEXT; int compare = decrypted.CompareTo(verifyText); if (compare != 0) { // wrong password return null string return(null); } else { // correct password return xml byte[] data_frag = new Byte[C_BLOCKSIZE * 2 + 16]; int realsize; System.Text.StringBuilder xml = new System.Text.StringBuilder(10000); while ((realsize = br.Read(data_frag, 0, C_BLOCKSIZE * 2 + 16)) > 0) { int olds = xml.Length; ed = new EncrDecr(); string element = ed.Decrypt(data_frag, crypt_key, crypt_iv); xml.Append(element, 0, element.Length); } PadlockFile pf = ParsePazwordFile(xml.ToString()); //string json = JsonConvert.SerializeObject(pf); //PadlockFile pf2 = JsonConvert.DeserializeObject<PadlockFile>(json); return(pf); } } } }