Пример #1
0
        private void save_encrypted(string szFileName)
        {
            EncrDecr ed;

            System.IO.FileStream   fs = new System.IO.FileStream(szFileName, System.IO.FileMode.Create);
            System.IO.BinaryWriter sw = new System.IO.BinaryWriter(fs, System.Text.Encoding.UTF8);

            sw.Write(PASSWORD_PROTECTED);

            int    i, l;
            string tmp;

            for (i = unlockPassword.Length; i < EncrDecr.C_KEYSIZE; i++)
            {
                sw.Write(crypt_key[i]);
            }
            //sw.Write(crypt_key);

            sw.Write(crypt_iv);

            ed = new EncrDecr();
            sw.Write(ed.Encrypt(C_VERIFYTEXT, crypt_key, crypt_iv));

            tmp = xmlDoc.OuterXml;
            l   = tmp.Length;
            for (i = 0; i < l; i += C_BLOCKSIZE)
            {
                if (i + C_BLOCKSIZE <= l)
                {
                    ed = new EncrDecr();
                    sw.Write(ed.Encrypt(tmp.Substring(i, C_BLOCKSIZE), crypt_key, crypt_iv));
                }
                else
                {
                    ed = new EncrDecr();
                    sw.Write(ed.Encrypt(tmp.Substring(i), crypt_key, crypt_iv));
                }
            }

            sw.Close();
            fs.Close();
        }
Пример #2
0
        public bool TryPassword(string password)
        {
            bool bRet = false;

            unlockPassword = password;
            string tmp;

            EncrDecr ed;

            ed = new EncrDecr();

            crypt_key = new byte[EncrDecr.C_KEYSIZE];
            crypt_iv  = new byte[EncrDecr.C_IVSIZE];

            System.IO.FileStream   fs = new System.IO.FileStream(FileName, System.IO.FileMode.Open);
            System.IO.BinaryReader br = new System.IO.BinaryReader(fs, System.Text.Encoding.UTF8);

            // 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[fs.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);


            if (ed.Decrypt(data_test, crypt_key, crypt_iv).CompareTo(C_VERIFYTEXT) != 0)
            {
                unlockPassword = "";
                bRet           = false;
            }
            else
            {
                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);
                }

                xmlDoc.LoadXml(xml.ToString());
                this.szFullfile = null;
                bRet            = true;
            }

            checkXMLValidity();

            br.Close();
            fs.Close();
            return(bRet);
        }