Example #1
0
 //-------------------------------------------------------------------------
 public DbContent()
 {
     Provider       = ProviderType.OleDB;
     CommandTimeout = 15;
     Fields         = new List <string>();
     ed             = new EncrDecr(string.Empty, string.Empty);
     dbController   = SqlController.CreateSqlController(this);
 }
        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);
                    }
                }
            }
        }